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

Shorter CSS naming algorithm #5036

Merged
merged 3 commits into from
Oct 10, 2022
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
5 changes: 5 additions & 0 deletions .changeset/six-oranges-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

New algorithm for shorter CSS bundle names
34 changes: 5 additions & 29 deletions packages/astro/src/core/build/vite-plugin-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import esbuild from 'esbuild';
import npath from 'path';
import { Plugin as VitePlugin, ResolvedConfig } from 'vite';
import { isCSSRequest } from '../render/util.js';
import { relativeToSrcDir } from '../util.js';
import { getTopLevelPages, moduleIsTopLevelPage, walkParentInfos } from './graph.js';
import {
eachPageData,
Expand All @@ -23,47 +22,24 @@ interface PluginOptions {
target: 'client' | 'server';
}

// Arbitrary magic number, can change.
const MAX_NAME_LENGTH = 70;

export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
const { internals, buildOptions } = options;
const { settings } = buildOptions;

let resolvedConfig: ResolvedConfig;

// Turn a page location into a name to be used for the CSS file.
function nameifyPage(id: string) {
let rel = relativeToSrcDir(settings.config, id);
// Remove pages, ex. blog/posts/something.astro
if (rel.startsWith('pages/')) {
rel = rel.slice(6);
}
// Remove extension, ex. blog/posts/something
const ext = npath.extname(rel);
const noext = rel.slice(0, rel.length - ext.length);
// Replace slashes with dashes, ex. blog-posts-something
const named = noext.replace(/\//g, '-');
return named;
}

function createNameForParentPages(id: string, ctx: { getModuleInfo: GetModuleInfo }): string {
const parents = Array.from(getTopLevelPages(id, ctx));
const proposedName = parents
.map(([page]) => nameifyPage(page.id))
.sort()
.join('-');

// We don't want absurdedly long chunk names, so if this is too long create a hashed version instead.
if (proposedName.length <= MAX_NAME_LENGTH) {
return proposedName;
}
const firstParentId = parents[0]?.[0].id
const firstParentName = firstParentId ? npath.parse(firstParentId).name : 'index'

const hash = crypto.createHash('sha256');
for (const [page] of parents) {
hash.update(page.id, 'utf-8');
}
return hash.digest('hex').slice(0, 8);
const h = hash.digest('hex').slice(0, 8);
const proposedName = firstParentName + '.' + h;
return proposedName;
}

function* getParentClientOnlys(
Expand Down