Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"oxlint": "^1.94.0",
"picocolors": "^1.1.1",
"playwright-chromium": "^1.65.1",
"rolldown": "~1.3.2",
"rolldown": "~1.3.4",
"rollup": "^4.59.0",
"simple-git-hooks": "^2.14.0",
"tsrs": "7.1.0-beta.tsrs4",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"lightningcss": "^1.33.0",
"picomatch": "^4.0.7",
"postcss": "^8.5.33",
"rolldown": "~1.3.2",
"rolldown": "~1.3.4",
"tinyglobby": "^0.2.17"
},
"devDependencies": {
Expand Down
67 changes: 67 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,73 @@ describe('build', () => {
}
})

describe('shared CSS and inline common chunks', () => {
async function buildProject(emitAssets: boolean) {
const root = await fsp.realpath(
await fsp.mkdtemp(join(tmpdir(), 'vite-css-chunk-size-')),
)
onTestFinished(() => fsp.rm(root, { recursive: true, force: true }))

const files = {
'a.js': "import './shared.css'; import './a.css'; console.log('a')",
'b.js': "import './shared.css'; import './b.css'; console.log('b')",
'a.css': '.local-a { color: red; }\n',
'b.css': '.local-b { color: blue; }\n',
// 133 UTF-16 code units and 333 UTF-8 bytes straddle the limit.
'shared.css': `.shared::before { content: "${'界'.repeat(100)}"; }\n`,
}
await Promise.all(
Object.entries(files).map(([name, source]) =>
fsp.writeFile(join(root, name), source),
),
)

return (await build({
root,
configFile: false,
logLevel: 'silent',
build: {
cssCodeSplit: true,
emitAssets,
minify: false,
write: false,
rolldownOptions: {
input: { a: join(root, 'a.js'), b: join(root, 'b.js') },
output: {
codeSplitting: {
experimentalInlineCommonChunks: { maxSize: 256 },
},
},
},
},
})) as RolldownOutput
}

test('keeps a large shared stylesheet in a single asset', async () => {
const { output } = await buildProject(true)
const sharedAssets = output.filter(
(item): item is OutputAsset =>
item.type === 'asset' &&
item.fileName.endsWith('.css') &&
String(item.source).includes('.shared::before'),
)
expect(sharedAssets).toHaveLength(1)
expect(sharedAssets[0].source).not.toContain('.local-')
})

test('places the shared stylesheet module in both entry chunks when emitAssets is false', async () => {
const { output } = await buildProject(false)
expect(
output.filter(
(item) =>
item.type === 'chunk' &&
item.isEntry &&
item.moduleIds.some((id) => basename(id) === 'shared.css'),
),
).toHaveLength(2)
})
})

test('file hash should change when pure css chunk changes', async () => {
const buildProject = async (cssColor: string) => {
return (await build({
Expand Down
8 changes: 8 additions & 0 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,13 +711,21 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
code = ''
}

const { cssCodeSplit, emitAssets } = this.environment.config.build
return {
code,
map: { mappings: '' },
// avoid the css module from being tree-shaken so that we can retrieve
// it in renderChunk()
moduleSideEffects: modulesCode || inlined ? false : 'no-treeshake',
moduleType: 'js',
...(cssCodeSplit &&
emitAssets &&
!inlined &&
!transformOnlyRE.test(id) && {
declaredModuleSize:
Buffer.byteLength(code) + Buffer.byteLength(css),
}),
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"convert-source-map": "^2.0.0",
"css-color-names": "^1.0.1",
"kill-port": "^1.6.1",
"rolldown": "~1.3.2",
"rolldown": "~1.3.4",
"tinyexec": "^1.3.1"
}
}
Loading
Loading