Skip to content

Commit

Permalink
fix: multiple entries with shared css and no JS (#13962)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Jul 28, 2023
1 parent 24c12fe commit 89a3db0
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,9 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
js = `import "${modulePreloadPolyfillId}";\n${js}`
}

return js
// Force rollup to keep this module from being shared between other entry points.
// If the resulting chunk is empty, it will be removed in generateBundle.
return { code: js, moduleSideEffects: 'no-treeshake' }
}
},

Expand Down
8 changes: 8 additions & 0 deletions playground/css-codesplit/__tests__/css-codesplit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe.runIf(isBuild)('build', () => {
expect(findAssetFile(/async.*\.js$/)).toBe('')
})

test('should remove empty chunk, HTML without JS', async () => {
const sharedCSSWithJSChunk = findAssetFile('shared-css-with-js.*.js$')
expect(sharedCSSWithJSChunk).toMatch(`/* empty css`)
// there are functions and modules in the src code that should be tree-shaken
expect(sharedCSSWithJSChunk).not.toMatch('function')
expect(sharedCSSWithJSChunk).not.toMatch(/import(?!".\/modulepreload)/)
})

test('should generate correct manifest', async () => {
const manifest = readManifest()
expect(manifest['index.html'].css.length).toBe(2)
Expand Down
4 changes: 4 additions & 0 deletions playground/css-codesplit/shared-css-empty-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function shouldBeTreeshaken_1() {
// This function should be treeshaken, even if { moduleSideEffects: 'no-treeshake' }
// was used in the JS corresponding to the HTML entrypoint.
}
4 changes: 4 additions & 0 deletions playground/css-codesplit/shared-css-empty-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function shouldBeTreeshaken_2() {
// This function should be treeshaken, even if { moduleSideEffects: 'no-treeshake' }
// was used in the JS corresponding to the HTML entrypoint.
}
10 changes: 10 additions & 0 deletions playground/css-codesplit/shared-css-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import shouldTreeshake from './shared-css-empty-2.js'
document.querySelector('#app').innerHTML = `
<div>
<h1>Shared CSS, with JS</h1>
</div>
`
function shouldBeTreeshaken_0() {
// This function should be treeshaken, even if { moduleSideEffects: 'no-treeshake' }
// was used in the JS corresponding to the HTML entrypoint.
}
4 changes: 4 additions & 0 deletions playground/css-codesplit/shared-css-no-js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<link rel="stylesheet" type="text/css" href="./shared-css-theme.css" />
<body>
<h1>Share CSS, no JS</h1>
</body>
3 changes: 3 additions & 0 deletions playground/css-codesplit/shared-css-theme.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: red;
}
6 changes: 6 additions & 0 deletions playground/css-codesplit/shared-css-with-js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<link rel="stylesheet" type="text/css" href="./shared-css-theme.css" />
<script type="module" src="./shared-css-main.js"></script>
<script type="module" src="./shared-css-empty-1.js"></script>
<body>
<h1>Replaced by shared-css-main.js</h1>
</body>
2 changes: 2 additions & 0 deletions playground/css-codesplit/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default defineConfig({
main: resolve(__dirname, './index.html'),
other: resolve(__dirname, './other.js'),
style2: resolve(__dirname, './style2.js'),
'shared-css-with-js': resolve(__dirname, 'shared-css-with-js.html'),
'shared-css-no-js': resolve(__dirname, 'shared-css-no-js.html'),
},
output: {
manualChunks(id) {
Expand Down

0 comments on commit 89a3db0

Please sign in to comment.