-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathindex.js
More file actions
214 lines (199 loc) · 5.77 KB
/
index.js
File metadata and controls
214 lines (199 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import prefresh from "@prefresh/vite";
import { createFilter } from "@rollup/pluginutils";
import { transformAsync } from "@babel/core";
// @ts-ignore package doesn't ship with declaration files
import babelReactJsx from "@babel/plugin-transform-react-jsx";
// @ts-ignore package doesn't ship with declaration files
import babelReactJsxDev from "@babel/plugin-transform-react-jsx-development";
// @ts-ignore package doesn't ship with declaration files
import babelHookNames from "babel-plugin-transform-hook-names";
import { preactDevtoolsPlugin } from "./devtools.js";
import { transformHookNamesPlugin } from "./transform-hook-names.js";
import { parseId } from "./utils.js";
/** @import { Plugin, ResolvedConfig } from "vite"; */
/** @import { ParserPlugin } from "@babel/parser"; */
/** @import { Options as PrerenderPluginOptions } from "vite-prerender-plugin" */
/** @import { preact as PreactPlugin, PreactBabelOptions } from "./index.d.ts" */
/** @type {(options?: PrerenderPluginOptions) => Plugin[]} */
let vitePrerenderPlugin;
try {
({ vitePrerenderPlugin } = await import("vite-prerender-plugin"));
} catch {}
/**
* Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts
*
* @type {PreactPlugin}
*/
function preactPlugin({
devToolsEnabled,
prefreshEnabled,
reactAliasesEnabled,
prerender,
include,
exclude,
babel,
jsxImportSource,
} = {}) {
const baseParserOptions = [
"importMeta",
"explicitResourceManagement",
"topLevelAwait",
];
/** @type {ResolvedConfig} */
let config;
let babelOptions = /** @type {PreactBabelOptions} */ ({
babelrc: false,
configFile: false,
...babel,
});
babelOptions.plugins ||= [];
babelOptions.presets ||= [];
babelOptions.overrides ||= [];
babelOptions.parserOpts ||= /** @type {any} */ ({});
babelOptions.parserOpts.plugins ||= [];
const useBabel = typeof babel !== "undefined";
const shouldTransform = createFilter(
include || [/\.[cm]?[tj]sx?$/],
exclude || [/node_modules/],
);
prefreshEnabled = prefreshEnabled ?? true;
reactAliasesEnabled = reactAliasesEnabled ?? true;
prerender = prerender ?? { enabled: false };
/** @type {Plugin} */
const jsxPlugin = {
name: "vite:preact-jsx",
enforce: "pre",
config() {
return {
build: {
rollupOptions: {
onwarn(warning, warn) {
// Silence Rollup's module-level directive warnings re:"use client".
// They're likely to come from `node_modules` and won't be actionable.
if (
warning.code === "MODULE_LEVEL_DIRECTIVE" &&
warning.message.includes("use client")
)
return;
// ESBuild seemingly doesn't include mappings for directives, causing
// Rollup to emit warnings about missing source locations. This too is
// likely to come from `node_modules` and won't be actionable.
// evanw/esbuild#3548
if (
warning.code === "SOURCEMAP_ERROR" &&
warning.message.includes("resolve original location") &&
warning.pos === 0
)
return;
warn(warning);
},
},
},
esbuild: useBabel
? undefined
: {
jsx: "automatic",
jsxImportSource: jsxImportSource ?? "preact",
},
optimizeDeps: {
include: ["preact", "preact/jsx-runtime", "preact/jsx-dev-runtime"],
},
};
},
configResolved(resolvedConfig) {
config = resolvedConfig;
devToolsEnabled = devToolsEnabled ?? !config.isProduction;
},
async transform(code, url) {
// Ignore query parameters, as in Vue SFC virtual modules.
const { id } = parseId(url);
if (!useBabel || !shouldTransform(id)) return;
const parserPlugins = /** @type {ParserPlugin[]} */ ([
...baseParserOptions,
"classProperties",
"classPrivateProperties",
"classPrivateMethods",
!/\.[cm]?ts$/.test(id) && "jsx",
// Babel doesn't support many transforms without also transforming TS.
// Whilst our limited transforms (JSX & hook names) are fine, if users
// add their own, they may run into unhelpful errors. See #170
/\.[cm]?tsx?$/.test(id) && typeof babel === "undefined" && "typescript",
].filter(Boolean));
const result = await transformAsync(code, {
...babelOptions,
ast: true,
root: config.root,
filename: id,
parserOpts: {
...babelOptions.parserOpts,
sourceType: "module",
allowAwaitOutsideFunction: true,
plugins: parserPlugins,
},
generatorOpts: {
...babelOptions.generatorOpts,
decoratorsBeforeExport: true,
},
plugins: [
...babelOptions.plugins,
[
config.isProduction ? babelReactJsx : babelReactJsxDev,
{
runtime: "automatic",
importSource: jsxImportSource ?? "preact",
},
],
...(devToolsEnabled ? [babelHookNames] : []),
],
sourceMaps: true,
inputSourceMap: undefined,
});
// NOTE: Since no config file is being loaded, this path wouldn't occur.
if (!result) return;
return {
code: result.code || code,
map: result.map,
};
},
};
return [
...(reactAliasesEnabled
? [
{
name: "preact:config",
config() {
return {
resolve: {
alias: {
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat",
"react/jsx-runtime": "preact/jsx-runtime",
react: "preact/compat",
},
},
};
},
},
]
: []),
jsxPlugin,
...(!useBabel
? [
transformHookNamesPlugin({
devToolsEnabled,
shouldTransform,
}),
]
: []),
preactDevtoolsPlugin({
devToolsEnabled,
shouldTransform,
}),
...(prefreshEnabled
? [prefresh({ include, exclude, parserPlugins: baseParserOptions })]
: []),
...(prerender.enabled ? vitePrerenderPlugin(prerender) : []),
];
}
export default preactPlugin;
export { preactPlugin as preact };