Skip to content

Commit 273ca4b

Browse files
committed
Minify the generated translation files
1 parent 5b141f0 commit 273ca4b

1 file changed

Lines changed: 48 additions & 21 deletions

File tree

vite.config.js

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { defineConfig } from "vite";
2-
import { existsSync } from "fs";
3-
import { mkdir, readdir, readFile, writeFile } from "fs/promises";
2+
import { access, mkdir, readdir, readFile, writeFile } from "fs/promises";
43
import { resolve } from "path";
54

65
function ckeditorTranslations() {
@@ -21,36 +20,64 @@ function ckeditorTranslations() {
2120
];
2221

2322
// Collect the translation directories that exist for these packages.
24-
const translationDirs = packages
25-
.map((pkg) =>
26-
resolve("node_modules/@ckeditor", pkg, "dist/translations"),
23+
const allDirs = packages.map((pkg) =>
24+
resolve("node_modules/@ckeditor", pkg, "dist/translations"),
25+
);
26+
const translationDirs = (
27+
await Promise.all(
28+
allDirs.map((dir) =>
29+
access(dir)
30+
.then(() => dir)
31+
.catch(() => null),
32+
),
2733
)
28-
.filter((dir) => existsSync(dir));
34+
).filter(Boolean);
2935

3036
// Determine available languages from the first directory.
3137
const languages = (await readdir(translationDirs[0]))
32-
.filter((f) => f.endsWith(".umd.js") && f !== "en.umd.js")
33-
.map((f) => f.replace(".umd.js", ""));
38+
.filter((f) => f.endsWith(".js") && !f.endsWith(".umd.js") && !f.endsWith(".d.ts") && f !== "en.js")
39+
.map((f) => f.replace(".js", ""));
3440

3541
await mkdir(targetDir, { recursive: true });
3642

37-
// For each language, concatenate the translation files from all packages.
43+
// For each language, merge dictionaries from all packages into a single IIFE.
3844
await Promise.all(
3945
languages.map(async (lang) => {
40-
const parts = await Promise.all(
41-
translationDirs.map(async (dir) => {
42-
const file = resolve(dir, `${lang}.umd.js`);
43-
if (existsSync(file)) {
44-
return readFile(file, "utf-8");
46+
const dictionaries = [];
47+
let getPluralForm = null;
48+
49+
for (const dir of translationDirs) {
50+
const file = resolve(dir, `${lang}.js`);
51+
let source;
52+
try {
53+
source = await readFile(file, "utf-8");
54+
} catch {
55+
continue;
56+
}
57+
58+
// Extract the dictionary entries from:
59+
// export default {"lang":{"dictionary":{...},getPluralForm(n){...}}}
60+
const dictMatch = source.match(/"dictionary":\{(.+?)\},getPluralForm/);
61+
if (dictMatch) {
62+
dictionaries.push(dictMatch[1]);
63+
}
64+
65+
if (getPluralForm === null) {
66+
const pluralMatch = source.match(/getPluralForm(\([^)]*\)\{.+?\})/);
67+
if (pluralMatch) {
68+
getPluralForm = pluralMatch[1];
4569
}
46-
return "";
47-
}),
48-
);
70+
}
71+
}
72+
73+
const merged =
74+
`(e=>{` +
75+
`const l=e['${lang}']||={dictionary:{},getPluralForm:null};` +
76+
`Object.assign(l.dictionary,{${dictionaries.join(",")}});` +
77+
`l.getPluralForm=${getPluralForm ? `function${getPluralForm}` : "null"};` +
78+
`})(window.CKEDITOR_TRANSLATIONS||={});`;
4979

50-
await writeFile(
51-
resolve(targetDir, `${lang}.js`),
52-
parts.filter(Boolean).join("\n"),
53-
);
80+
await writeFile(resolve(targetDir, `${lang}.js`), merged);
5481
}),
5582
);
5683
},

0 commit comments

Comments
 (0)