-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (37 loc) · 1.17 KB
/
index.js
File metadata and controls
45 lines (37 loc) · 1.17 KB
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
const acorn = require("acorn");
class CheckEsVersionPlugin {
constructor({ esVersion } = { esVersion: 5 }) {
this.esVersion = esVersion;
}
apply(compiler) {
compiler.hooks.emit.tap("CheckEsVersionPlugin", (compilation) => {
for (const [filename, asset] of Object.entries(compilation.assets)) {
if (!/\.js$/.test(filename)) {
continue;
}
const source = asset.source();
try {
acorn.parse(source, {
ecmaVersion: this.esVersion,
});
} catch (err) {
if (err instanceof SyntaxError) {
compilation.errors.push(this.buildError({
err,
source,
filename,
}));
} else {
compilation.errors.push(err);
}
}
}
});
}
buildError({ err, source, filename }) {
return new SyntaxError(`Invalid ES${this.esVersion} at ${filename}: ${err}`);
}
}
module.exports = CheckEsVersionPlugin; // not recommended
// `const { CheckEsVersionPlugin } = require("@bitjourney/check-es-version-webpack-plugin")` is recommended:
module.exports.CheckEsVersionPlugin = CheckEsVersionPlugin;