-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-and-flush-i18n.js
116 lines (99 loc) · 3.41 KB
/
merge-and-flush-i18n.js
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
111
112
113
114
115
116
const fPath = require('path');
const fs = require('fs');
class MergeAndFlushI18nPlugin {
constructor(state) {
this.transFolder = 'i18n-dicts';
this.state = state;
this.fired = false;
}
apply(compiler) {
compiler.hooks.afterCompile.tap('Hello World Plugin', stats => {
if (this.fired) {
console.log('Skip flush twice');
return;
}
this.fired = true;
this.doMergeAndFlush();
});
}
doMergeAndFlush() {
// this.logInputData();
const rootFiles = this.findRootFiles();
this.checkStaticRootsAndBundleNames(rootFiles);
this.mkdir(this.transFolder);
this.bundlAndFlushBundleDicts(rootFiles);
}
logInputData() {
console.log(`Flush dynamic import graph:\n${JSON.stringify(this.state.dynamicImports.sourceVertexes, null, 2)}`);
console.log(`Flush static import graph:\n${JSON.stringify(this.state.staticImports.sourceVertexes, null, 2)}`);
console.log(`Flush translation dictionaries:\n${JSON.stringify(this.state.dicts.dicts, null, 2)}`);
}
findReachableFiles(rootFile, accumulator) {
const reachable = this.state.staticImports.r(rootFile);
accumulator.add(rootFile);
if (reachable) {
reachable.forEach(file => {
this.findReachableFiles(file, accumulator);
});
}
}
combineDictionaries(files) {
const combined = {};
[...files].forEach(file => {
const fileDictEntries = this.state.dicts.entries(file);
for (let entry of fileDictEntries) {
combined[entry[0]] = entry[1];
}
});
// console.log(`combine files: ${JSON.stringify([...files])} ==> ${JSON.stringify(combined)}`);
return combined;
}
flushDictionary(rootFile, dictionary) {
const folder = `${this.transFolder}${fPath.sep}${fPath.basename(rootFile)}`;
this.mkdir(folder);
fs.writeFileSync(`${folder}${fPath.sep}index.json`, JSON.stringify(dictionary));
}
mkdir(folder) {
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
}
findDynamicRootFiles() {
const result = new Set();
for (let srcEntry of this.state.dynamicImports.entries()) {
srcEntry[1].forEach(dstFile => result.add(dstFile));
}
return result;
}
checkStaticRootsAndBundleNames(rootFiles) {
const dynamicRoots = this.findDynamicRootFiles();
const staticRootFiles = [...rootFiles].filter(file => !dynamicRoots.has(file));
if (staticRootFiles.length !== 1) {
throw new Error(
`expected 1 static root file but got ${JSON.stringify(staticRootFiles)}`);
}
const uniqueBaseNames = new Set([...rootFiles].map(file => fPath.basename(file)));
if (uniqueBaseNames.size != rootFiles.size) {
throw new Error(
`base names ofo some root files are umbigous: ${JSON.stringify([...rootFiles])}`);
}
}
bundlAndFlushBundleDicts(rootFiles) {
for (let rootFile of rootFiles) {
const reachableFiles = new Set();
this.findReachableFiles(rootFile, reachableFiles);
const dictionary = this.combineDictionaries(reachableFiles);
this.flushDictionary(rootFile, dictionary);
}
}
findRootFiles() {
const dstFiles = new Set();
for (let srcEntry of this.state.staticImports.entries()) {
srcEntry[1].forEach(dstFile => dstFiles.add(dstFile));
}
return new Set(this.state.staticImports
.sources()
.filter(file => !dstFiles.has(file)));
}
}
module.exports = MergeAndFlushI18nPlugin;