forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevSupportPlugin.js
More file actions
59 lines (50 loc) · 1.77 KB
/
DevSupportPlugin.js
File metadata and controls
59 lines (50 loc) · 1.77 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const NullFactory = require("./NullFactory");
const NullDependency = require("./dependencies/NullDependency");
class DevSupportPlugin {
constructor() {
}
apply(compiler) {
compiler.hooks.compilation.tap("DevSupportPlugin", (compilation, {
normalModuleFactory
}) => {
compilation.dependencyFactories.set(DevSupportDependency, new NullFactory());
compilation.dependencyTemplates.set(DevSupportDependency, new DevSupportDependency.Template());
const handler = (parser, parserOptions) => {
if(typeof parserOptions.devSupport !== "undefined" && !parserOptions.devSupport)
return;
parser.hooks.program.tap("DevSupportPlugin", () => {
const module = parser.state.module;
const dep = new DevSupportDependency(parser.state, module.buildInfo.strict);
module.buildInfo.strict = false;
module.addDependency(dep);
});
};
normalModuleFactory.hooks.parser.for("javascript/auto").tap("DevSupportPlugin", handler);
normalModuleFactory.hooks.parser.for("javascript/esm").tap("DevSupportPlugin", handler);
});
}
}
class DevSupportDependency extends NullDependency {
constructor(parserState, strict) {
super();
this.parserState = parserState;
this.strict = strict;
}
get type() {
return "dev support";
}
}
DevSupportDependency.Template = class DevSupportDependencyTemplate {
apply(dep, source, runtime, dependencyTemplates) {
source.insert(-1, `/* ${JSON.stringify(dep.parserState.harmonySpecifier && Array.from(dep.parserState.harmonySpecifier))} */`);
source.insert(-1, "(function() {");
if(dep.strict) source.insert(-1, "\"use strict\";");
source.insert(Infinity, "}());");
}
};
module.exports = DevSupportPlugin;