-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslationsPlugin.js
176 lines (159 loc) · 5.82 KB
/
TranslationsPlugin.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/** @author: yashvesikar */
const fs = require("fs");
const path = require("path");
const webpack = require("webpack");
const i18nConfig = require("./i18n.config");
const { Compilation } = webpack;
const { RawSource, RawModule } = webpack.sources;
const CustomTranslationCallTag = Symbol("CustomTranslationCallTag");
function readJsonFile(filePath) {
// slowest possible way to read a file - probably the biggest bottleneck of the whole plugin
const fileData = fs.readFileSync(filePath);
const jsonData = JSON.parse(fileData);
return jsonData;
}
class TranslationPlugin {
constructor() {
this.translationsMap = new Map();
const locales = i18nConfig.i18n.locales;
this.translations = locales.reduce((acc, locale) => {
// get relative path to src/messages directory
const relativePath = path.join(
__dirname,
"src",
"messages",
`${locale}.json`
);
acc[locale] = readJsonFile(relativePath);
return acc;
}, {});
this.translationManifest = {};
this.emitTranslationBundle = this.emitTranslationBundle.bind(this);
this.generateTranslationsMap = this.generateTranslationsMap.bind(this);
}
apply(compiler) {
if (compiler.options.name !== "client") {
return;
}
this.generateTranslationsMap(compiler);
// compiler.hooks.emit.tap(
compiler.hooks.thisCompilation.tap(
"TranslationPlugin",
// client side only
(compilation) => {
compilation.hooks.processAssets.tap(
{
name: "TranslationPlugin",
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
},
(_) => {
const { entrypoints } = compilation;
entrypoints.forEach((entrypoint) => {
this.emitTranslationBundle(compiler, compilation, entrypoint);
});
}
);
return true;
}
);
}
/**
* Extracts all the t("") function call arguments.
* This could technically be done in a webpack loader using clever regex but by using the AST the solution is technically more correct in generic albeit slower.
* @param {webpack.Compiler} compiler
*/
generateTranslationsMap(compiler) {
compiler.hooks.normalModuleFactory.tap("TranslationPlugin", (factory) => {
factory.hooks.parser
.for("javascript/auto")
.tap("TranslationPlugin", (parser) => {
// find all call expressions to useTranslations hook and extract the declared variable name - conventionally this is `t`
parser.hooks.evaluateCallExpression
.for("useTranslations")
.tap("TranslationPlugin", (expr) => {
const isDeclaration =
parser.statementPath.at(-1).type === "VariableDeclaration";
if (isDeclaration) {
const declarations = parser.statementPath.at(-1).declarations;
const variableName =
declarations[0].type === "VariableDeclarator"
? declarations[0].id.name
: null;
if (!variableName) return;
parser.tagVariable(variableName, CustomTranslationCallTag);
}
});
// find all call expressions to declared variable name i.e. t("key") function and extract the argument "key"
// generate the translation map module identifier -> {key1, key2, ...}
parser.hooks.expression
.for(CustomTranslationCallTag)
.tap("TranslationPlugin", (expr) => {
const regex = new RegExp(`${expr.name}\\("([^"]+)"\\)`, "g");
const matches = parser.state.source
.match(regex)
.map((match) =>
match.substring(expr.name.length + 2, match.length - 2)
);
if (this.translationsMap.has(parser.state.current.identifier())) {
this.translationsMap.set(
parser.state.current.identifier(),
new Set([
...this.translationsMap.get(
parser.state.current.identifier()
),
...matches,
])
);
} else {
this.translationsMap.set(
parser.state.current.identifier(),
new Set(matches)
);
}
});
});
});
}
/**
* Emit translation bundle for each entrypoint with only the translations used in the entrypoint
* @param {webpack.Compilation} compilation
* @param {*} entrypoint
* @returns
*/
emitTranslationBundle(compiler, compilation, entrypoint) {
entrypoint._modulePostOrderIndices.forEach((_, value) => {
const identifier = value.identifier();
// if this is the primary entrpoint mdoule
if (this.translationsMap.has(identifier)) {
Object.keys(this.translations).forEach((locale) => {
const outputPathAndFilename = path.resolve(
compilation.options.output.path,
"i18n",
entrypoint.options.name,
`${locale}.json`
);
const relativeOutputPath = path.relative(
compilation.options.output.path,
outputPathAndFilename
);
const content = {};
for (let key of this.translationsMap.get(identifier).values()) {
let translation = this.translations[locale][key];
if (!translation) {
console.warn(`Missing translation for key: ${key}`);
translation = key;
}
content[key] = translation;
}
// emit the translation bundle
compilation.emitAsset(
relativeOutputPath,
new RawSource(JSON.stringify(content))
);
});
}
});
return;
}
}
module.exports = TranslationPlugin;