Skip to content

Commit

Permalink
fix(plugin-legacy): avoid executing blank dynamic import
Browse files Browse the repository at this point in the history
Importing `data:` uri doesn't pass strict CSP
Export `__vite_legacy_guard` to keep rollup from removing it

Closes #4568
  • Loading branch information
nulladdict committed Aug 28, 2021
1 parent da0abc5 commit 8f1f822
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/plugin-legacy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const legacyEntryId = 'vite-legacy-entry'
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`
const dynamicFallbackInlineCode = `!function(){try{new Function("m","return import(m)")}catch(o){console.warn("vite: loading legacy build because dynamic import is unsupported, syntax error above should be ignored");var e=document.getElementById("${legacyPolyfillId}"),n=document.createElement("script");n.src=e.src,n.onload=function(){${systemJSInlineCode}},document.body.appendChild(n)}}();`

const blankDynamicImport = `import('data:text/javascript;base64,Cg==');`
const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`

const legacyEnvVarMarker = `__VITE_IS_LEGACY__`

Expand Down Expand Up @@ -238,7 +238,7 @@ function viteLegacyPlugin(options = {}) {
const ms = new MagicString(raw)

if (genDynamicFallback && chunk.isEntry) {
ms.prepend(blankDynamicImport)
ms.prepend(forceDynamicImportUsage)
}

if (raw.includes(legacyEnvVarMarker)) {
Expand Down

0 comments on commit 8f1f822

Please sign in to comment.