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

Error when manually including vite/modulepreload-polyfill #4786

Closed
7 tasks done
rynpsc opened this issue Aug 29, 2021 · 8 comments · Fixed by #7816
Closed
7 tasks done

Error when manually including vite/modulepreload-polyfill #4786

rynpsc opened this issue Aug 29, 2021 · 8 comments · Fixed by #7816
Labels
p4-important Violate documented behavior or significantly improves performance (priority)

Comments

@rynpsc
Copy link

rynpsc commented Aug 29, 2021

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

@yoshiotobe
Copy link

I've got the exactly same error when importing vite/modulepreload-polyfill with backend integration.

@andrefelipe
Copy link

Same here, any solutions appreciated

@jitingcn
Copy link

jitingcn commented Oct 9, 2021

if (import.meta.env.MODE !== 'development') {
  import('vite/modulepreload-polyfill')
}

Here is my workaround.

@kaibyao
Copy link

kaibyao commented 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.

@gnuletik
Copy link

gnuletik commented Dec 14, 2021

If you have eslint rule import/first and use typescript, here's how to ignore the workaround errors :

/* eslint-disable import/first */
if (import.meta.env.MODE !== 'development') {
  // @ts-expect-error
  import('vite/modulepreload-polyfill')
}

// other imports...

@jeromebon
Copy link

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.

@sadeghbarati
Copy link

@patak-dev

also if using @vitejs/plugin-legacy and use dynamic import instead of static import for this issue

it's will generate two chunk, modulepreload-polyfill-*******.js and modulepreload-polyfill-legacy.********.js


modulepreload-polyfill-legacy.********.js :

System.register([],(function(){"use strict";return{execute:function(){}}}));

modulepreload-polyfill-*******.js:

!function(){const e=document.createElement("link").relList;if(!(e&&e.supports&&e.supports("modulepreload"))){for(const e of document.querySelectorAll('link[rel="modulepreload"]'))r(e);new MutationObserver((e=>{for(const o of e)if("childList"===o.type)for(const e of o.addedNodes)"LINK"===e.tagName&&"modulepreload"===e.rel&&r(e)})).observe(document,{childList:!0,subtree:!0})}function r(e){if(e.ep)return;e.ep=!0;const r=function(e){const r={};return e.integrity&&(r.integrity=e.integrity),e.referrerpolicy&&(r.referrerPolicy=e.referrerpolicy),"use-credentials"===e.crossorigin?r.credentials="include":"anonymous"===e.crossorigin?r.credentials="omit":r.credentials="same-origin",r}(e);fetch(e.href,r)}}();

@gone
Copy link

gone commented Feb 17, 2022

If you are running tsc against this, the import() call will raise the error error TS2468: Cannot find global value 'Promise'.

@sapphi-red sapphi-red added bug p4-important Violate documented behavior or significantly improves performance (priority) and removed pending triage labels Apr 20, 2022
@github-actions github-actions bot locked and limited conversation to collaborators May 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants