Replies: 5 comments 2 replies
-
|
Hey! I ran into something similar before. This is basically a Rollup limitation — if a module is statically and dynamically imported from the same file, Rollup just won't split it into a separate chunk. The static import already pulls it into the bundle so the dynamic What worked for me was splitting the imports into separate wrapper files so Rollup sees them as different entry points: // polyfill-check.js
export { isSupported } from "invokers-polyfill/fn";// polyfill-apply.js
export { apply } from "invokers-polyfill/fn";// polyfill.js
import { isSupported } from "./polyfill-check";
void (async () => {
if (!isSupported) {
const { apply } = await import("./polyfill-apply");
apply();
}
})();Your re-export workaround probably didn't work because Rollup still traces both imports back to the same source module. With separate files it actually sees two different entry points and splits them into chunks. Related: rollup/rollup#4951 |
Beta Was this translation helpful? Give feedback.
-
|
Thanks. I've tried here https://codesandbox.io/p/devbox/vite-polyfill-dynamic-import-will-not-move-into-another-chunk-with-re-export-workaround-forked-wcjwpl?workspaceId=ws_SohSVZzbWrDK5D8vU7Zh1F It seems to work, but only if I specify |
Beta Was this translation helpful? Give feedback.
-
|
Hi @imagoiq! Vite is usually blazingly fast, but issues like this often crop up around dependency pre-bundling or rollup configurations.
Let me know if clearing the Vite cache or tweaking the Rollup config gets you past this hurdle. Hope this points you in the right direction! Let me know how it goes. Happy coding! |
Beta Was this translation helpful? Give feedback.
-
|
Remove the static import entirely. Since isSupported is just a feature-detection boolean, replicate that check inline yourself. Then the dynamic import is truly lazy and Rollup will split it into its own chunk. void (async () => { if (!isSupported) { |
Beta Was this translation helpful? Give feedback.
-
|
This is a fundamental constraint in Rollup's chunking algorithm (inherited by Vite). When a module is both statically and dynamically imported from the same file, the static import wins: the module gets pulled into the importer's chunk, and the dynamic The wrapper-file approach mentioned above works, but you shouldn't need Here's the cleanest pattern for conditional polyfills: // polyfill.js
void (async () => {
const { isSupported } = await import("invokers-polyfill/fn");
if (!isSupported) {
const { apply } = await import("invokers-polyfill/fn");
apply();
}
})();Both imports are dynamic, so Rollup can split If you need the check to be synchronous (zero network cost when the feature is supported), split them into two distinct modules so Rollup sees no overlap: // detect.js -- only exports the boolean
export { isSupported } from "invokers-polyfill/fn";
// polyfill.js
import { isSupported } from "./detect.js";
if (!isSupported) {
const { apply } = await import("invokers-polyfill/fn");
apply();
}This works without touching The reason your earlier re-export workaround didn't split: Rollup traces through re-exports and sees the same underlying module ID on both the static and dynamic sides. The Why |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I would love to be able to optimize polyfill loading, but I can't find a proper way. Here is the idea, import
isSupportedto check if the polyfill is needed and import dynamically theapplyfunction only when needed.With this code, I'm getting the following warning on build, and all code appears in the same file:
✓ 3 modules transformed. [plugin vite:reporter] (!) /project/workspace/node_modules/.pnpm/invokers-polyfill@1.0.1/node_modules/invokers-polyfill/invoker.js is dynamically imported by /project/workspace/polyfill.js but also statically imported by /project/workspace/polyfill.js, dynamic import will not move module into another chunk. dist/assets/polyfill-BcPiOrKD.js 6.82 kB │ gzip: 2.57 kBYou can check with this demo and running
npm run build: https://codesandbox.io/p/devbox/9zg3qmUsing a "re-export workaround" hides the warning but doesn't create another chunk for the dynamic import: https://codesandbox.io/p/devbox/vite-polyfill-dynamic-import-will-not-move-into-another-chunk-with-re-export-workaround-h8l733
Note that it happens with other polyfill like https://github.com/oddbird/popover-polyfill (but it's probably using the same structure)
Is there a way? Is there an issue with those modules? What am I missing?
Related links ?:
Beta Was this translation helpful? Give feedback.
All reactions