Skip to content

Commit 2defe68

Browse files
committed
fix: split delayed assets to separate chunks
1 parent 1809a47 commit 2defe68

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

packages/astro/src/core/build/vite-plugin-css.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,21 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
3232
function createNameForParentPages(id: string, ctx: { getModuleInfo: GetModuleInfo }): string {
3333
const parents = Array.from(getTopLevelPages(id, ctx));
3434
const firstParentId = parents[0]?.[0].id;
35-
const firstParentName = firstParentId ? npath.parse(firstParentId).name : 'index';
35+
const proposedName = createNameHash(
36+
firstParentId,
37+
parents.map(([page]) => page.id)
38+
);
39+
return proposedName;
40+
}
3641

42+
function createNameHash(baseId: string, hashIds: string[]): string {
43+
const baseName = baseId ? npath.parse(baseId).name : 'index';
3744
const hash = crypto.createHash('sha256');
38-
for (const [page] of parents) {
39-
hash.update(page.id, 'utf-8');
45+
for (const id of hashIds) {
46+
hash.update(id, 'utf-8');
4047
}
4148
const h = hash.digest('hex').slice(0, 8);
42-
const proposedName = firstParentName + '.' + h;
49+
const proposedName = baseName + '.' + h;
4350
return proposedName;
4451
}
4552

@@ -74,6 +81,15 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
7481
// For CSS, create a hash of all of the pages that use it.
7582
// This causes CSS to be built into shared chunks when used by multiple pages.
7683
if (isCSSRequest(id)) {
84+
for (const [pageInfo] of walkParentInfos(id, {
85+
getModuleInfo: args[0].getModuleInfo,
86+
})) {
87+
if (pageInfo.id.endsWith(DELAYED_ASSET_FLAG)) {
88+
// Split delayed assets to separate modules
89+
// so they can be injected where needed
90+
return createNameHash(id, [id]);
91+
}
92+
}
7793
return createNameForParentPages(id, args[0]);
7894
}
7995
};

0 commit comments

Comments
 (0)