Skip to content

Commit 99058bc

Browse files
committed
WIP
1 parent 6922a5e commit 99058bc

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

lib/DevSupportPlugin.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
const NullFactory = require("./NullFactory");
8+
const NullDependency = require("./dependencies/NullDependency");
9+
10+
class DevSupportPlugin {
11+
constructor() {
12+
}
13+
14+
apply(compiler) {
15+
compiler.hooks.compilation.tap("DevSupportPlugin", (compilation, {
16+
normalModuleFactory
17+
}) => {
18+
compilation.dependencyFactories.set(DevSupportDependency, new NullFactory());
19+
compilation.dependencyTemplates.set(DevSupportDependency, new DevSupportDependency.Template());
20+
21+
const handler = (parser, parserOptions) => {
22+
if(typeof parserOptions.devSupport !== "undefined" && !parserOptions.devSupport)
23+
return;
24+
25+
parser.hooks.program.tap("DevSupportPlugin", () => {
26+
const module = parser.state.module;
27+
const dep = new DevSupportDependency(parser.state, module.buildInfo.strict);
28+
module.buildInfo.strict = false;
29+
module.addDependency(dep);
30+
});
31+
};
32+
normalModuleFactory.hooks.parser.for("javascript/auto").tap("DevSupportPlugin", handler);
33+
normalModuleFactory.hooks.parser.for("javascript/esm").tap("DevSupportPlugin", handler);
34+
});
35+
}
36+
}
37+
38+
class DevSupportDependency extends NullDependency {
39+
constructor(parserState, strict) {
40+
super();
41+
this.parserState = parserState;
42+
this.strict = strict;
43+
}
44+
45+
get type() {
46+
return "dev support";
47+
}
48+
}
49+
50+
DevSupportDependency.Template = class DevSupportDependencyTemplate {
51+
apply(dep, source, runtime, dependencyTemplates) {
52+
source.insert(-1, `/* ${JSON.stringify(dep.parserState.harmonySpecifier && Array.from(dep.parserState.harmonySpecifier))} */`);
53+
source.insert(-1, "(function() {");
54+
if(dep.strict) source.insert(-1, "\"use strict\";");
55+
source.insert(Infinity, "}());");
56+
}
57+
};
58+
59+
module.exports = DevSupportPlugin;

lib/WebpackOptionsApply.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const NamedModulesPlugin = require("./NamedModulesPlugin");
5656
const NamedChunksPlugin = require("./NamedChunksPlugin");
5757
const DefinePlugin = require("./DefinePlugin");
5858
const SizeLimitsPlugin = require("./performance/SizeLimitsPlugin");
59+
const DevSupportPlugin = require("./DevSupportPlugin");
5960

6061
class WebpackOptionsApply extends OptionsApply {
6162
constructor() {
@@ -302,6 +303,7 @@ class WebpackOptionsApply extends OptionsApply {
302303
minimizer.apply(compiler);
303304
}
304305
}
306+
new DevSupportPlugin().apply(compiler);
305307

306308
if(options.performance) {
307309
new SizeLimitsPlugin(options.performance).apply(compiler);

0 commit comments

Comments
 (0)