-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (61 loc) · 2.22 KB
/
index.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
const fs = require('fs');
function ExtractStringsPlugin(options) {
this.variable = options.variable || 'R';
this.output = typeof options.output === 'string' ? options.output : false;
this.regex = new RegExp(`^${this.variable}\\.(\\w+)\\s*=\\s*(.*)$`);
this.strings_predefined = {};
this.strings = [];
}
ExtractStringsPlugin.prototype.apply = function (compiler) {
compiler.plugin('compilation', (compilation, params) => {
params.normalModuleFactory.plugin('parser', (parser) => {
parser.plugin('program', (expr) => {
// Scan comments for R.<identifier> = "<value>"
const comments = parser.getComments(expr.range).map((comment) => comment.value)
for (const comment of comments) {
const trim = comment.trim();
const matches = trim.match(this.regex);
if (matches) {
const string = matches[1];
const string_default_json = matches[2];
try {
this.strings_predefined[string] = JSON.parse(string_default_json)
} catch (e) {
console.warn(new SyntaxError("Could not parse comment as string literal: " + matches[2]));
}
}
}
// Look for R.<identifier>
const self = this;
(function traverse(expr) {
if (typeof expr === 'object' && expr) {
if (expr.type === 'MemberExpression') {
if (expr.object.name === self.variable) {
if (expr.property.type === 'Identifier') {
self.strings.push(expr.property.name);
}
}
}
for (const key in expr) {
traverse(expr[key]);
}
}
}(expr));
})
});
});
// Write data to output
compiler.plugin('done', () => {
for (const string of this.strings) {
if (typeof this.strings_predefined[string] === 'undefined') {
this.strings_predefined[string] = `#${string}`;
}
}
if (this.output) {
const sorted_keys = Object.keys(this.strings_predefined).sort();
sorted_keys.push('')
fs.writeFileSync(this.output, JSON.stringify({ "": this.strings_predefined }, sorted_keys, '\t'));
}
})
};
module.exports = ExtractStringsPlugin;