1- import type { Plugin , ResolvedConfig } from "vite" ;
2- import type { FilterPattern } from "@rollup/pluginutils" ;
3- import type { ParserPlugin , ParserOptions } from "@babel/parser" ;
4- import type { TransformOptions } from "@babel/core" ;
5-
61import prefresh from "@prefresh/vite" ;
7- import { preactDevtoolsPlugin } from "./devtools.js" ;
8- import { createFilter , parseId } from "./utils.js" ;
92import { vitePrerenderPlugin } from "vite-prerender-plugin" ;
103import { transformAsync } from "@babel/core" ;
114// @ts -ignore package doesn't ship with declaration files
@@ -14,96 +7,25 @@ import babelReactJsx from "@babel/plugin-transform-react-jsx";
147import babelReactJsxDev from "@babel/plugin-transform-react-jsx-development" ;
158// @ts -ignore package doesn't ship with declaration files
169import babelHookNames from "babel-plugin-transform-hook-names" ;
17-
18- export type BabelOptions = Omit <
19- TransformOptions ,
20- | "ast"
21- | "filename"
22- | "root"
23- | "sourceFileName"
24- | "sourceMaps"
25- | "inputSourceMap"
26- > ;
27-
28- export interface PreactPluginOptions {
29- /**
30- * Whether to use Preact devtools
31- * @default !isProduction
32- */
33- devToolsEnabled ?: boolean ;
34-
35- /**
36- * Whether to use prefresh HMR
37- * @default true
38- */
39- prefreshEnabled ?: boolean ;
40-
41- /**
42- * Whether to alias react, react-dom to preact/compat
43- * @default true
44- */
45- reactAliasesEnabled ?: boolean ;
46-
47- /**
48- * Prerender plugin options
49- */
50- prerender ?: {
51- /**
52- * Whether to prerender your app on build
53- */
54- enabled : boolean ;
55- /**
56- * Absolute path to script containing an exported `prerender()` function
57- */
58- prerenderScript ?: string ;
59- /**
60- * Query selector for specifying where to insert prerender result in your HTML template
61- */
62- renderTarget ?: string ;
63- /**
64- * Additional routes that should be prerendered
65- */
66- additionalPrerenderRoutes ?: string [ ] ;
67- /**
68- * Vite's preview server won't use our prerendered HTML by default, this middleware correct this
69- */
70- previewMiddlewareEnabled ?: boolean ;
71- /**
72- * Path to use as a fallback/404 route, i.e., `/404` or `/not-found`
73- */
74- previewMiddlewareFallback ?: string ;
75- } ;
76-
77- /**
78- * RegExp or glob to match files to be transformed
79- */
80- include ?: FilterPattern ;
81-
82- /**
83- * RegExp or glob to match files to NOT be transformed
84- */
85- exclude ?: FilterPattern ;
86-
87- /**
88- * Babel configuration applied in both dev and prod.
89- */
90- babel ?: BabelOptions ;
91- /**
92- * Import Source for jsx. Defaults to "preact".
93- */
94- jsxImportSource ?: string ;
95- }
96-
97- export interface PreactBabelOptions extends BabelOptions {
98- plugins : Extract < BabelOptions [ "plugins" ] , any [ ] > ;
99- presets : Extract < BabelOptions [ "presets" ] , any [ ] > ;
100- overrides : Extract < BabelOptions [ "overrides" ] , any [ ] > ;
101- parserOpts : ParserOptions & {
102- plugins : Extract < ParserOptions [ "plugins" ] , any [ ] > ;
103- } ;
104- }
105-
106- // Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts
10+ import { createFilter } from "@rollup/pluginutils" ;
11+ import { preactDevtoolsPlugin } from "./devtools.js" ;
12+ import { parseId } from "./utils.js" ;
13+
14+ /**
15+ * @typedef {import('vite').Plugin } Plugin
16+ * @typedef {import('vite').ResolvedConfig } ResolvedConfig
17+ * @typedef {import('@babel/parser').ParserPlugin } ParserPlugin
18+ *
19+ * @typedef {import('./index.d.ts').PreactPluginOptions } PreactPluginOptions
20+ * @typedef {import('./index.d.ts').PreactBabelOptions } PreactBabelOptions
21+ */
22+
23+ /**
24+ * Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts
25+ *
26+ * @param {PreactPluginOptions } options
27+ * @returns {import('vite').Plugin[] }
28+ */
10729function preactPlugin ( {
10830 devToolsEnabled,
10931 prefreshEnabled,
@@ -113,24 +35,25 @@ function preactPlugin({
11335 exclude,
11436 babel,
11537 jsxImportSource,
116- } : PreactPluginOptions = { } ) : Plugin [ ] {
38+ } = { } ) {
11739 const baseParserOptions = [
11840 "importMeta" ,
11941 "explicitResourceManagement" ,
12042 "topLevelAwait" ,
12143 ] ;
122- let config : ResolvedConfig ;
44+ /** @type {ResolvedConfig } */
45+ let config ;
12346
124- let babelOptions = {
47+ let babelOptions = /** @type { PreactBabelOptions } */ ( {
12548 babelrc : false ,
12649 configFile : false ,
12750 ...babel ,
128- } as PreactBabelOptions ;
51+ } ) ;
12952
13053 babelOptions . plugins ||= [ ] ;
13154 babelOptions . presets ||= [ ] ;
13255 babelOptions . overrides ||= [ ] ;
133- babelOptions . parserOpts ||= { } as any ;
56+ babelOptions . parserOpts ||= /** @type { any } */ ( { } ) ;
13457 babelOptions . parserOpts . plugins ||= [ ] ;
13558
13659 let useBabel = typeof babel !== "undefined" ;
@@ -153,7 +76,8 @@ function preactPlugin({
15376 }
15477 }
15578
156- const jsxPlugin : Plugin = {
79+ /** @type {Plugin } */
80+ const jsxPlugin = {
15781 name : "vite:preact-jsx" ,
15882 enforce : "pre" ,
15983 config ( ) {
@@ -204,7 +128,7 @@ function preactPlugin({
204128
205129 if ( ! useBabel || ! shouldTransform ( id ) ) return ;
206130
207- const parserPlugins = [
131+ const parserPlugins = /** @type { ParserPlugin[] } */ ( [
208132 ...baseParserOptions ,
209133 "classProperties" ,
210134 "classPrivateProperties" ,
@@ -214,7 +138,7 @@ function preactPlugin({
214138 // Whilst our limited transforms (JSX & hook names) are fine, if users
215139 // add their own, they may run into unhelpful errors. See #170
216140 / \. [ c m ] ? t s x ? $ / . test ( id ) && typeof babel !== "undefined" && "typescript" ,
217- ] . filter ( Boolean ) as ParserPlugin [ ] ;
141+ ] . filter ( Boolean ) ) ;
218142
219143 const result = await transformAsync ( code , {
220144 ...babelOptions ,
@@ -243,7 +167,7 @@ function preactPlugin({
243167 ...( devToolsEnabled ? [ babelHookNames ] : [ ] ) ,
244168 ] ,
245169 sourceMaps : true ,
246- inputSourceMap : false as any ,
170+ inputSourceMap : null ,
247171 } ) ;
248172
249173 // NOTE: Since no config file is being loaded, this path wouldn't occur.
0 commit comments