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

fix(plugin-legacy): respect entryFileNames for polyfill chunks #8247

Merged
merged 1 commit into from May 20, 2022
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
14 changes: 9 additions & 5 deletions packages/plugin-legacy/src/index.ts
Expand Up @@ -131,11 +131,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
modernPolyfills
)
await buildPolyfillChunk(
'polyfills-modern',
modernPolyfills,
bundle,
facadeToModernPolyfillMap,
config.build,
'es',
opts,
options.externalSystemJS
)
return
Expand All @@ -160,13 +161,14 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
)

await buildPolyfillChunk(
'polyfills-legacy',
legacyPolyfills,
bundle,
facadeToLegacyPolyfillMap,
// force using terser for legacy polyfill minification, since esbuild
// isn't legacy-safe
config.build,
'iife',
opts,
options.externalSystemJS
)
}
Expand Down Expand Up @@ -549,11 +551,12 @@ export async function detectPolyfills(
}

async function buildPolyfillChunk(
name: string,
imports: Set<string>,
bundle: OutputBundle,
facadeToChunkMap: Map<string, string>,
buildOptions: BuildOptions,
format: 'iife' | 'es',
rollupOutputOptions: NormalizedOutputOptions,
externalSystemJS?: boolean
) {
let { minify, assetsDir } = buildOptions
Expand All @@ -571,10 +574,11 @@ async function buildPolyfillChunk(
assetsDir,
rollupOptions: {
input: {
[name]: polyfillId
polyfills: polyfillId
},
output: {
format: name.includes('legacy') ? 'iife' : 'es',
format,
entryFileNames: rollupOutputOptions.entryFileNames,
manualChunks: undefined
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/manifest.ts
Expand Up @@ -37,7 +37,8 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
)
if (format === 'system' && !chunk.name.includes('-legacy')) {
const ext = path.extname(name)
name = name.slice(0, -ext.length) + `-legacy` + ext
const endPos = ext.length !== 0 ? -ext.length : undefined
name = name.slice(0, endPos) + `-legacy` + ext
}
return name.replace(/\0/g, '')
} else {
Expand Down
6 changes: 6 additions & 0 deletions playground/legacy/__tests__/legacy.spec.ts
Expand Up @@ -71,6 +71,12 @@ test('should load dynamic import with css', async () => {
describe.runIf(isBuild)('build', () => {
test('should generate correct manifest', async () => {
const manifest = readManifest()
// legacy polyfill
expect(manifest['../../vite/legacy-polyfills-legacy']).toBeDefined()
expect(manifest['../../vite/legacy-polyfills-legacy'].src).toBe(
'../../vite/legacy-polyfills-legacy'
)
// modern polyfill
expect(manifest['../../vite/legacy-polyfills']).toBeDefined()
expect(manifest['../../vite/legacy-polyfills'].src).toBe(
'../../vite/legacy-polyfills'
Expand Down
2 changes: 1 addition & 1 deletion playground/legacy/vite.config-custom-filename.js
@@ -1,7 +1,7 @@
const legacy = require('@vitejs/plugin-legacy').default

module.exports = {
plugins: [legacy()],
plugins: [legacy({ modernPolyfills: true })],
build: {
manifest: true,
minify: false,
Expand Down
3 changes: 2 additions & 1 deletion playground/legacy/vite.config.js
Expand Up @@ -6,7 +6,8 @@ module.exports = {
base: './',
plugins: [
legacy({
targets: 'IE 11'
targets: 'IE 11',
modernPolyfills: true
})
],

Expand Down