Skip to content

Commit 60b6f55

Browse files
authored
feat(plugin-legacy): add externalSystemJS option (#5024)
1 parent 9164da0 commit 60b6f55

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

packages/plugin-legacy/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ export default {
122122
}
123123
```
124124

125+
### `externalSystemJS`
126+
127+
- **Type:** `boolean`
128+
- **Default:** `false`
129+
130+
Defaults to `false`. Enabling this option will exclude `systemjs/dist/s.min.js` inside polyfills-legacy chunk.
131+
125132
## Dynamic Import
126133

127134
The legacy plugin offers a way to use native `import()` in the modern build while falling back to the legacy build in browsers with native ESM but without dynamic import support (e.g. Legacy Edge). This feature works by injecting a runtime check and loading the legacy bundle with SystemJs runtime if needed. There are the following drawbacks:

packages/plugin-legacy/index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export interface Options {
2222
* default: true
2323
*/
2424
renderLegacyChunks?: boolean
25+
/**
26+
* default: false
27+
*/
28+
externalSystemJS?: boolean
2529
}
2630

2731
declare function createPlugin(options?: Options): Plugin

packages/plugin-legacy/index.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ function viteLegacyPlugin(options = {}) {
124124
modernPolyfills,
125125
bundle,
126126
facadeToModernPolyfillMap,
127-
config.build
127+
config.build,
128+
options.externalSystemJS,
128129
)
129130
return
130131
}
@@ -154,7 +155,8 @@ function viteLegacyPlugin(options = {}) {
154155
facadeToLegacyPolyfillMap,
155156
// force using terser for legacy polyfill minification, since esbuild
156157
// isn't legacy-safe
157-
config.build
158+
config.build,
159+
options.externalSystemJS,
158160
)
159161
}
160162
}
@@ -533,7 +535,8 @@ async function buildPolyfillChunk(
533535
imports,
534536
bundle,
535537
facadeToChunkMap,
536-
buildOptions
538+
buildOptions,
539+
externalSystemJS
537540
) {
538541
let { minify, assetsDir } = buildOptions
539542
minify = minify ? 'terser' : false
@@ -542,7 +545,7 @@ async function buildPolyfillChunk(
542545
root: __dirname,
543546
configFile: false,
544547
logLevel: 'error',
545-
plugins: [polyfillsPlugin(imports)],
548+
plugins: [polyfillsPlugin(imports, externalSystemJS)],
546549
build: {
547550
write: false,
548551
target: false,
@@ -582,7 +585,7 @@ const polyfillId = 'vite/legacy-polyfills'
582585
* @param {Set<string>} imports
583586
* @return {import('rollup').Plugin}
584587
*/
585-
function polyfillsPlugin(imports) {
588+
function polyfillsPlugin(imports, externalSystemJS) {
586589
return {
587590
name: 'vite:legacy-polyfills',
588591
resolveId(id) {
@@ -594,7 +597,7 @@ function polyfillsPlugin(imports) {
594597
if (id === polyfillId) {
595598
return (
596599
[...imports].map((i) => `import "${i}";`).join('') +
597-
`import "systemjs/dist/s.min.js";`
600+
(externalSystemJS ? '' : `import "systemjs/dist/s.min.js";`)
598601
)
599602
}
600603
}

0 commit comments

Comments
 (0)