Skip to content

Commit 8216a43

Browse files
ocavueMenci
andauthored
fix: ensure compatibility with vite v6.0.7 (#63)
* fix: ensure compatibility with vite v6.0.7 * remove unrelated update * set default target * format --------- Co-authored-by: Menci <[email protected]>
1 parent ae432b6 commit 8216a43

File tree

4 files changed

+332
-432
lines changed

4 files changed

+332
-432
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
"@types/jest": "^29.5.12",
3333
"@types/uuid": "^10.0.0",
3434
"cz-conventional-changelog": "^3.3.0",
35-
"esbuild": "^0.23.0",
35+
"esbuild": "^0.24.0",
3636
"jest": "^29.7.0",
3737
"jest-extended": "^4.0.2",
3838
"prettier": "^3.3.3",
3939
"ts-jest": "^29.2.3",
4040
"typescript": "^5.5.3",
41-
"vite": "^5.3.4"
41+
"vite": "^6.0.7"
4242
},
4343
"dependencies": {
4444
"@rollup/plugin-virtual": "^3.0.2",

src/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export type { Options } from "./options";
1313

1414
type ViteTarget = ResolvedConfig["build"]["target"];
1515

16+
// Use the same default target as Vite.
17+
// https://github.com/vitejs/vite/blob/v6.0.7/packages/vite/src/node/constants.ts#L70-L76
18+
const DEFAULT_VITE_TARGET: ViteTarget = ["es2020", "edge88", "firefox78", "chrome87", "safari14"];
19+
1620
export default function topLevelAwait(options?: Options): Plugin {
1721
const resolvedOptions: Options = {
1822
...DEFAULT_OPTIONS,
@@ -47,24 +51,26 @@ export default function topLevelAwait(options?: Options): Plugin {
4751
isWorkerIifeRequested = true;
4852
}
4953
},
50-
configResolved(config) {
51-
if (config.command === "build") {
52-
if (config.isWorker) {
54+
config(config, env) {
55+
if (env.command === "build") {
56+
if (config.worker) {
5357
isWorker = true;
5458
}
5559

5660
// By default Vite transforms code with esbuild with target for a browser list with ES modules support
5761
// This cause esbuild to throw an exception when there're top-level awaits in code
58-
// Let's backup the original target and override the esbuild target with "esnext", which allows TLAs
59-
buildTarget = config.build.target;
62+
// Let's backup the original target and override the esbuild target with "esnext", which allows TLAs.
63+
// If the user doesn't specify a target explicitly, `config.build.target` will be undefined and we'll
64+
// use the default Vite target.
65+
buildTarget = config.build.target ?? DEFAULT_VITE_TARGET;
6066
config.build.target = "esnext";
6167

6268
minify = !!config.build.minify;
6369

6470
assetsDir = config.build.assetsDir;
6571
}
6672

67-
if (config.command === "serve") {
73+
if (env.command === "serve") {
6874
// Fix errors in NPM packages which are getting pre-processed in development build
6975
if (config.optimizeDeps?.esbuildOptions) {
7076
config.optimizeDeps.esbuildOptions.target = "esnext";

src/transform.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,22 @@ export function transformModule(
192192
imports.flatMap(importStmt => importStmt.specifiers.map(specifier => specifier.local.value))
193193
);
194194
const exportFromedNames = new Set(
195-
exportFroms.flatMap(exportStmt => exportStmt.specifiers.map(specifier => {
196-
if (specifier.type === "ExportNamespaceSpecifier") {
197-
return specifier.name.value;
198-
} else if (specifier.type === "ExportDefaultSpecifier") {
199-
// When will this happen?
200-
return specifier.exported.value;
201-
} else {
202-
return (specifier.exported || specifier.orig).value;
203-
}
204-
}))
195+
exportFroms.flatMap(exportStmt =>
196+
exportStmt.specifiers.map(specifier => {
197+
if (specifier.type === "ExportNamespaceSpecifier") {
198+
return specifier.name.value;
199+
} else if (specifier.type === "ExportDefaultSpecifier") {
200+
// When will this happen?
201+
return specifier.exported.value;
202+
} else {
203+
return (specifier.exported || specifier.orig).value;
204+
}
205+
})
206+
)
207+
);
208+
const exportedNamesDeclaration = makeVariablesDeclaration(
209+
exportedNames.filter(name => !importedNames.has(name) && !exportFromedNames.has(name))
205210
);
206-
const exportedNamesDeclaration = makeVariablesDeclaration(exportedNames.filter(name => !importedNames.has(name) && !exportFromedNames.has(name)));
207211

208212
const warppedStatements = topLevelStatements.flatMap<SWC.Statement>(stmt => {
209213
if (stmt.type === "VariableDeclaration") {
@@ -368,7 +372,12 @@ export function transformModule(
368372
* export { ..., __tla };
369373
*/
370374

371-
const newTopLevel: SWC.ModuleItem[] = [...imports, ...newImportsByExportFroms, ...exportFroms, exportedNamesDeclaration];
375+
const newTopLevel: SWC.ModuleItem[] = [
376+
...imports,
377+
...newImportsByExportFroms,
378+
...exportFroms,
379+
exportedNamesDeclaration
380+
];
372381

373382
if (exportedNames.length > 0 || bundleInfo[moduleName]?.importedBy?.length > 0) {
374383
// If the chunk is being imported, append export of the TLA promise to export list

0 commit comments

Comments
 (0)