Skip to content

Commit 25ec7e4

Browse files
committed
Do not stop processing on error
1 parent 4b1a76b commit 25ec7e4

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

lib/Parser.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ const webpackImportCommentOptions = [
4343

4444
const webpackCommentRegExp = new RegExp(webpackImportCommentOptions.join("|"));
4545

46+
const EMPTY_COMMENT_OPTIONS = {
47+
options: null,
48+
errors: null
49+
};
50+
4651
class Parser extends Tapable {
4752
constructor(options, sourceType = "auto") {
4853
super();
@@ -1996,23 +2001,25 @@ class Parser extends Tapable {
19962001

19972002
parseCommentOptions(range) {
19982003
const comments = this.getComments(range);
1999-
if (comments.length === 0) return null;
2000-
const options = comments.map(comment => {
2004+
if (comments.length === 0) {
2005+
return EMPTY_COMMENT_OPTIONS;
2006+
}
2007+
let options = {};
2008+
let errors = [];
2009+
for (const comment of comments) {
20012010
const { value } = comment;
20022011
if (value && webpackCommentRegExp.test(value)) {
20032012
// try compile only if webpack options comment is present
20042013
try {
20052014
const val = vm.runInNewContext(`(function(){return {${value}};})()`);
2006-
return val;
2015+
Object.assign(options, val);
20072016
} catch (e) {
2008-
throw Error(
2009-
`compilation error while processing: /*${value}*/: ${e.message}`
2010-
);
2017+
e.comment = comment;
2018+
errors.push(e);
20112019
}
20122020
}
2013-
return {};
2014-
});
2015-
return options.reduce((o, i) => Object.assign(o, i), {});
2021+
}
2022+
return { options, errors };
20162023
}
20172024

20182025
getNameForExpression(expression) {
@@ -2100,11 +2107,7 @@ class Parser extends Tapable {
21002107
Object.defineProperty(Parser.prototype, "getCommentOptions", {
21012108
configurable: false,
21022109
value: util.deprecate(function(range) {
2103-
try {
2104-
return this.parseCommentOptions(range);
2105-
} catch (e) {
2106-
return {};
2107-
}
2110+
return this.parseCommentOptions(range).options;
21082111
}, "Parser.getCommentOptions: Use Parser.parseCommentOptions(range) instead")
21092112
});
21102113

lib/dependencies/ImportParserPlugin.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,26 @@ class ImportParserPlugin {
3030
let mode = "lazy";
3131
let include = null;
3232
let exclude = null;
33-
let importOptions = null;
3433
const groupOptions = {};
3534

36-
try {
37-
importOptions = parser.parseCommentOptions(expr.range);
38-
} catch (e) {
39-
parser.state.module.warnings.push(
40-
new ImportOptionsCompileWarning(
41-
e.message,
42-
parser.state.module,
43-
expr.loc
44-
)
45-
);
35+
const {
36+
options: importOptions,
37+
errors: commentErrors
38+
} = parser.parseCommentOptions(expr.range);
39+
40+
if (commentErrors) {
41+
for (const e of commentErrors) {
42+
const { comment } = e;
43+
parser.state.module.warnings.push(
44+
new ImportOptionsCompileWarning(
45+
`compilation error while processing: /*${comment.value}*/: ${
46+
e.message
47+
}`,
48+
parser.state.module,
49+
comment.loc
50+
)
51+
);
52+
}
4653
}
4754

4855
if (importOptions) {

test/__snapshots__/StatsTestCases.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,11 +1026,11 @@ Entrypoint main = main.js
10261026
10271027
WARNING in ./chunk.js
10281028
compilation error while processing: /* webpack Prefetch: 0, webpackChunkName: \\"notGoingToCompile-c\\" */: Unexpected identifier
1029-
@ ./index.js 4:4-90 1:0-49
1029+
@ ./index.js 4:11-77 1:0-49
10301030
10311031
WARNING in ./chunk.js
10321032
compilation error while processing: /* webpackPrefetch: true, webpackChunkName: notGoingToCompileChunkName */: notGoingToCompileChunkName is not defined
1033-
@ ./index.js 2:4-97 1:0-49"
1033+
@ ./index.js 2:11-84 1:0-49"
10341034
`;
10351035

10361036
exports[`StatsTestCases should print correct stats for limit-chunk-count-plugin 1`] = `

0 commit comments

Comments
 (0)