-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
103 lines (89 loc) · 3.71 KB
/
build.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
(function (module) {
const path = require('path');
const fs = require('fs');
const merge = require('merge');
function findAllMessageFiles(startDir) {
let ans = [];
fs.readdirSync(startDir).forEach((item) => {
let fullPath = startDir + '/' + item;
if (fs.lstatSync(fullPath).isDirectory() && ['min'].indexOf(item) === -1) {
ans = ans.concat(findAllMessageFiles(fullPath));
} else if (item.match(/messages(\.[a-zA-Z]{1,8})?\.json/)) {
ans.push(fullPath);
}
});
return ans;
}
function mergeTranslations(startDir, destFile, configuredLangs) {
let files = findAllMessageFiles(startDir);
let translations = {};
files.forEach((item) => {
const data = JSON.parse(fs.readFileSync(item));
validateLanguageCodes(Object.keys(data));
Object.keys(data).forEach(avail => {
if (configuredLangs.indexOf(avail) === -1) {
delete data[avail];
}
})
translations = merge.recursive(translations, data);
});
if (!destFile || destFile.length === 0) {
throw new Error('No target file for client-side translations specified');
} else if (!Array.isArray(destFile)) {
destFile = [destFile];
}
destFile.forEach((destItem) => {
fs.writeFileSync(destItem, "module.exports = " + JSON.stringify(translations) + ";\n");
const loadconf = (path) => {
false
}
});
}
function loadModulePathMap(conf, srcPath, distPath) {
const langs = Object.keys(conf['languages']);
console.log(conf)
console.log('langs: ', langs);
mergeTranslations(srcPath, path.resolve(distPath, '.compiled/translations.js'), langs);
const moduleMap = {
'translations': path.resolve(distPath, '.compiled/translations')
};
return moduleMap;
}
function validateLanguageCodes(codeList) {
codeList.forEach((item, i) => {
if (item.match(/^[a-z]{1,8}_[A-Za-z0-9]{1,8}$/)) {
console.log('\x1b[31m', `WARNING: Invalid language format - please use ${item.replace('_', '-')} instead of ${item}`, '\x1b[0m');
codeList[i] = item.replace('_','-');
console.log(' (auto-fixed)', '\n');
}
})
}
class ProcTranslationsPlugin {
constructor(srcPath, distPath, conf) {
this._srcPath = srcPath;
this._distPath = distPath;
this._conf = conf
}
apply(compiler) {
compiler.hooks.afterPlugins.tap('ProcTranslationsPlugin', (compilation) => {
const tmpJsDir = path.resolve(this._distPath, '.compiled');
if (fs.existsSync(tmpJsDir)) {
fs.readdirSync(tmpJsDir).forEach(item => {
fs.unlinkSync(path.resolve(tmpJsDir, item));
});
fs.rmdirSync(tmpJsDir);
}
fs.mkdirSync(tmpJsDir);
compiler.options.resolve.alias = loadModulePathMap(this._conf, this._srcPath, this._distPath);
console.log("\x1b[44m", 'Defined aliases:', "\x1b[0m");
Object.keys(compiler.options.resolve.alias).forEach(item => {
console.log("\x1b[32m", item, "\x1b[0m", '\u21D2', compiler.options.resolve.alias[item]);
});
});
}
}
module.exports.ProcTranslationsPlugin = ProcTranslationsPlugin;
module.exports.loadConf = (path) => {
return JSON.parse(fs.readFileSync(path));
};
})(module);