-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.config.js
More file actions
110 lines (98 loc) · 3.2 KB
/
Copy pathvite.config.js
File metadata and controls
110 lines (98 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { defineConfig } from "vite";
import { access, mkdir, readdir, readFile, writeFile } from "fs/promises";
import { resolve } from "path";
function ckeditorTranslations() {
return {
name: "ckeditor5-translations",
async writeBundle(options) {
const outDir = options.dir || "dist";
const targetDir = resolve(outDir, "translations");
// Parse app.ts to find all @ckeditor/ckeditor5-* packages.
const appSource = await readFile("app.ts", "utf-8");
const packages = [
...new Set(
[...appSource.matchAll(/@ckeditor\/(ckeditor5-[a-z-]+)/g)].map(
(m) => m[1],
),
),
];
// Collect the translation directories that exist for these packages.
const allDirs = packages.map((pkg) =>
resolve("node_modules/@ckeditor", pkg, "dist/translations"),
);
const translationDirs = (
await Promise.all(
allDirs.map((dir) =>
access(dir)
.then(() => dir)
.catch(() => null),
),
)
).filter(Boolean);
// Determine available languages from the first directory.
const languages = (await readdir(translationDirs[0]))
.filter(
(f) =>
f.endsWith(".js") &&
!f.endsWith(".umd.js") &&
!f.endsWith(".d.ts") &&
f !== "en.js",
)
.map((f) => f.replace(".js", ""));
await mkdir(targetDir, { recursive: true });
// For each language, merge dictionaries from all packages into a single IIFE.
await Promise.all(
languages.map(async (lang) => {
const dictionaries = [];
let getPluralForm = null;
for (const dir of translationDirs) {
const file = resolve(dir, `${lang}.js`);
let source;
try {
source = await readFile(file, "utf-8");
} catch {
continue;
}
// Extract the dictionary entries from:
// export default {"lang":{"dictionary":{...},getPluralForm(n){...}}}
const dictMatch = source.match(
/"dictionary":\{(.+?)\},getPluralForm/,
);
if (dictMatch) {
dictionaries.push(dictMatch[1]);
}
if (getPluralForm === null) {
const pluralMatch = source.match(
/getPluralForm(\([^)]*\)\{.+?\})/,
);
if (pluralMatch) {
getPluralForm = pluralMatch[1];
}
}
}
const merged =
`(e=>{` +
`const l=e['${lang}']||={dictionary:{},getPluralForm:null};` +
`Object.assign(l.dictionary,{${dictionaries.join(",")}});` +
`l.getPluralForm=${getPluralForm ? `function${getPluralForm}` : "null"};` +
`})(window.CKEDITOR_TRANSLATIONS||={});`;
await writeFile(resolve(targetDir, `${lang}.js`), merged);
}),
);
},
};
}
export default defineConfig({
build: {
lib: {
entry: ["app.ts"],
fileName: "ckeditor5",
formats: ["umd"],
name: "ckeditor5",
},
},
define: {
define: "undefined",
},
plugins: [ckeditorTranslations()],
});