-
Notifications
You must be signed in to change notification settings - Fork 27
/
GlobalizeCompilerHelper.js
93 lines (76 loc) · 2.92 KB
/
GlobalizeCompilerHelper.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
"use strict";
const fs = require("fs");
const globalizeCompiler = require("globalize-compiler");
const path = require("path");
const { SyncHook } = require("tapable");
class GlobalizeCompilerHelper {
constructor(attributes) {
this.asts = {};
this.extracts = [];
this.extractsMap = {};
this.modules = {};
this.cldr = attributes.cldr;
this.developmentLocale = attributes.developmentLocale;
this.messages = attributes.messages || {};
this.timeZoneData = attributes.timeZoneData;
this.tmpdir = attributes.tmpdir;
this.webpackCompiler = attributes.webpackCompiler;
this.webpackCompiler.hooks.globalizeBeforeCompileExtracts = new SyncHook(["locale", "attributes", "request"]);
}
setAst(request, ast) {
this.asts[request] = ast;
}
getExtract(request) {
let ast, extract;
if(!this.extractsMap[request]) {
ast = this.asts[request];
extract = globalizeCompiler.extract(ast);
this.extractsMap[request] = extract;
this.extracts.push(extract);
}
return this.extractsMap[request];
}
createCompiledDataModule(request, locale) {
const filepath = this.getModuleFilepath(request, locale);
this.modules[filepath] = true;
fs.writeFileSync(filepath, this.compile(locale, request));
return filepath;
}
getModuleFilepath(request, locale) {
// Always append .js to the file path to cater for non-JS files (e.g. .coffee).
return path.join(this.tmpdir, request.replace(/.*!/, "").replace(/[\/\\?" :\.]/g, "-") + "-" + locale + ".js");
}
compile(locale, request) {
let content;
const attributes = {
cldr: this.cldr,
defaultLocale: locale,
extracts: request ? this.getExtract(request) : this.extracts,
timeZoneData: this.timeZoneData
};
if (this.messages[locale]) {
attributes.messages = this.messages[locale];
}
this.webpackCompiler.hooks.globalizeBeforeCompileExtracts.call(locale, attributes, request);
try {
content = globalizeCompiler.compileExtracts(attributes);
} catch(e) {
// The only case to throw is when it's missing formatters/parsers for the
// whole chunk, i.e., when `request` isn't present; or when error is
// something else obviously. If a particular file misses formatters/parsers,
// it can be safely ignored (i.e., by using a stub content), because in the
// end generating the content for the whole chunk will ultimately verify
// whether or not formatters/parsers has been used.
if (!/No formatters or parsers has been provided/.test(e.message) || !request) {
throw e;
}
content = "module.exports = require(\"globalize\");";
}
// Inject set defaultLocale.
return content.replace(/(return Globalize;)/, "Globalize.locale(\"" + locale + "\"); $1");
}
isCompiledDataModule(request) {
return request && this.modules[request.replace(/.*!/, "")];
}
}
module.exports = GlobalizeCompilerHelper;