Skip to content

Commit

Permalink
fix(legacy): code was running twice on Chrome 63
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Feb 2, 2023
1 parent b451858 commit 5ac8417
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import type { Options } from './types'
import {
detectModernBrowserCode,
dynamicFallbackInlineCode,
forceDynamicImportUsage,
legacyEntryId,
legacyPolyfillId,
modernChunkLegacyGuard,
safari10NoModuleFix,
systemJSInlineCode,
} from './snippets'
Expand Down Expand Up @@ -362,7 +362,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
const ms = new MagicString(raw)

if (genLegacy && chunk.isEntry) {
ms.prepend(forceDynamicImportUsage)
// append this code to avoid modern chunks running on legacy targeted browsers
ms.prepend(modernChunkLegacyGuard)
}

if (raw.includes(legacyEnvVarMarker)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-legacy/src/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ const detectModernBrowserVarName = '__vite_is_modern_browser'
export const detectModernBrowserDetector =
'import.meta.url;import("_").catch(()=>1);'
export const detectModernBrowserCode = `${detectModernBrowserDetector}window.${detectModernBrowserVarName}=true;`
export 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)}();`
export 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 and the same error below should be ignored");var e=document.getElementById("${legacyPolyfillId}"),n=document.createElement("script");n.src=e.src,n.onload=function(){${systemJSInlineCode}},document.body.appendChild(n)}();`

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

0 comments on commit 5ac8417

Please sign in to comment.