mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-06 04:42:40 +02:00
fix(v2): refactor routes.ts + add route hash for chunkNames key (#3001)
* add simpleHash util * refactor/split the routes generation logic + add route hash to avoid chunk conflicts * minor fixes + fix tests * fix comment typo
This commit is contained in:
parent
984e2d4598
commit
cf97662eef
6 changed files with 161 additions and 149 deletions
|
@ -80,6 +80,10 @@ export function encodePath(userpath: string): string {
|
|||
.join('/');
|
||||
}
|
||||
|
||||
export function simpleHash(str: string, length: number): string {
|
||||
return createHash('md5').update(str).digest('hex').substr(0, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an input string, convert to kebab-case and append a hash.
|
||||
* Avoid str collision.
|
||||
|
@ -88,7 +92,7 @@ export function docuHash(str: string): string {
|
|||
if (str === '/') {
|
||||
return 'index';
|
||||
}
|
||||
const shortHash = createHash('md5').update(str).digest('hex').substr(0, 3);
|
||||
const shortHash = simpleHash(str, 3);
|
||||
return `${kebabCase(str)}-${shortHash}`;
|
||||
}
|
||||
|
||||
|
@ -139,17 +143,11 @@ export function genChunkName(
|
|||
let chunkName: string | undefined = chunkNameCache.get(modulePath);
|
||||
if (!chunkName) {
|
||||
if (shortId) {
|
||||
chunkName = createHash('md5')
|
||||
.update(modulePath)
|
||||
.digest('hex')
|
||||
.substr(0, 8);
|
||||
chunkName = simpleHash(modulePath, 8);
|
||||
} else {
|
||||
let str = modulePath;
|
||||
if (preferredName) {
|
||||
const shortHash = createHash('md5')
|
||||
.update(modulePath)
|
||||
.digest('hex')
|
||||
.substr(0, 3);
|
||||
const shortHash = simpleHash(modulePath, 3);
|
||||
str = `${preferredName}${shortHash}`;
|
||||
}
|
||||
const name = str === '/' ? 'index' : docuHash(str);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue