Error when manually including vite/modulepreload-polyfill
See original GitHub issueDescribe the bug
When manually including vite/modulepreload-polyfill
as per backend integration an error is thrown in the browser when running vite serve
.
Uncaught ReferenceError: __VITE_IS_MODERN__ is not defined
<anonymous> http://localhost:3000/@id/vite/modulepreload-polyfill:43
I’m guessing because __VITE_IS_MODERN__
is replaced during build and during “normal” Vite usage the polyfill is only added to the output during build, but with a backend integration the polyfill is present during dev and build.
Reproduction
Issue can be replicated outside of a backend integration by:
- Importing
vite/modulepreload-polyfill
into an entry file - Running
vite serve
, visiting the dev server and opening the browser dev tools
This yields the same result as I’m seeing with my backend setup.
https://github.com/rynpsc/vite-modulepreload-polyfill
System Info
System:
OS: macOS 11.5.2
CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Memory: 681.98 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 12.18.3 - ~/.nvm/versions/node/v12.18.3/bin/node
npm: 7.5.2 - ~/.nvm/versions/node/v12.18.3/bin/npm
Browsers:
Edge: 92.0.902.84
Firefox: 91.0.1
Safari: 14.1.2
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:28
- Comments:8
Top Results From Across the Web
Build Options - Vite
By default, a module preload polyfill is automatically injected. ... then it is necessary to manually import the polyfill in your custom entry:....
Read more >Features | Vite
CSS Modules #. Any CSS file ending with .module.css is considered a CSS modules file. Importing such a file will return the corresponding...
Read more >Configuring Vite
You can use --force flag or manually delete the directory to regenerate the cache files. The value can be either an absolute file...
Read more >Backend Integration - Vite
If you want to serve the HTML using a traditional backend (e.g. Rails, Laravel) but use Vite for serving assets, check for existing...
Read more >How do I polyfill the "process" Node module in the Vite dev ...
Interestingly, I don't see this error when I create a production build. How can I polyfill process with a no-op so that the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
How do you turn the dynamic into a static import? With just this solution, the modulepreload-polyfill is always in it’s own chunk.
For those who already changed manualChunks from it’s default value then
vite/modulepreload-polyfill
can be added to manualChunks so that it behave as a static import. But it’s not always practical.Unfortunately in TypeScript, the workaround throws the error:
Cannot find module 'vite/modulepreload-polyfill' or its corresponding type declarations.
My workaround was to copy the polyfill function at https://github.com/vitejs/vite/blob/cb75dbd4a2b1c56e2a7aeaf1625818a4d1865f00/packages/vite/src/node/plugins/modulePreloadPolyfill.ts and run it in its own file.