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

refactor(legacy): remove unneeded dynamic import var init code #7759

Merged

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Apr 16, 2022

Description

Currently plugin-legacy outputs html like below. (extracted the relevant part)

    <script type="module">var __vite_is_dynamic_import_support=false;</script>
    <script type="module">try{import("_").catch(()=>1);}catch(e){}window.__vite_is_dynamic_import_support=true;</script>
    <script type="module">!function(){if(window.__vite_is_dynamic_import_support)return;console.warn("vite: loading legacy build because dynamic import is unsupported, syntax error above should be ignored");var e=document.getElementById("vite-legacy-polyfill"),n=document.createElement("script");n.src=e.src,n.onload=function(){System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))},document.body.appendChild(n)}();</script>

The first script tag is unused.
The reason is that a top level variable inside type="module" is not exposed to global.
It could be fixed by removing type="module" or changing to window.__vite_is_dynamic_import_support = false.
But since it is a falsy value, the declaration is not needed.
So I dropped this script tag.

This will simply the code a bit and also reduce file size of html (and csp header) a bit.

I tested this with https://github.com/yoyo930021/vite-legacy-csp-bug in Chrome 102, Chrome 62, Edge 18, Firefox 66, Safari 10.1.

refs #6535

Additional context

https://discord.com/channels/804011606160703521/804439875226173480/964612065337696307


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added plugin: legacy p1-chore Doesn't change code behavior (priority) labels Apr 16, 2022
@patak-dev patak-dev changed the title refactor(legacy): remove dynamic import var init code refactor(legacy): remove unneeded dynamic import var init code Apr 16, 2022
@patak-dev patak-dev merged commit 12a4e7d into vitejs:main Apr 16, 2022
@sapphi-red sapphi-red deleted the refactor/legacy-dynamic-import-support branch April 18, 2022 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p1-chore Doesn't change code behavior (priority) plugin: legacy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants