Skip to content

Commit

Permalink
Shorter CSS naming algorithm (#5036)
Browse files Browse the repository at this point in the history
* Shorter CSS naming algorithm

* Adding a changeset

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

Co-authored-by: Bjorn Lu <[email protected]>

Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
matthewp and bluwy authored Oct 10, 2022
1 parent d7bfb14 commit 38fdb4c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
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

0 comments on commit 38fdb4c

Please sign in to comment.