-
Notifications
You must be signed in to change notification settings - Fork 59
/
index.ts
298 lines (276 loc) · 8.94 KB
/
index.ts
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { readFileSync } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { SourceMapPayload } from "module";
import {
Output,
ParserConfig,
ReactConfig,
JscTarget,
transform,
} from "@swc/core";
import { PluginOption, UserConfig, BuildOptions } from "vite";
import { createRequire } from "module";
const runtimePublicPath = "/@react-refresh";
const preambleCode = `import { injectIntoGlobalHook } from "__PATH__";
injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => (type) => type;`;
const _dirname =
typeof __dirname !== "undefined"
? __dirname
: dirname(fileURLToPath(import.meta.url));
const resolve = createRequire(
typeof __filename !== "undefined" ? __filename : import.meta.url,
).resolve;
const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;
const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
type Options = {
/**
* Control where the JSX factory is imported from.
* @default "react"
*/
jsxImportSource?: string;
/**
* Enable TypeScript decorators. Requires experimentalDecorators in tsconfig.
* @default false
*/
tsDecorators?: boolean;
/**
* Use SWC plugins. Enable SWC at build time.
* @default undefined
*/
plugins?: [string, Record<string, any>][];
/**
* Set the target for SWC in dev. This can avoid to down-transpile private class method for example.
* For production target, see https://vitejs.dev/config/build-options.html#build-target
* @default "es2020"
*/
devTarget?: JscTarget;
/**
* Override the default include list (.ts, .tsx, .mts, .jsx, .mdx).
* This requires to redefine the config for any file you want to be included.
* If you want to trigger fast refresh on compiled JS, use `jsx: true`.
* Exclusion of node_modules should be handled by the function if needed.
*/
parserConfig?: (id: string) => ParserConfig | undefined;
};
const isWebContainer = globalThis.process?.versions?.webcontainer;
const react = (_options?: Options): PluginOption[] => {
let hmrDisabled = false;
const options = {
jsxImportSource: _options?.jsxImportSource ?? "react",
tsDecorators: _options?.tsDecorators,
plugins: _options?.plugins
? _options?.plugins.map((el): typeof el => [resolve(el[0]), el[1]])
: undefined,
devTarget: _options?.devTarget ?? "es2020",
parserConfig: _options?.parserConfig,
};
return [
{
name: "vite:react-swc:resolve-runtime",
apply: "serve",
enforce: "pre", // Run before Vite default resolve to avoid syscalls
resolveId: (id) => (id === runtimePublicPath ? id : undefined),
load: (id) =>
id === runtimePublicPath
? readFileSync(join(_dirname, "refresh-runtime.js"), "utf-8")
: undefined,
},
{
name: "vite:react-swc",
apply: "serve",
config: () => ({
esbuild: false,
optimizeDeps: {
include: [`${options.jsxImportSource}/jsx-dev-runtime`],
esbuildOptions: { jsx: "automatic" },
},
}),
configResolved(config) {
if (config.server.hmr === false) hmrDisabled = true;
const mdxIndex = config.plugins.findIndex(
(p) => p.name === "@mdx-js/rollup",
);
if (
mdxIndex !== -1 &&
mdxIndex >
config.plugins.findIndex((p) => p.name === "vite:react-swc")
) {
throw new Error(
"[vite:react-swc] The MDX plugin should be placed before this plugin",
);
}
if (isWebContainer) {
config.logger.warn(
"[vite:react-swc] SWC is currently not supported in WebContainers. You can use the default React plugin instead.",
);
}
},
transformIndexHtml: (_, config) => [
{
tag: "script",
attrs: { type: "module" },
children: preambleCode.replace(
"__PATH__",
config.server!.config.base + runtimePublicPath.slice(1),
),
},
],
async transform(code, _id, transformOptions) {
const id = _id.split("?")[0];
const refresh = !transformOptions?.ssr && !hmrDisabled;
const result = await transformWithOptions(
id,
code,
options.devTarget,
options,
{
refresh,
development: true,
runtime: "automatic",
importSource: options.jsxImportSource,
},
);
if (!result) return;
if (!refresh) return result;
const hasRefresh = refreshContentRE.test(result.code);
if (!hasRefresh && !reactCompRE.test(result.code)) return result;
const sourceMap: SourceMapPayload = JSON.parse(result.map!);
sourceMap.mappings = ";;" + sourceMap.mappings;
result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
${result.code}`;
if (hasRefresh) {
sourceMap.mappings = ";;;;;;" + sourceMap.mappings;
result.code = `if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
const prevRefreshReg = window.$RefreshReg$;
const prevRefreshSig = window.$RefreshSig$;
window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
${result.code}
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;
`;
}
result.code += `
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
import.meta.hot.accept((nextExports) => {
if (!nextExports) return;
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate("${id}", currentExports, nextExports);
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
});
});
`;
return { code: result.code, map: sourceMap };
},
},
options.plugins
? {
name: "vite:react-swc",
apply: "build",
enforce: "pre", // Run before esbuild
config: (userConfig) => ({
build: silenceUseClientWarning(userConfig),
}),
transform: (code, _id) =>
transformWithOptions(_id.split("?")[0], code, "esnext", options, {
runtime: "automatic",
importSource: options.jsxImportSource,
}),
}
: {
name: "vite:react-swc",
apply: "build",
config: (userConfig) => ({
build: silenceUseClientWarning(userConfig),
esbuild: {
jsx: "automatic",
jsxImportSource: options.jsxImportSource,
tsconfigRaw: {
compilerOptions: { useDefineForClassFields: true },
},
},
}),
},
];
};
const transformWithOptions = async (
id: string,
code: string,
target: JscTarget,
options: Options,
reactConfig: ReactConfig,
) => {
const decorators = options?.tsDecorators ?? false;
const parser: ParserConfig | undefined = options.parserConfig
? options.parserConfig(id)
: id.endsWith(".tsx")
? { syntax: "typescript", tsx: true, decorators }
: id.endsWith(".ts") || id.endsWith(".mts")
? { syntax: "typescript", tsx: false, decorators }
: id.endsWith(".jsx")
? { syntax: "ecmascript", jsx: true }
: id.endsWith(".mdx")
? // JSX is required to trigger fast refresh transformations, even if MDX already transforms it
{ syntax: "ecmascript", jsx: true }
: undefined;
if (!parser) return;
let result: Output;
try {
result = await transform(code, {
filename: id,
swcrc: false,
configFile: false,
sourceMaps: true,
jsc: {
target,
parser,
experimental: { plugins: options.plugins },
transform: {
useDefineForClassFields: true,
react: reactConfig,
},
},
});
} catch (e: any) {
const message: string = e.message;
const fileStartIndex = message.indexOf("╭─[");
if (fileStartIndex !== -1) {
const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
if (match) {
e.line = match[1];
e.column = match[2];
}
}
throw e;
}
return result;
};
const silenceUseClientWarning = (userConfig: UserConfig): BuildOptions => ({
rollupOptions: {
onwarn(warning, defaultHandler) {
if (
warning.code === "MODULE_LEVEL_DIRECTIVE" &&
warning.message.includes("use client")
) {
return;
}
// https://github.com/vitejs/vite/issues/15012
if (
warning.code === "SOURCEMAP_ERROR" &&
warning.message.includes("resolve original location") &&
warning.pos === 0
) {
return;
}
if (userConfig.build?.rollupOptions?.onwarn) {
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
} else {
defaultHandler(warning);
}
},
},
});
export default react;