From a118a1d98c63028ddc8b2b3389b8cfa58d771e76 Mon Sep 17 00:00:00 2001 From: IU Date: Mon, 21 Mar 2022 23:22:13 +0800 Subject: [PATCH] fix(plugin-legacy): always fallback legacy build when CSP (#6535) --- packages/plugin-legacy/README.md | 4 +++- packages/plugin-legacy/index.js | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/plugin-legacy/README.md b/packages/plugin-legacy/README.md index b5bf572e53fa2c..ec7e630c87755a 100644 --- a/packages/plugin-legacy/README.md +++ b/packages/plugin-legacy/README.md @@ -165,7 +165,9 @@ The legacy plugin requires inline scripts for [Safari 10.1 `nomodule` fix](https - `sha256-MS6/3FCg4WjP9gwgaBGwLpRCY6fZBgwmhVCdrPrNf3E=` - `sha256-tQjf8gvb2ROOMapIxFvFAYBeUJ0v1HCbOcSmDNXGtDo=` -- `sha256-T9h4ixy0FtNsCwAyTfBtIY6uV5ZhMeNQIlL42GAKEME=` +- `sha256-xYj09txJ9OsgySe5ommpqul6FiaJZRrwe3KTD7wbV6w=` +- `sha256-4m6wOIrq/wFDmi9Xh3mFM2mwI4ik9n3TMgHk6xDtLxk=` +- `sha256-uS7/g9fhQwNZS1f/MqYqqKv8y9hCu36IfX9XZB5L7YY=` These values (without the `sha256-` prefix) can also be retrieved via diff --git a/packages/plugin-legacy/index.js b/packages/plugin-legacy/index.js index b66c4714e19c09..626b48f85ea955 100644 --- a/packages/plugin-legacy/index.js +++ b/packages/plugin-legacy/index.js @@ -18,7 +18,11 @@ const safari10NoModuleFix = `!function(){var e=document,t=e.createElement("scrip const legacyPolyfillId = 'vite-legacy-polyfill' 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 detectDynamicImportVarName = '__vite_is_dynamic_import_support' +const detectDynamicImportVarInitCode = `var ${detectDynamicImportVarName}=false;` +const detectDynamicImportCode = `try{import("_").catch(()=>1);}catch(e){}window.${detectDynamicImportVarName}=true;` +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)}();` const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};` @@ -430,6 +434,18 @@ function viteLegacyPlugin(options = {}) { // 5. inject dynamic import fallback entry if (genDynamicFallback && legacyPolyfillFilename && legacyEntryFilename) { + tags.push({ + tag: 'script', + attrs: { type: 'module' }, + children: detectDynamicImportVarInitCode, + injectTo: 'head' + }) + tags.push({ + tag: 'script', + attrs: { type: 'module' }, + children: detectDynamicImportCode, + injectTo: 'head' + }) tags.push({ tag: 'script', attrs: { type: 'module' }, @@ -695,5 +711,7 @@ viteLegacyPlugin.default = viteLegacyPlugin viteLegacyPlugin.cspHashes = [ createHash('sha256').update(safari10NoModuleFix).digest('base64'), createHash('sha256').update(systemJSInlineCode).digest('base64'), + createHash('sha256').update(detectDynamicImportVarInitCode).digest('base64'), + createHash('sha256').update(detectDynamicImportCode).digest('base64'), createHash('sha256').update(dynamicFallbackInlineCode).digest('base64') ]