Skip to content

Commit a118a1d

Browse files
authored
fix(plugin-legacy): always fallback legacy build when CSP (#6535)
1 parent c43467a commit a118a1d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/plugin-legacy/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ The legacy plugin requires inline scripts for [Safari 10.1 `nomodule` fix](https
165165

166166
- `sha256-MS6/3FCg4WjP9gwgaBGwLpRCY6fZBgwmhVCdrPrNf3E=`
167167
- `sha256-tQjf8gvb2ROOMapIxFvFAYBeUJ0v1HCbOcSmDNXGtDo=`
168-
- `sha256-T9h4ixy0FtNsCwAyTfBtIY6uV5ZhMeNQIlL42GAKEME=`
168+
- `sha256-xYj09txJ9OsgySe5ommpqul6FiaJZRrwe3KTD7wbV6w=`
169+
- `sha256-4m6wOIrq/wFDmi9Xh3mFM2mwI4ik9n3TMgHk6xDtLxk=`
170+
- `sha256-uS7/g9fhQwNZS1f/MqYqqKv8y9hCu36IfX9XZB5L7YY=`
169171

170172
These values (without the `sha256-` prefix) can also be retrieved via
171173

packages/plugin-legacy/index.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ const safari10NoModuleFix = `!function(){var e=document,t=e.createElement("scrip
1818
const legacyPolyfillId = 'vite-legacy-polyfill'
1919
const legacyEntryId = 'vite-legacy-entry'
2020
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`
21-
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)}}();`
21+
22+
const detectDynamicImportVarName = '__vite_is_dynamic_import_support'
23+
const detectDynamicImportVarInitCode = `var ${detectDynamicImportVarName}=false;`
24+
const detectDynamicImportCode = `try{import("_").catch(()=>1);}catch(e){}window.${detectDynamicImportVarName}=true;`
25+
const dynamicFallbackInlineCode = `!function(){if(window.${detectDynamicImportVarName})return;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)}();`
2226

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

@@ -430,6 +434,18 @@ function viteLegacyPlugin(options = {}) {
430434

431435
// 5. inject dynamic import fallback entry
432436
if (genDynamicFallback && legacyPolyfillFilename && legacyEntryFilename) {
437+
tags.push({
438+
tag: 'script',
439+
attrs: { type: 'module' },
440+
children: detectDynamicImportVarInitCode,
441+
injectTo: 'head'
442+
})
443+
tags.push({
444+
tag: 'script',
445+
attrs: { type: 'module' },
446+
children: detectDynamicImportCode,
447+
injectTo: 'head'
448+
})
433449
tags.push({
434450
tag: 'script',
435451
attrs: { type: 'module' },
@@ -695,5 +711,7 @@ viteLegacyPlugin.default = viteLegacyPlugin
695711
viteLegacyPlugin.cspHashes = [
696712
createHash('sha256').update(safari10NoModuleFix).digest('base64'),
697713
createHash('sha256').update(systemJSInlineCode).digest('base64'),
714+
createHash('sha256').update(detectDynamicImportVarInitCode).digest('base64'),
715+
createHash('sha256').update(detectDynamicImportCode).digest('base64'),
698716
createHash('sha256').update(dynamicFallbackInlineCode).digest('base64')
699717
]

0 commit comments

Comments
 (0)