1- import type { Plugin , ResolvedConfig } from "vite" ;
2- import type { FilterPattern } from "vite" ;
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,23 @@ 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" ;
10+ import { preactDevtoolsPlugin } from "./devtools.js" ;
11+ import { createFilter , parseId } from "./utils.js" ;
1712
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
13+ /**
14+ * @typedef {import('vite').Plugin } Plugin
15+ * @typedef {import('vite').ResolvedConfig } ResolvedConfig
16+ * @typedef {import('@babel/parser').ParserPlugin } ParserPlugin
17+ *
18+ * @typedef {import('./index.d.ts').preact } PreactPlugin
19+ * @typedef {import('./index.d.ts').PreactBabelOptions } PreactBabelOptions
20+ */
21+
22+ /**
23+ * Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts
24+ *
25+ * @type {PreactPlugin }
26+ */
10727function preactPlugin ( {
10828 devToolsEnabled,
10929 prefreshEnabled,
@@ -113,24 +33,25 @@ function preactPlugin({
11333 exclude,
11434 babel,
11535 jsxImportSource,
116- } : PreactPluginOptions = { } ) : Plugin [ ] {
36+ } = { } ) {
11737 const baseParserOptions = [
11838 "importMeta" ,
11939 "explicitResourceManagement" ,
12040 "topLevelAwait" ,
12141 ] ;
122- let config : ResolvedConfig ;
42+ /** @type {ResolvedConfig } */
43+ let config ;
12344
124- let babelOptions = {
45+ let babelOptions = /** @type { PreactBabelOptions } */ ( {
12546 babelrc : false ,
12647 configFile : false ,
12748 ...babel ,
128- } as PreactBabelOptions ;
49+ } ) ;
12950
13051 babelOptions . plugins ||= [ ] ;
13152 babelOptions . presets ||= [ ] ;
13253 babelOptions . overrides ||= [ ] ;
133- babelOptions . parserOpts ||= { } as any ;
54+ babelOptions . parserOpts ||= /** @type { any } */ ( { } ) ;
13455 babelOptions . parserOpts . plugins ||= [ ] ;
13556
13657 let useBabel = typeof babel !== "undefined" ;
@@ -153,7 +74,8 @@ function preactPlugin({
15374 }
15475 }
15576
156- const jsxPlugin : Plugin = {
77+ /** @type {Plugin } */
78+ const jsxPlugin = {
15779 name : "vite:preact-jsx" ,
15880 enforce : "pre" ,
15981 config ( ) {
@@ -204,7 +126,7 @@ function preactPlugin({
204126
205127 if ( ! useBabel || ! shouldTransform ( id ) ) return ;
206128
207- const parserPlugins = [
129+ const parserPlugins = /** @type { ParserPlugin[] } */ ( [
208130 ...baseParserOptions ,
209131 "classProperties" ,
210132 "classPrivateProperties" ,
@@ -214,7 +136,7 @@ function preactPlugin({
214136 // Whilst our limited transforms (JSX & hook names) are fine, if users
215137 // add their own, they may run into unhelpful errors. See #170
216138 / \. [ c m ] ? t s x ? $ / . test ( id ) && typeof babel === "undefined" && "typescript" ,
217- ] . filter ( Boolean ) as ParserPlugin [ ] ;
139+ ] . filter ( Boolean ) ) ;
218140
219141 const result = await transformAsync ( code , {
220142 ...babelOptions ,
@@ -243,7 +165,7 @@ function preactPlugin({
243165 ...( devToolsEnabled ? [ babelHookNames ] : [ ] ) ,
244166 ] ,
245167 sourceMaps : true ,
246- inputSourceMap : false as any ,
168+ inputSourceMap : undefined ,
247169 } ) ;
248170
249171 // NOTE: Since no config file is being loaded, this path wouldn't occur.
0 commit comments