Skip to content

Commit

Permalink
fix: don't emit an empty bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Sep 26, 2023
1 parent 63cc250 commit 83b5a85
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 16 deletions.
34 changes: 18 additions & 16 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,24 +268,26 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
)
}

isDebug &&
console.log(
`[@vitejs/plugin-legacy] legacy polyfills:`,
if (legacyPolyfills.size || !options.externalSystemJS) {
isDebug &&
console.log(
`[@vitejs/plugin-legacy] legacy polyfills:`,
legacyPolyfills,
)

await buildPolyfillChunk(
config.mode,
legacyPolyfills,
bundle,
facadeToLegacyPolyfillMap,
// force using terser for legacy polyfill minification, since esbuild
// isn't legacy-safe
config.build,
'iife',
opts,
options.externalSystemJS,
)

await buildPolyfillChunk(
config.mode,
legacyPolyfills,
bundle,
facadeToLegacyPolyfillMap,
// force using terser for legacy polyfill minification, since esbuild
// isn't legacy-safe
config.build,
'iife',
opts,
options.externalSystemJS,
)
}
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test } from 'vitest'
import { isBuild, page, untilUpdated, viteTestUrl } from '~utils'

test.runIf(isBuild)('includes only a single script tag', async () => {
await page.goto(viteTestUrl + '/no-polyfills-no-systemjs.html')

await untilUpdated(
() => page.getAttribute('#vite-legacy-entry', 'data-src'),
/.\/assets\/index-legacy-(.+)\.js/,
true,
)

expect(await page.locator('script').count()).toBe(1)
expect(await page.locator('#vite-legacy-polyfill').count()).toBe(0)
expect(await page.locator('#vite-legacy-entry').count()).toBe(1)
})
3 changes: 3 additions & 0 deletions playground/legacy/no-polyfills-no-systemjs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<meta charset="UTF-8" />
<main></main>
<script type="module" src="./no-polyfills-no-systemjs.js"></script>
1 change: 1 addition & 0 deletions playground/legacy/no-polyfills-no-systemjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
document.querySelector('main').innerHTML = '👋'
1 change: 1 addition & 0 deletions playground/legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build:custom-filename": "vite --config ./vite.config-custom-filename.js build --debug legacy",
"build:multiple-output": "vite --config ./vite.config-multiple-output.js build",
"build:no-polyfills": "vite --config ./vite.config-no-polyfills.js build",
"build:no-polyfills-no-systemjs": "vite --config ./vite.config-no-polyfills-no-systemjs.js build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
Expand Down
30 changes: 30 additions & 0 deletions playground/legacy/vite.config-no-polyfills-no-systemjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import path from 'node:path'
import legacy from '@vitejs/plugin-legacy'
import { defineConfig } from 'vite'

export default defineConfig({
base: './',
plugins: [
legacy({
renderModernChunks: false,
polyfills: false,
externalSystemJS: true,
}),
{
name: 'remove crossorigin attribute',
transformIndexHtml: (html) => html.replaceAll('crossorigin', ''),
enforce: 'post',
},
],

build: {
rollupOptions: {
input: {
index: path.resolve(__dirname, 'no-polyfills-no-systemjs.html'),
},
},
},
testConfig: {
baseRoute: '/no-polyfills-no-systemjs/',
},
})

0 comments on commit 83b5a85

Please sign in to comment.