question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when manually including vite/modulepreload-polyfill

See original GitHub issue

Describe 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:

  1. Importing vite/modulepreload-polyfill into an entry file
  2. 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:28
  • Comments:8

github_iconTop GitHub Comments

3reactions
jeromeboncommented, Dec 24, 2021
if (import.meta.env.MODE !== 'development') {
  import('vite/modulepreload-polyfill')
}

Here is my workaround.

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.

1reaction
kaibyaocommented, Dec 2, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found