Skip to content

Commit

Permalink
feat(plugin-legacy): add externalSystemJS option (vitejs#5024)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrekiSH authored and aleclarson committed Nov 8, 2021
1 parent 2862cde commit e855e19
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
7 changes: 7 additions & 0 deletions packages/plugin-legacy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ export default {
}
```

### `externalSystemJS`

- **Type:** `boolean`
- **Default:** `false`

Defaults to `false`. Enabling this option will exclude `systemjs/dist/s.min.js` inside polyfills-legacy chunk.

## Dynamic Import

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:
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-legacy/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface Options {
* default: true
*/
renderLegacyChunks?: boolean
/**
* default: false
*/
externalSystemJS?: boolean
}

declare function createPlugin(options?: Options): Plugin
Expand Down
15 changes: 9 additions & 6 deletions packages/plugin-legacy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ function viteLegacyPlugin(options = {}) {
modernPolyfills,
bundle,
facadeToModernPolyfillMap,
config.build
config.build,
options.externalSystemJS,
)
return
}
Expand Down Expand Up @@ -154,7 +155,8 @@ function viteLegacyPlugin(options = {}) {
facadeToLegacyPolyfillMap,
// force using terser for legacy polyfill minification, since esbuild
// isn't legacy-safe
config.build
config.build,
options.externalSystemJS,
)
}
}
Expand Down Expand Up @@ -533,7 +535,8 @@ async function buildPolyfillChunk(
imports,
bundle,
facadeToChunkMap,
buildOptions
buildOptions,
externalSystemJS
) {
let { minify, assetsDir } = buildOptions
minify = minify ? 'terser' : false
Expand All @@ -542,7 +545,7 @@ async function buildPolyfillChunk(
root: __dirname,
configFile: false,
logLevel: 'error',
plugins: [polyfillsPlugin(imports)],
plugins: [polyfillsPlugin(imports, externalSystemJS)],
build: {
write: false,
target: false,
Expand Down Expand Up @@ -582,7 +585,7 @@ const polyfillId = 'vite/legacy-polyfills'
* @param {Set<string>} imports
* @return {import('rollup').Plugin}
*/
function polyfillsPlugin(imports) {
function polyfillsPlugin(imports, externalSystemJS) {
return {
name: 'vite:legacy-polyfills',
resolveId(id) {
Expand All @@ -594,7 +597,7 @@ function polyfillsPlugin(imports) {
if (id === polyfillId) {
return (
[...imports].map((i) => `import "${i}";`).join('') +
`import "systemjs/dist/s.min.js";`
(externalSystemJS ? '' : `import "systemjs/dist/s.min.js";`)
)
}
}
Expand Down

0 comments on commit e855e19

Please sign in to comment.