Skip to content

Commit d671811

Browse files
authored
fix(plugin-legacy): prevent failed to load module (#8285)
1 parent d7cba46 commit d671811

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

packages/plugin-legacy/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,12 @@ The legacy plugin requires inline scripts for [Safari 10.1 `nomodule` fix](https
147147

148148
- `sha256-MS6/3FCg4WjP9gwgaBGwLpRCY6fZBgwmhVCdrPrNf3E=`
149149
- `sha256-tQjf8gvb2ROOMapIxFvFAYBeUJ0v1HCbOcSmDNXGtDo=`
150-
- `sha256-4m6wOIrq/wFDmi9Xh3mFM2mwI4ik9n3TMgHk6xDtLxk=`
151-
- `sha256-uS7/g9fhQwNZS1f/MqYqqKv8y9hCu36IfX9XZB5L7YY=`
150+
- `sha256-BoFUHKsYhJ9tbsHugtNQCmnkBbZ11pcW6kZguu+T+EU=`
151+
- `sha256-A18HC3jLpyEc9B8oyxq/NBFCyFBJFSsRLt0gmT9kft8=`
152+
153+
<!--
154+
Run `node --input-type=module -e "import {cspHashes} from '@vitejs/plugin-legacy'; console.log(cspHashes.map(h => 'sha256-'+h))"` to retrieve the value.
155+
-->
152156

153157
These values (without the `sha256-` prefix) can also be retrieved via
154158

packages/plugin-legacy/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const legacyEntryId = 'vite-legacy-entry'
4040
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`
4141

4242
const detectModernBrowserVarName = '__vite_is_modern_browser'
43-
const detectModernBrowserCode = `try{import(new URL(import.meta.url).href).catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`
43+
const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`
4444
const dynamicFallbackInlineCode = `!function(){if(window.${detectModernBrowserVarName})return;console.warn("vite: loading legacy build because dynamic import or import.meta.url 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)}();`
4545

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

0 commit comments

Comments
 (0)