Merge branch 'develop' into johannes/webpack-5
This commit is contained in:
commit
a56a2268f0
14 changed files with 881 additions and 1023 deletions
|
@ -6,7 +6,6 @@ import parseArgs from "minimist";
|
|||
import * as chokidar from "chokidar";
|
||||
import * as fs from "node:fs";
|
||||
import _ from "lodash";
|
||||
import { Cpx } from "cpx";
|
||||
import { util } from "webpack";
|
||||
import { Translations } from "matrix-web-i18n";
|
||||
|
||||
|
@ -16,29 +15,6 @@ const INCLUDE_LANGS = [...new Set([...fs.readdirSync(I18N_BASE_PATH), ...fs.read
|
|||
.filter((fn) => fn.endsWith(".json"))
|
||||
.map((f) => f.slice(0, -5));
|
||||
|
||||
// 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
|
||||
// "dest/b/...".
|
||||
const COPY_LIST: [
|
||||
sourceGlob: string,
|
||||
outputPath: string,
|
||||
opts?: {
|
||||
directwatch?: 1;
|
||||
},
|
||||
][] = [
|
||||
["res/apple-app-site-association", "webapp"],
|
||||
["res/manifest.json", "webapp"],
|
||||
["res/sw.js", "webapp"],
|
||||
["res/welcome.html", "webapp"],
|
||||
["res/welcome/**", "webapp/welcome"],
|
||||
["res/themes/**", "webapp/themes"],
|
||||
["res/vector-icons/**", "webapp/vector-icons"],
|
||||
["res/decoder-ring/**", "webapp/decoder-ring"],
|
||||
["node_modules/matrix-react-sdk/res/media/**", "webapp/media"],
|
||||
["node_modules/@matrix-org/olm/olm_legacy.js", "webapp", { directwatch: 1 }],
|
||||
["./config.json", "webapp", { directwatch: 1 }],
|
||||
["contribute.json", "webapp"],
|
||||
];
|
||||
const argv = parseArgs(process.argv.slice(2), {});
|
||||
|
||||
const watch = argv.w;
|
||||
|
@ -60,53 +36,13 @@ if (!fs.existsSync("webapp/i18n/")) {
|
|||
fs.mkdirSync("webapp/i18n/");
|
||||
}
|
||||
|
||||
function next(i: number, err?: Error): void {
|
||||
errCheck(err);
|
||||
|
||||
if (i >= COPY_LIST.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const ent = COPY_LIST[i];
|
||||
const source = ent[0];
|
||||
const dest = ent[1];
|
||||
const opts = ent[2] || {};
|
||||
const cpx = new Cpx(source, dest);
|
||||
|
||||
const logWatch = (path: string) => {
|
||||
if (verbose) {
|
||||
cpx.on("copy", (event) => {
|
||||
console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
|
||||
});
|
||||
cpx.on("remove", (event) => {
|
||||
console.log(`Removed: ${event.path}`);
|
||||
});
|
||||
console.log(`Watching: ${path}`);
|
||||
}
|
||||
};
|
||||
|
||||
const cb = (err?: Error): void => {
|
||||
next(i + 1, err);
|
||||
};
|
||||
|
||||
if (watch) {
|
||||
if (opts.directwatch) {
|
||||
// cpx -w creates a watcher for the parent of any files specified,
|
||||
// which in the case of config.json is '.', which inevitably takes
|
||||
// ages to crawl. So we create our own watcher on the files
|
||||
// instead.
|
||||
const copy = (): void => {
|
||||
cpx.copy(errCheck);
|
||||
};
|
||||
chokidar.watch(source).on("add", copy).on("change", copy).on("ready", cb).on("error", errCheck);
|
||||
} else {
|
||||
cpx.on("watch-ready", cb);
|
||||
cpx.on("watch-error", cb);
|
||||
cpx.watch();
|
||||
}
|
||||
} else {
|
||||
cpx.copy(cb);
|
||||
}
|
||||
}
|
||||
|
||||
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 riotWebFile = I18N_BASE_PATH + lang + ".json";
|
||||
|
||||
|
@ -127,12 +63,14 @@ function genLangFile(lang: string, dest: string): string {
|
|||
const digest = util.createHash("xxhash64").update(jsonBuffer).digest("hex").slice(0, 7);
|
||||
const filename = `${lang}.${digest}.json`;
|
||||
|
||||
return [filename, json];
|
||||
}
|
||||
|
||||
function genLangFile(dest: string, filename: string, json: string) {
|
||||
fs.writeFileSync(dest + filename, json);
|
||||
if (verbose) {
|
||||
console.log("Generated language file: " + filename);
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
function genLangList(langFileMap: Record<string, string>): void {
|
||||
|
@ -175,29 +113,38 @@ function watchLanguage(lang: string, dest: string, langFileMap: Record<string, s
|
|||
clearTimeout(makeLangDebouncer);
|
||||
}
|
||||
makeLangDebouncer = setTimeout(() => {
|
||||
const filename = genLangFile(lang, dest);
|
||||
const [filename, json] = prepareLangFile(lang, dest);
|
||||
genLangFile(dest, filename, json);
|
||||
langFileMap[lang] = filename;
|
||||
genLangList(langFileMap);
|
||||
}, 500);
|
||||
};
|
||||
|
||||
[reactSdkFile, riotWebFile].forEach(function (f) {
|
||||
chokidar.watch(f).on("add", makeLang).on("change", makeLang).on("error", errCheck);
|
||||
chokidar
|
||||
.watch(f, { ignoreInitial: true })
|
||||
.on("ready", () => {
|
||||
logWatch(f);
|
||||
})
|
||||
.on("add", makeLang)
|
||||
.on("change", makeLang)
|
||||
.on("error", errCheck);
|
||||
});
|
||||
}
|
||||
|
||||
// language resources
|
||||
const I18N_DEST = "webapp/i18n/";
|
||||
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;
|
||||
return m;
|
||||
}, {});
|
||||
genLangList(I18N_FILENAME_MAP);
|
||||
|
||||
if (watch) {
|
||||
INCLUDE_LANGS.forEach((l) => watchLanguage(l, I18N_DEST, I18N_FILENAME_MAP));
|
||||
} else {
|
||||
genLangList(I18N_FILENAME_MAP);
|
||||
}
|
||||
|
||||
// non-language resources
|
||||
next(0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue