Skip to content

Commit ec262a4

Browse files
committed
refactoring: moved parser instancation into NormalModuleFactory
breaking change: compiler.parser must no longer be used. Use this instead: ``` js compiler.plugin("compilation", function(compilation, params) { params.normalModuleFactory.plugin("parser", function(parser, parserOptions) { parser.plugin(/* ... */); }); }); ``` required for webpack#2978
1 parent 9c9b20a commit ec262a4

20 files changed

Lines changed: 765 additions & 601 deletions

lib/APIPlugin.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,27 @@ var REPLACEMENT_TYPES = {
2626
};
2727
var IGNORES = [];
2828
APIPlugin.prototype.apply = function(compiler) {
29-
compiler.plugin("compilation", function(compilation) {
29+
compiler.plugin("compilation", function(compilation, params) {
3030
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
3131
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
32-
});
33-
Object.keys(REPLACEMENTS).forEach(function(key) {
34-
compiler.parser.plugin("expression " + key, function(expr) {
35-
var dep = new ConstDependency(REPLACEMENTS[key], expr.range);
36-
dep.loc = expr.loc;
37-
this.state.current.addDependency(dep);
38-
return true;
39-
});
40-
compiler.parser.plugin("evaluate typeof " + key, function(expr) {
41-
return new BasicEvaluatedExpression().setString(REPLACEMENT_TYPES[key]).setRange(expr.range);
42-
});
43-
});
44-
IGNORES.forEach(function(key) {
45-
compiler.parser.plugin(key, function() {
46-
return true;
32+
33+
params.normalModuleFactory.plugin("parser", function(parser) {
34+
Object.keys(REPLACEMENTS).forEach(function(key) {
35+
parser.plugin("expression " + key, function(expr) {
36+
var dep = new ConstDependency(REPLACEMENTS[key], expr.range);
37+
dep.loc = expr.loc;
38+
this.state.current.addDependency(dep);
39+
return true;
40+
});
41+
parser.plugin("evaluate typeof " + key, function(expr) {
42+
return new BasicEvaluatedExpression().setString(REPLACEMENT_TYPES[key]).setRange(expr.range);
43+
});
44+
});
45+
IGNORES.forEach(function(key) {
46+
parser.plugin(key, function() {
47+
return true;
48+
});
49+
});
4750
});
4851
});
4952
};

lib/CompatibilityPlugin.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,31 @@ function CompatibilityPlugin() {}
1111
module.exports = CompatibilityPlugin;
1212

1313
CompatibilityPlugin.prototype.apply = function(compiler) {
14-
compiler.plugin("compilation", function(compilation) {
14+
compiler.plugin("compilation", function(compilation, params) {
1515
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
1616
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
17-
});
18-
compiler.parser.plugin("call require", function(expr) {
19-
// support for browserify style require delegator: "require(o, !0)"
20-
if(expr.arguments.length !== 2) return;
21-
var second = this.evaluateExpression(expr.arguments[1]);
22-
if(!second.isBoolean()) return;
23-
if(second.asBool() !== true) return;
24-
var dep = new ConstDependency("require", expr.callee.range);
25-
dep.loc = expr.loc;
26-
if(this.state.current.dependencies.length > 1) {
27-
var last = this.state.current.dependencies[this.state.current.dependencies.length - 1];
28-
if(last.critical && last.request === "." && last.userRequest === "." && last.recursive)
29-
this.state.current.dependencies.pop();
30-
}
31-
this.state.current.addDependency(dep);
32-
return true;
17+
18+
params.normalModuleFactory.plugin("parser", function(parser, parserOptions) {
19+
20+
if(typeof parserOptions.browserify !== "undefined" && !parserOptions.browserify)
21+
return;
22+
23+
parser.plugin("call require", function(expr) {
24+
// support for browserify style require delegator: "require(o, !0)"
25+
if(expr.arguments.length !== 2) return;
26+
var second = this.evaluateExpression(expr.arguments[1]);
27+
if(!second.isBoolean()) return;
28+
if(second.asBool() !== true) return;
29+
var dep = new ConstDependency("require", expr.callee.range);
30+
dep.loc = expr.loc;
31+
if(this.state.current.dependencies.length > 1) {
32+
var last = this.state.current.dependencies[this.state.current.dependencies.length - 1];
33+
if(last.critical && last.request === "." && last.userRequest === "." && last.recursive)
34+
this.state.current.dependencies.pop();
35+
}
36+
this.state.current.addDependency(dep);
37+
return true;
38+
});
39+
});
3340
});
3441
};

lib/Compiler.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var assign = require("object-assign");
77
var Tapable = require("tapable");
88

99
var Compilation = require("./Compilation");
10-
var Parser = require("./Parser");
1110
var Resolver = require("enhanced-resolve/lib/Resolver");
1211

1312
var NormalModuleFactory = require("./NormalModuleFactory");
@@ -160,7 +159,36 @@ function Compiler() {
160159
loader: new Resolver(null),
161160
context: new Resolver(null)
162161
};
163-
this.parser = new Parser();
162+
var deprecationReported = false;
163+
this.parser = {
164+
plugin: function(hook, fn) {
165+
if(!deprecationReported) {
166+
console.warn("webpack: Using compiler.parser is deprecated.\n" +
167+
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.plugin(/* ... */); });\n}); instead. " +
168+
"It was called " + new Error().stack.split("\n")[2].trim() + ".");
169+
deprecationReported = true;
170+
}
171+
this.plugin("compilation", function(compilation, data) {
172+
data.normalModuleFactory.plugin("parser", function(parser) {
173+
parser.plugin(hook, fn);
174+
});
175+
});
176+
}.bind(this),
177+
apply: function() {
178+
var args = arguments;
179+
if(!deprecationReported) {
180+
console.warn("webpack: Using compiler.parser is deprecated.\n" +
181+
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.apply(/* ... */); });\n}); instead. " +
182+
"It was called " + new Error().stack.split("\n")[2].trim() + ".");
183+
deprecationReported = true;
184+
}
185+
this.plugin("compilation", function(compilation, data) {
186+
data.normalModuleFactory.plugin("parser", function(parser) {
187+
parser.apply.apply(parser, args);
188+
});
189+
});
190+
}.bind(this)
191+
}
164192

165193
this.options = {};
166194
}
@@ -369,7 +397,6 @@ Compiler.prototype.createChildCompiler = function(compilation, compilerName, out
369397
childCompiler.inputFileSystem = this.inputFileSystem;
370398
childCompiler.outputFileSystem = null;
371399
childCompiler.resolvers = this.resolvers;
372-
childCompiler.parser = this.parser;
373400
childCompiler.fileTimestamps = this.fileTimestamps;
374401
childCompiler.contextTimestamps = this.contextTimestamps;
375402
if(!this.records[compilerName]) this.records[compilerName] = [];
@@ -403,7 +430,7 @@ Compiler.prototype.newCompilation = function(params) {
403430
};
404431

405432
Compiler.prototype.createNormalModuleFactory = function() {
406-
var normalModuleFactory = new NormalModuleFactory(this.options.context, this.resolvers, this.parser, this.options.module || {});
433+
var normalModuleFactory = new NormalModuleFactory(this.options.context, this.resolvers, this.options.module || {});
407434
this.applyPlugins("normal-module-factory", normalModuleFactory);
408435
return normalModuleFactory;
409436
};

lib/ConstPlugin.js

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,47 @@ function ConstPlugin() {}
1616
module.exports = ConstPlugin;
1717

1818
ConstPlugin.prototype.apply = function(compiler) {
19-
compiler.plugin("compilation", function(compilation) {
19+
compiler.plugin("compilation", function(compilation, params) {
2020
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
2121
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
22-
});
23-
compiler.parser.plugin("statement if", function(statement) {
24-
var param = this.evaluateExpression(statement.test);
25-
var bool = param.asBool();
26-
if(typeof bool === "boolean") {
27-
if(statement.test.type !== "Literal") {
28-
var dep = new ConstDependency(bool + "", param.range);
29-
dep.loc = statement.loc;
30-
this.state.current.addDependency(dep);
31-
}
32-
return bool;
33-
}
34-
});
35-
compiler.parser.plugin("expression ?:", function(expression) {
36-
var param = this.evaluateExpression(expression.test);
37-
var bool = param.asBool();
38-
if(typeof bool === "boolean") {
39-
if(expression.test.type !== "Literal") {
40-
var dep = new ConstDependency(" " + bool + "", param.range);
41-
dep.loc = expression.loc;
42-
this.state.current.addDependency(dep);
43-
}
44-
return bool;
45-
}
46-
});
47-
compiler.parser.plugin("evaluate Identifier __resourceQuery", function(expr) {
48-
if(!this.state.module) return;
49-
var res = new BasicEvaluatedExpression();
50-
res.setString(getQuery(this.state.module.resource));
51-
res.setRange(expr.range);
52-
return res;
53-
});
54-
compiler.parser.plugin("expression __resourceQuery", function() {
55-
if(!this.state.module) return;
56-
this.state.current.addVariable("__resourceQuery", JSON.stringify(getQuery(this.state.module.resource)));
57-
return true;
22+
23+
params.normalModuleFactory.plugin("parser", function(parser) {
24+
parser.plugin("statement if", function(statement) {
25+
var param = this.evaluateExpression(statement.test);
26+
var bool = param.asBool();
27+
if(typeof bool === "boolean") {
28+
if(statement.test.type !== "Literal") {
29+
var dep = new ConstDependency(bool + "", param.range);
30+
dep.loc = statement.loc;
31+
this.state.current.addDependency(dep);
32+
}
33+
return bool;
34+
}
35+
});
36+
parser.plugin("expression ?:", function(expression) {
37+
var param = this.evaluateExpression(expression.test);
38+
var bool = param.asBool();
39+
if(typeof bool === "boolean") {
40+
if(expression.test.type !== "Literal") {
41+
var dep = new ConstDependency(" " + bool + "", param.range);
42+
dep.loc = expression.loc;
43+
this.state.current.addDependency(dep);
44+
}
45+
return bool;
46+
}
47+
});
48+
parser.plugin("evaluate Identifier __resourceQuery", function(expr) {
49+
if(!this.state.module) return;
50+
var res = new BasicEvaluatedExpression();
51+
res.setString(getQuery(this.state.module.resource));
52+
res.setRange(expr.range);
53+
return res;
54+
});
55+
parser.plugin("expression __resourceQuery", function() {
56+
if(!this.state.module) return;
57+
this.state.current.addVariable("__resourceQuery", JSON.stringify(getQuery(this.state.module.resource)));
58+
return true;
59+
});
60+
});
5861
});
5962
};

0 commit comments

Comments
 (0)