refactor(core): refactor routes generation logic (#7054)

* refactor(core): refactor routes generation logic

* fixes
This commit is contained in:
Joshua Chen 2022-03-29 16:37:29 +08:00 committed by GitHub
parent e31e91ef47
commit 77662260f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 551 additions and 506 deletions

View file

@ -8,7 +8,6 @@
import path from 'path';
import fs from 'fs-extra';
import {createHash} from 'crypto';
import {simpleHash, docuHash} from './hashUtils';
import {findAsyncSequential} from './jsUtils';
const fileHash = new Map<string, string>();
@ -18,7 +17,8 @@ const fileHash = new Map<string, string>();
* differs from cache (for hot reload performance).
*
* @param generatedFilesDir Absolute path.
* @param file Path relative to `generatedFilesDir`.
* @param file Path relative to `generatedFilesDir`. File will always be
* outputted; no need to ensure directory exists.
* @param content String content to write.
* @param skipCache If `true` (defaults as `true` for production), file is
* force-rewritten, skipping cache.
@ -29,7 +29,7 @@ export async function generate(
content: string,
skipCache: boolean = process.env.NODE_ENV === 'production',
): Promise<void> {
const filepath = path.join(generatedFilesDir, file);
const filepath = path.resolve(generatedFilesDir, file);
if (skipCache) {
await fs.outputFile(filepath, content);
@ -62,35 +62,6 @@ export async function generate(
}
}
const chunkNameCache = new Map<string, string>();
/**
* Generate unique chunk name given a module path.
*/
export function genChunkName(
modulePath: string,
prefix?: string,
preferredName?: string,
shortId: boolean = process.env.NODE_ENV === 'production',
): string {
let chunkName = chunkNameCache.get(modulePath);
if (!chunkName) {
if (shortId) {
chunkName = simpleHash(modulePath, 8);
} else {
let str = modulePath;
if (preferredName) {
const shortHash = simpleHash(modulePath, 3);
str = `${preferredName}${shortHash}`;
}
const name = str === '/' ? 'index' : docuHash(str);
chunkName = prefix ? `${prefix}---${name}` : name;
}
chunkNameCache.set(modulePath, chunkName);
}
return chunkName;
}
/**
* @param permalink The URL that the HTML file corresponds to, without base URL
* @param outDir Full path to the output directory