Skip to content

Commit 180f5e5

Browse files
committed
refactor to simpler way of extracting options from comments
1 parent 83aeea8 commit 180f5e5

5 files changed

Lines changed: 51 additions & 44 deletions

File tree

lib/Parser.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
*/
55
var acorn = require("acorn-dynamic-import").default;
66
var Tapable = require("tapable");
7+
var json5 = require("json5");
78
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
89

910
function Parser(options) {
1011
Tapable.call(this);
1112
this.options = options;
13+
this.scope = undefined;
14+
this.state = undefined;
15+
this.comments = undefined;
1216
this.initializeEvaluating();
1317
}
1418
module.exports = Parser;
@@ -1187,16 +1191,19 @@ Parser.prototype.parse = function parse(source, initialState) {
11871191
throw new Error("Source couldn't be parsed");
11881192
var oldScope = this.scope;
11891193
var oldState = this.state;
1194+
var oldComments = this.comments;
11901195
this.scope = {
11911196
inTry: false,
11921197
definitions: [],
11931198
renames: {}
11941199
};
11951200
var state = this.state = initialState || {};
1201+
this.comments = comments;
11961202
if(this.applyPluginsBailResult("program", ast, comments) === undefined)
11971203
this.walkStatements(ast.body);
11981204
this.scope = oldScope;
11991205
this.state = oldState;
1206+
this.comments = oldComments;
12001207
return state;
12011208
};
12021209

@@ -1216,3 +1223,20 @@ Parser.prototype.evaluate = function evaluate(source) {
12161223
throw new Error("evaluate: Source is not a expression");
12171224
return this.evaluateExpression(ast.body[0].expression);
12181225
};
1226+
1227+
Parser.prototype.getComments = function getComments(range) {
1228+
return this.comments.filter(comment => comment.range[0] >= range[0] && comment.range[1] <= range[1]);
1229+
};
1230+
1231+
Parser.prototype.getCommentOptions = function getCommentOptions(range) {
1232+
var comments = this.getComments(range);
1233+
if(comments.length === 0) return null;
1234+
var options = comments.map(comment => {
1235+
try {
1236+
return json5.parse(`{${comment.value}}`);
1237+
} catch(e) {
1238+
return {};
1239+
}
1240+
});
1241+
return options.reduce((o, i) => Object.assign(o, i), {});
1242+
};

lib/dependencies/ImportParserPlugin.js

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,21 @@ class ImportParserPlugin {
1515

1616
apply(parser) {
1717
const options = this.options;
18-
let parsedComments, chunkNameAssignment;
19-
20-
parser.plugin("program", (ast, comments) => {
21-
parsedComments = comments;
22-
});
23-
24-
// use /* webpackChunkName = "chunkName" */ to specify a chunkName
25-
parser.plugin("evaluate AssignmentExpression", (assignment) => {
26-
if(assignment.left.name === "webpackChunkName") {
27-
chunkNameAssignment = assignment;
28-
}
29-
});
3018

3119
parser.plugin(["call System.import", "import-call"], (expr) => {
3220
if(expr.arguments.length !== 1)
3321
throw new Error("Incorrect number of arguments provided to 'import(module: string) -> Promise'.");
3422

3523
const param = parser.evaluateExpression(expr.arguments[0]);
36-
const exprEndPos = expr.end;
37-
const paramEndPos = expr.arguments[0].end;
24+
3825
let chunkName = null;
3926

40-
//check chunkName from comments
41-
if(parsedComments.length && exprEndPos - paramEndPos > "/*webpackChunkName=*/".length) {
42-
for(let i = 0; i < parsedComments.length; i++) {
43-
let comment = parsedComments[i];
44-
// should match the location and length
45-
if(comment.type === "Block" && comment.start >= paramEndPos && comment.end < exprEndPos) {
46-
chunkNameAssignment = null;
47-
parser.evaluate(comment.value);
48-
// expect an AssignmentExpression after evaluate
49-
if(chunkNameAssignment) {
50-
const chunkNameExpr = parser.evaluateExpression(chunkNameAssignment.right);
51-
if(chunkNameExpr.isString()) {
52-
chunkName = chunkNameExpr.string;
53-
} else {
54-
throw new Error(`\`webpackChunkName\` expected a String, but received: ${comment.value} .`);
55-
}
56-
}
57-
break;
58-
}
27+
const importOptions = parser.getCommentOptions(expr.range);
28+
if(importOptions) {
29+
if(typeof importOptions.webpackChunkName !== "undefined") {
30+
if(typeof importOptions.webpackChunkName !== "string")
31+
throw new Error(`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`);
32+
chunkName = importOptions.webpackChunkName;
5933
}
6034
}
6135

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"enhanced-resolve": "^3.0.0",
1313
"interpret": "^1.0.0",
1414
"json-loader": "^0.5.4",
15+
"json5": "^0.5.1",
1516
"loader-runner": "^2.3.0",
1617
"loader-utils": "^0.2.16",
1718
"memory-fs": "~0.4.1",

test/cases/chunks/named-chunks/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,20 @@ it("should handle empty named chunks when there is an error callback", function(
6969
sync = false;
7070
});
7171
});
72-
72+
7373
it("should be able to use named chunks in import()", function(done) {
7474
var sync = false;
75-
import("./empty?import1-in-chunk1" /* webpackChunkName = "import-named-chunk-1" */).then(function(result){
76-
import("./empty?import2-in-chunk1" /* webpackChunkName = "import-named-chunk-1" */).then(function(result){
75+
import("./empty?import1-in-chunk1" /* webpackChunkName: "import-named-chunk-1" */).then(function(result){
76+
var i = 0;
77+
import("./empty?import2-in-chunk1" /* webpackChunkName: "import-named-chunk-1" */).then(function(result){
7778
sync.should.be.ok();
79+
if(i++ > 0) done();
7880
}).catch(function(err){
7981
done(err);
8082
});
81-
import("./empty?import3-in-chunk2" /* webpackChunkName = "import-named-chunk-2" */).then(function(result){
83+
import("./empty?import3-in-chunk2" /* webpackChunkName: "import-named-chunk-2" */).then(function(result){
8284
sync.should.not.be.ok();
83-
done();
85+
if(i++ > 0) done();
8486
}).catch(function(err){
8587
done(err);
8688
});
@@ -94,15 +96,17 @@ it("should be able to use named chunks in import()", function(done) {
9496
it("should be able to use named chunk in context import()", function(done) {
9597
var mpty = "mpty";
9698
var sync = false;
97-
import("./e" + mpty + "2" /* webpackChunkName = "context-named-chunk" */).then(function(result) {
98-
import("./e" + mpty + "3" /* webpackChunkName = "context-named-chunk" */).then(function(result){
99+
import("./e" + mpty + "2" /* webpackChunkName: "context-named-chunk" */).then(function(result) {
100+
var i = 0;
101+
import("./e" + mpty + "3" /* webpackChunkName: "context-named-chunk" */).then(function(result){
99102
sync.should.be.ok();
103+
if(i++ > 0) done();
100104
}).catch(function(err){
101105
done(err);
102106
});
103-
import("./e" + mpty + "4" /* webpackChunkName = "context-named-chunk-2" */).then(function(result){
107+
import("./e" + mpty + "4" /* webpackChunkName: "context-named-chunk-2" */).then(function(result){
104108
sync.should.not.be.ok();
105-
done();
109+
if(i++ > 0) done();
106110
}).catch(function(err){
107111
done(err);
108112
});

yarn.lock

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ acorn@^3.0.4:
4747
version "3.3.0"
4848
resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a"
4949

50-
acorn@^4.0.3, acorn@^4.0.4:
50+
acorn@^4.0.3:
5151
version "4.0.11"
5252
resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.11.tgz#edcda3bd937e7556410d42ed5860f67399c794c0"
5353

54+
acorn@^5.0.0:
55+
version "5.0.3"
56+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.0.3.tgz#c460df08491463f028ccb82eab3730bf01087b3d"
57+
5458
agent-base@2:
5559
version "2.0.1"
5660
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-2.0.1.tgz#bd8f9e86a8eb221fffa07bd14befd55df142815e"
@@ -2102,7 +2106,7 @@ json3@3.3.2:
21022106
version "3.3.2"
21032107
resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1"
21042108

2105-
json5@^0.5.0:
2109+
json5@^0.5.0, json5@^0.5.1:
21062110
version "0.5.1"
21072111
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
21082112

0 commit comments

Comments
 (0)