-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
131 lines (114 loc) · 4.11 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(function (module) {
const path = require('path');
const fs = require('fs');
const merge = require('merge');
function findAllMessageFiles(startDir) {
const ans = [];
fs.readdirSync(startDir).forEach((item) => {
let fullPath = startDir + '/' + item;
if (fs.statSync(fullPath).isDirectory() && ['min'].indexOf(item) === -1) {
ans.push(...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.info(conf)
console.info('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');
}
})
}
module.exports.ProcTranslationsPlugin = 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 => {
try {
fs.unlinkSync(path.resolve(tmpJsDir, item));
} catch (err) {
console.log('Ingored error: ', err);
}
});
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.loadConf = (path) => {
return JSON.parse(fs.readFileSync(path));
};
module.exports.createBabelOptions = (env) => {
return {
presets: [
["@babel/env", { modules: false }],
"@babel/react",
"@babel/typescript"
],
plugins: [
[
"babel-plugin-styled-components",
{
ssr: true,
displayNames: env === 'development'
}
],
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread",
"@babel/plugin-syntax-dynamic-import",
"babel-plugin-dynamic-import-webpack"
]
};
};
})(module);