fix(v2): fix Webpack persistent caching (evict on swizzle/alias/config change) (#5047)

* webpack upgrade

* refactor docusaurus-utils hash fns

* Fix webpack cache eviction problems on config/aliases/swizzle changes

* Move/Rename InitPlugin type

* fix TS typos

* Add tests for webpack aliases

* fix windows tests
This commit is contained in:
Sébastien Lorber 2021-06-24 12:56:56 +02:00 committed by GitHub
parent 9916a0b4a4
commit 99270dbab2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 274 additions and 169 deletions

View file

@ -5,7 +5,28 @@
* LICENSE file in the root directory of this source tree.
*/
import {docuHash} from '../docuHash';
import {simpleHash, docuHash} from '../hashUtils';
describe('hashUtils', () => {
test('simpleHash', () => {
const asserts: Record<string, string> = {
'': 'd41',
'/foo-bar': '096',
'/foo/bar': '1df',
'/endi/lie': '9fa',
'/endi-lie': 'fd3',
'/yangshun/tay': '48d',
'/yangshun-tay': 'f3b',
'/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar':
'd46',
'/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/test1-test2':
'787',
};
Object.keys(asserts).forEach((str) => {
expect(simpleHash(str, 3)).toBe(asserts[str]);
});
});
});
describe('docuHash', () => {
test('docuHash works', () => {

View file

@ -5,28 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/
import {simpleHash, isNameTooLong, shortName} from '../pathUtils';
import {isNameTooLong, shortName} from '../pathUtils';
describe('pathUtils', () => {
test('simpleHash', () => {
const asserts: Record<string, string> = {
'': 'd41',
'/foo-bar': '096',
'/foo/bar': '1df',
'/endi/lie': '9fa',
'/endi-lie': 'fd3',
'/yangshun/tay': '48d',
'/yangshun-tay': 'f3b',
'/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar':
'd46',
'/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/test1-test2':
'787',
};
Object.keys(asserts).forEach((file) => {
expect(simpleHash(file, 3)).toBe(asserts[file]);
});
});
test('isNameTooLong', () => {
const asserts: Record<string, boolean> = {
'': false,

View file

@ -5,10 +5,19 @@
* LICENSE file in the root directory of this source tree.
*/
import {createHash} from 'crypto';
import {kebabCase} from 'lodash';
import {shortName, isNameTooLong} from './pathUtils';
import {shortName, isNameTooLong, simpleHash} from './pathUtils';
export function md5Hash(str: string): string {
return createHash('md5').update(str).digest('hex');
}
export function simpleHash(str: string, length: number): string {
return md5Hash(str).substr(0, length);
}
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
/**
* Given an input string, convert to kebab-case and append a hash.
* Avoid str collision.

View file

@ -22,8 +22,7 @@ import {
import resolvePathnameUnsafe from 'resolve-pathname';
import {posixPath as posixPathImport} from './posixPath';
import {simpleHash} from './pathUtils';
import {docuHash} from './docuHash';
import {simpleHash, docuHash} from './hashUtils';
export const posixPath = posixPathImport;
@ -32,8 +31,7 @@ export * from './codeTranslationsUtils';
export * from './markdownParser';
export * from './markdownLinks';
export * from './escapePath';
export * from './docuHash';
export {simpleHash} from './pathUtils';
export {md5Hash, simpleHash, docuHash} from './hashUtils';
const fileHash = new Map();
export async function generate(

View file

@ -7,8 +7,6 @@
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
import {createHash} from 'crypto';
// MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes
const MAX_PATH_SEGMENT_CHARS = 255;
const MAX_PATH_SEGMENT_BYTES = 255;
@ -42,7 +40,3 @@ export const shortName = (str: string): string => {
)
.toString();
};
export function simpleHash(str: string, length: number): string {
return createHash('md5').update(str).digest('hex').substr(0, length);
}