Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure compatibility with vite v6.0.7 #63

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
"@types/jest": "^29.5.12",
"@types/uuid": "^10.0.0",
"cz-conventional-changelog": "^3.3.0",
"esbuild": "^0.23.0",
"esbuild": "^0.24.0",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"prettier": "^3.3.3",
"ts-jest": "^29.2.3",
"typescript": "^5.5.3",
"vite": "^5.3.4"
"vite": "^6.0.7"
},
"dependencies": {
"@rollup/plugin-virtual": "^3.0.2",
Expand Down
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export type { Options } from "./options";

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

// Use the same default target as Vite.
// https://github.com/vitejs/vite/blob/v6.0.7/packages/vite/src/node/constants.ts#L70-L76
const DEFAULT_VITE_TARGET: ViteTarget = [
'es2020',
'edge88',
'firefox78',
'chrome87',
'safari14',
]

export default function topLevelAwait(options?: Options): Plugin {
const resolvedOptions: Options = {
...DEFAULT_OPTIONS,
Expand Down Expand Up @@ -47,24 +57,26 @@ export default function topLevelAwait(options?: Options): Plugin {
isWorkerIifeRequested = true;
}
},
configResolved(config) {
if (config.command === "build") {
if (config.isWorker) {
config(config, env) {
if (env.command === "build") {
if (config.worker) {
isWorker = true;
}

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

minify = !!config.build.minify;

assetsDir = config.build.assetsDir;
}

if (config.command === "serve") {
if (env.command === "serve") {
// Fix errors in NPM packages which are getting pre-processed in development build
if (config.optimizeDeps?.esbuildOptions) {
config.optimizeDeps.esbuildOptions.target = "esnext";
Expand Down
Loading