Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin-legacy): add externalSystemJS option #5024

Merged
merged 1 commit into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/plugin-legacy/README.md
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
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
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