Skip to content

Commit

Permalink
Prettify base name
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Oct 6, 2023
1 parent 7f17321 commit ee00eec
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/astro/src/core/build/css-asset-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function shortHashedName(id: string, ctx: { getModuleInfo: GetModuleInfo
}

export function createNameHash(baseId: string | undefined, hashIds: string[]): string {
const baseName = baseId ? npath.parse(baseId).name : 'index';
const baseName = baseId ? prettifyBaseName(npath.parse(baseId).name) : 'index';
const hash = crypto.createHash('sha256');
for (const id of hashIds) {
hash.update(id, 'utf-8');
Expand Down Expand Up @@ -54,7 +54,7 @@ export function createSlugger(settings: AstroSettings) {
break;
}

const name = npath.parse(npath.basename(dir)).name;
const name = prettifyBaseName(npath.parse(npath.basename(dir)).name);
key = key.length ? name + sep + key : name;
dir = npath.dirname(dir);
i++;
Expand Down Expand Up @@ -102,3 +102,15 @@ function getFirstParentId(parents: [ModuleInfo, number, number][]) {
// parents, this will return undefined.
return parents[0]?.[0].id;
}

const charsToReplaceRe = /[.\[\]]/g;
const underscoresRe = /_+/g;
/**
* Prettify base names so they're easier to read:
* - index -> index
* - [slug] -> _slug_
* - [...spread] -> _spread_
*/
function prettifyBaseName(str: string) {
return str.replace(charsToReplaceRe, '_').replace(underscoresRe, '_');
}

0 comments on commit ee00eec

Please sign in to comment.