-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Dynamic import().then(m => m.foo)
works in dev but fails in prod
#19695
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
Comments
To investigate, I experimented with this configuration: export default {
build: {
minify: false,
rollupOptions: {
preserveEntrySignatures: true,
treeshake: false,
output: {
preserveModules: true
},
}
},
}; And this shows that this isn't a tree-shaking issue, but rather that the Compiled foo.js: const foo = {
bar: 123
};
export {
foo
}; Compiled index.js: import { __vitePreload } from "../_virtual/preload-helper-DAnU8ViX.js";
(async () => {
const { bar } = await __vitePreload(async () => {
const { bar: bar2 } = await import("./foo-Wx1xxurL.js");
return { bar: bar2 };
}, true ? [] : void 0).then((a) => a.foo);
console.log(bar);
})(); The vite/packages/vite/src/node/plugins/importAnalysisBuild.ts Lines 348 to 351 in 998303b
|
I dug deeper into this. Looks like the And the case I'm encountering is not handled here: vite/packages/vite/src/node/plugins/importAnalysisBuild.ts Lines 258 to 304 in 998303b
I think this regular expression approach is frail and will encounter more edge-cases in the future (I'm thinking many people have already encountered this but just didn't debug/report). Planning to look into alternative solutions soon. |
Describe the bug
Dynamically importing a file and extracting the named export with a chained
.then(m => m.foo)
doesn't seem to work when the project is built (it works fine in dev mode).index.js
foo.js
Error
Built code
^ Seems foo.js is completely empty.
This reproduction is super boiled down, but for context, the bug originated from inside a library dependency that's also built with Vite. While avoiding
.then(m => m.foo)
might be a workaround, that's not easy to do as it's coming from minified dependency code.Reproduction
https://stackblitz.com/edit/vitejs-vite-jdxhwfbi?file=src%2Findex.js
Steps to reproduce
npm start
(which runsvite build && vite preview
)System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: