Prevent copy-res -w from triggering unnecessary changes while webpack is already building

This commit is contained in:
Johannes Marbach 2023-11-15 21:43:00 +01:00
parent 08bc6d816a
commit ba72b3b09b
2 changed files with 28 additions and 17 deletions

View file

@ -45,9 +45,9 @@
"build:bundle-stats": "webpack --progress --mode production --json > webpack-stats.json", "build:bundle-stats": "webpack --progress --mode production --json > webpack-stats.json",
"build:module_system": "tsc --project ./tsconfig.module_system.json && node ./lib/module_system/scripts/install.js", "build:module_system": "tsc --project ./tsconfig.module_system.json && node ./lib/module_system/scripts/install.js",
"dist": "scripts/package.sh", "dist": "scripts/package.sh",
"start": "yarn build:module_system && concurrently --kill-others-on-fail --prefix \"{time} [{name}]\" -n res,element-js \"yarn start:res\" \"yarn start:js\"", "start": "concurrently --kill-others-on-fail --prefix \"{time} [{name}]\" -n modules,res,jitsi \"yarn build:module_system\" \"yarn build:res\" \"yarn build:jitsi\" && concurrently --kill-others-on-fail --prefix \"{time} [{name}]\" -n res,element-js \"yarn start:res\" \"yarn start:js\"",
"start:https": "concurrently --kill-others-on-fail --prefix \"{time} [{name}]\" -n res,element-js \"yarn start:res\" \"yarn start:js --https\"", "start:https": "concurrently --kill-others-on-fail --prefix \"{time} [{name}]\" -n res,element-js \"yarn start:res\" \"yarn start:js --https\"",
"start:res": "yarn build:jitsi && ts-node scripts/copy-res.ts -w", "start:res": "ts-node scripts/copy-res.ts -w",
"start:js": "webpack serve --output-path webapp --mode development", "start:js": "webpack serve --output-path webapp --mode development",
"lint": "yarn lint:types && yarn lint:js && yarn lint:style", "lint": "yarn lint:types && yarn lint:js && yarn lint:style",
"lint:js": "yarn lint:js:src && yarn lint:js:module_system", "lint:js": "yarn lint:js:src && yarn lint:js:module_system",

View file

@ -19,10 +19,7 @@ const INCLUDE_LANGS = [...new Set([...fs.readdirSync(I18N_BASE_PATH), ...fs.read
// cpx includes globbed parts of the filename in the destination, but excludes // cpx includes globbed parts of the filename in the destination, but excludes
// common parents. Hence, "res/{a,b}/**": the output will be "dest/a/..." and // common parents. Hence, "res/{a,b}/**": the output will be "dest/a/..." and
// "dest/b/...". // "dest/b/...".
const COPY_LIST: [ const COPY_LIST: [sourceGlob: string, outputPath: string][] = [
sourceGlob: string,
outputPath: string,
][] = [
["res/apple-app-site-association", "webapp"], ["res/apple-app-site-association", "webapp"],
["res/manifest.json", "webapp"], ["res/manifest.json", "webapp"],
["res/sw.js", "webapp"], ["res/sw.js", "webapp"],
@ -65,7 +62,7 @@ function createCpx(source: string, dest: string): Cpx {
}); });
} }
return cpx; return cpx;
}; }
const logWatch = (path: string) => { const logWatch = (path: string) => {
if (verbose) { if (verbose) {
@ -96,8 +93,12 @@ function next(i: number, err?: Error): void {
const copy = (path: string): void => { const copy = (path: string): void => {
createCpx(path, dest).copy(errCheck); createCpx(path, dest).copy(errCheck);
}; };
chokidar.watch(source) chokidar
.on("ready", () => { logWatch(source); cb(); }) .watch(source, { ignoreInitial: true })
.on("ready", () => {
logWatch(source);
cb();
})
.on("add", copy) .on("add", copy)
.on("change", copy) .on("change", copy)
.on("error", errCheck); .on("error", errCheck);
@ -106,7 +107,7 @@ function next(i: number, err?: Error): void {
} }
} }
function genLangFile(lang: string, dest: string): string { function prepareLangFile(lang: string, dest: string): [filename: string, json: string] {
const reactSdkFile = REACT_I18N_BASE_PATH + lang + ".json"; const reactSdkFile = REACT_I18N_BASE_PATH + lang + ".json";
const riotWebFile = I18N_BASE_PATH + lang + ".json"; const riotWebFile = I18N_BASE_PATH + lang + ".json";
@ -127,12 +128,14 @@ function genLangFile(lang: string, dest: string): string {
const digest = loaderUtils.getHashDigest(jsonBuffer, null, "hex", 7); const digest = loaderUtils.getHashDigest(jsonBuffer, null, "hex", 7);
const filename = `${lang}.${digest}.json`; const filename = `${lang}.${digest}.json`;
return [filename, json];
}
function genLangFile(dest: string, filename: string, json: string) {
fs.writeFileSync(dest + filename, json); fs.writeFileSync(dest + filename, json);
if (verbose) { if (verbose) {
console.log("Generated language file: " + filename); console.log("Generated language file: " + filename);
} }
return filename;
} }
function genLangList(langFileMap: Record<string, string>): void { function genLangList(langFileMap: Record<string, string>): void {
@ -175,15 +178,19 @@ function watchLanguage(lang: string, dest: string, langFileMap: Record<string, s
clearTimeout(makeLangDebouncer); clearTimeout(makeLangDebouncer);
} }
makeLangDebouncer = setTimeout(() => { makeLangDebouncer = setTimeout(() => {
const filename = genLangFile(lang, dest); const [filename, json] = prepareLangFile(lang, dest);
genLangFile(dest, filename, json);
langFileMap[lang] = filename; langFileMap[lang] = filename;
genLangList(langFileMap); genLangList(langFileMap);
}, 500); }, 500);
}; };
[reactSdkFile, riotWebFile].forEach(function (f) { [reactSdkFile, riotWebFile].forEach(function (f) {
chokidar.watch(f) chokidar
.on("ready", () => { logWatch(f); }) .watch(f, { ignoreInitial: true })
.on("ready", () => {
logWatch(f);
})
.on("add", makeLang) .on("add", makeLang)
.on("change", makeLang) .on("change", makeLang)
.on("error", errCheck); .on("error", errCheck);
@ -193,14 +200,18 @@ function watchLanguage(lang: string, dest: string, langFileMap: Record<string, s
// language resources // language resources
const I18N_DEST = "webapp/i18n/"; const I18N_DEST = "webapp/i18n/";
const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce<Record<string, string>>((m, l) => { const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce<Record<string, string>>((m, l) => {
const filename = genLangFile(l, I18N_DEST); const [filename, json] = prepareLangFile(l, I18N_DEST);
if (!watch) {
genLangFile(I18N_DEST, filename, json);
}
m[l] = filename; m[l] = filename;
return m; return m;
}, {}); }, {});
genLangList(I18N_FILENAME_MAP);
if (watch) { if (watch) {
INCLUDE_LANGS.forEach((l) => watchLanguage(l, I18N_DEST, I18N_FILENAME_MAP)); INCLUDE_LANGS.forEach((l) => watchLanguage(l, I18N_DEST, I18N_FILENAME_MAP));
} else {
genLangList(I18N_FILENAME_MAP);
} }
// non-language resources // non-language resources