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: multiple entries with shared css and no JS #13962

Merged
merged 6 commits into from
Jul 28, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' }
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
}
},

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