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:
Sébastien Lorber 2020-06-30 11:52:39 +02:00 committed by GitHub
parent 984e2d4598
commit cf97662eef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 161 additions and 149 deletions

View file

@ -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',

View file

@ -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);