mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 08:07:26 +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
|
@ -8,6 +8,7 @@
|
|||
import path from 'path';
|
||||
import {
|
||||
fileToPath,
|
||||
simpleHash,
|
||||
docuHash,
|
||||
genComponentName,
|
||||
genChunkName,
|
||||
|
@ -71,6 +72,21 @@ describe('load utils', () => {
|
|||
});
|
||||
});
|
||||
|
||||
test('simpleHash', () => {
|
||||
const asserts = {
|
||||
'': 'd41',
|
||||
'/foo-bar': '096',
|
||||
'/foo/bar': '1df',
|
||||
'/endi/lie': '9fa',
|
||||
'/endi-lie': 'fd3',
|
||||
'/yangshun/tay': '48d',
|
||||
'/yangshun-tay': 'f3b',
|
||||
};
|
||||
Object.keys(asserts).forEach((file) => {
|
||||
expect(simpleHash(file, 3)).toBe(asserts[file]);
|
||||
});
|
||||
});
|
||||
|
||||
test('docuHash', () => {
|
||||
const asserts = {
|
||||
'': '-d41',
|
||||
|
|
|
@ -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