-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix: restore dynamic-import-polyfill #3434
fix: restore dynamic-import-polyfill #3434
Conversation
self[importFunctionName] = new Function('u', `return import(u)`) | ||
} catch (error) { | ||
const baseURL = new URL(modulePath, location) | ||
const cleanup = (script: any) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Is script
an HTMLScriptElement
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the exact code from the polyfill from google that was removed, so I would prefer to avoid touching it. If we want to improve it, we can do so in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTMLScriptElement
would be just only a compile time code addition
Would not touch runtime code 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's discuss it in another PR 👍🏼
// if dynamic import polyfill is used, rewrite the import to | ||
// use the polyfilled function. | ||
if (isPolyfillEnabled) { | ||
s.overwrite(dynamicIndex, dynamicIndex + 6, `__import__`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: What is 6
? It's a magic number in this context 🤔
Should we extract it into a variable/constant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 is the number of letters in import
, this is the same code that was removed before so it is ok to add it back as it was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so 'import'.length
would theoretically better here, but yeah, feel free to add it in a new PR or we could use non-squash-merge specially for this PR and use two commits 🤔
For readability and understandment, I would much prefer to use 'import'.length
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the context is enough here, if we add something in another PR maybe it could be a hint in the comment above like 'import'.length === 6
. I think this kind of patterns is not uncommon in the Vite code base though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly I don't think it's a magic number, as it's quite clear to see it's the length of import
and giving the previous comment saying "rewrite the import". 'import'.length
would do but it's runtime, which to me, is not worth to bring the extra memory and CPU cost for the tiny DX that is not commonly touched anyway. I think it's ok to leave it as-is
Description
This PR restores the dynamic import polyfill plugin that was removed in #2976 with
build.polyfillDynamicImport
disabled by default.@vitejs/legacy-plugin
will use this plugin settingbuild.polyfillDynamicImport
totrue
.There are two changes compared to the old plugin code: https://github.com/vitejs/vite/blob/02ba4ba32cd40f1cc3943781022c05f9df3b57e6/packages/vite/src/node/plugins/dynamicImportPolyfill.ts
vite/dynamic-import-polyfill
but the polyfill feature is disabled.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).