From 18c59c600d6d6149011f3c0a3b6d0e85b31329f3 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Tue, 22 Nov 2022 11:37:16 +0800 Subject: [PATCH 1/6] fix: avoid cross-realm objects --- lib/dependencies/CommonJsImportsParserPlugin.js | 3 +-- lib/dependencies/ImportParserPlugin.js | 11 +++++------ lib/dependencies/WorkerPlugin.js | 7 ++++--- lib/javascript/JavascriptParser.js | 15 +++++++++++---- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/dependencies/CommonJsImportsParserPlugin.js b/lib/dependencies/CommonJsImportsParserPlugin.js index e74e5c9743b..5a6e5faa4cd 100644 --- a/lib/dependencies/CommonJsImportsParserPlugin.js +++ b/lib/dependencies/CommonJsImportsParserPlugin.js @@ -236,8 +236,7 @@ class CommonJsImportsParserPlugin { parser.parseCommentOptions(expr.range); if (commentErrors) { - for (const e of commentErrors) { - const { comment } = e; + for (const { cause: e, comment } of commentErrors) { parser.state.module.addWarning( new CommentCompilationWarning( `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, diff --git a/lib/dependencies/ImportParserPlugin.js b/lib/dependencies/ImportParserPlugin.js index 151ff89adcc..3dc84bd83af 100644 --- a/lib/dependencies/ImportParserPlugin.js +++ b/lib/dependencies/ImportParserPlugin.js @@ -55,8 +55,7 @@ class ImportParserPlugin { parser.parseCommentOptions(expr.range); if (commentErrors) { - for (const e of commentErrors) { - const { comment } = e; + for (const { cause: e, comment } of commentErrors) { parser.state.module.addWarning( new CommentCompilationWarning( `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, @@ -137,7 +136,7 @@ class ImportParserPlugin { if (importOptions.webpackInclude !== undefined) { if ( !importOptions.webpackInclude || - importOptions.webpackInclude.constructor.name !== "RegExp" + !(importOptions.webpackInclude instanceof RegExp) ) { parser.state.module.addWarning( new UnsupportedFeatureWarning( @@ -146,13 +145,13 @@ class ImportParserPlugin { ) ); } else { - include = new RegExp(importOptions.webpackInclude); + include = importOptions.webpackInclude; } } if (importOptions.webpackExclude !== undefined) { if ( !importOptions.webpackExclude || - importOptions.webpackExclude.constructor.name !== "RegExp" + !(importOptions.webpackExclude instanceof RegExp) ) { parser.state.module.addWarning( new UnsupportedFeatureWarning( @@ -161,7 +160,7 @@ class ImportParserPlugin { ) ); } else { - exclude = new RegExp(importOptions.webpackExclude); + exclude = importOptions.webpackExclude; } } if (importOptions.webpackExports !== undefined) { diff --git a/lib/dependencies/WorkerPlugin.js b/lib/dependencies/WorkerPlugin.js index 5b68d84c06a..4dd193d44d7 100644 --- a/lib/dependencies/WorkerPlugin.js +++ b/lib/dependencies/WorkerPlugin.js @@ -203,8 +203,7 @@ class WorkerPlugin { parser.parseCommentOptions(expr.range); if (commentErrors) { - for (const e of commentErrors) { - const { comment } = e; + for (const { cause: e, comment } of commentErrors) { parser.state.module.addWarning( new CommentCompilationWarning( `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, @@ -246,7 +245,9 @@ class WorkerPlugin { } else { Object.assign( entryOptions, - importOptions.webpackEntryOptions + JSON.parse( + JSON.stringify(importOptions.webpackEntryOptions) + ) ); } } diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index c10c7b16eaf..c1e0d34dc3b 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -3641,11 +3641,18 @@ class JavascriptParser extends Parser { if (value && webpackCommentRegExp.test(value)) { // try compile only if webpack options comment is present try { - const val = vm.runInNewContext(`(function(){return {${value}};})()`); - Object.assign(options, val); + let val = vm.runInNewContext(`(function(){return {${value}};})()`); + const key = Object.getOwnPropertyNames(val)[0]; + if (!key) continue; + val = val[key]; + + if (typeof val === "object" && val !== null) { + if (val.constructor.name === "RegExp") val = new RegExp(val); + else val = JSON.parse(JSON.stringify(val)); + } + options[key] = val; } catch (e) { - e.comment = comment; - errors.push(e); + errors.push({ comment, cause: e }); } } } From 4e643be0d78f2f6b010d71fd1583c5e4a0f92258 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Tue, 22 Nov 2022 11:42:29 +0800 Subject: [PATCH 2/6] fix: remove extra change --- lib/dependencies/WorkerPlugin.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/dependencies/WorkerPlugin.js b/lib/dependencies/WorkerPlugin.js index 4dd193d44d7..a5a51a48154 100644 --- a/lib/dependencies/WorkerPlugin.js +++ b/lib/dependencies/WorkerPlugin.js @@ -245,9 +245,7 @@ class WorkerPlugin { } else { Object.assign( entryOptions, - JSON.parse( - JSON.stringify(importOptions.webpackEntryOptions) - ) + importOptions.webpackEntryOptions ); } } From 1d86c181a8342860676579ce8abc4d2e705b37f6 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Tue, 22 Nov 2022 11:52:04 +0800 Subject: [PATCH 3/6] fix: test fail --- lib/javascript/JavascriptParser.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index c1e0d34dc3b..780c2de03a7 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -3641,16 +3641,15 @@ class JavascriptParser extends Parser { if (value && webpackCommentRegExp.test(value)) { // try compile only if webpack options comment is present try { - let val = vm.runInNewContext(`(function(){return {${value}};})()`); - const key = Object.getOwnPropertyNames(val)[0]; - if (!key) continue; - val = val[key]; - - if (typeof val === "object" && val !== null) { - if (val.constructor.name === "RegExp") val = new RegExp(val); - else val = JSON.parse(JSON.stringify(val)); + for (let [key, val] of Object.entries( + vm.runInNewContext(`(function(){return {${value}};})()`) + )) { + if (typeof val === "object" && val !== null) { + if (val.constructor.name === "RegExp") val = new RegExp(val); + else val = JSON.parse(JSON.stringify(val)); + } + options[key] = val; } - options[key] = val; } catch (e) { errors.push({ comment, cause: e }); } From e7e2aecd1c3e5fc893664f85a1da3bf1083533bc Mon Sep 17 00:00:00 2001 From: Jack Works Date: Tue, 22 Nov 2022 11:57:02 +0800 Subject: [PATCH 4/6] update dts --- types.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/types.d.ts b/types.d.ts index 251d0adfd3d..10e2a474b77 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5476,7 +5476,9 @@ declare class JavascriptParser extends Parser { evaluatedVariable(tagInfo?: any): VariableInfo; parseCommentOptions( range?: any - ): { options: null; errors: null } | { options: object; errors: unknown[] }; + ): + | { options: null; errors: null } + | { options: object; errors: { comment: any; cause: unknown }[] }; extractMemberExpressionChain(expression: MemberExpression): { members: string[]; object: From c922ee15690941ba54a4b47c11c77003c2815a7c Mon Sep 17 00:00:00 2001 From: Jack Works Date: Tue, 22 Nov 2022 12:03:27 +0800 Subject: [PATCH 5/6] chore: revert breaking change --- lib/dependencies/CommonJsImportsParserPlugin.js | 3 ++- lib/dependencies/ImportParserPlugin.js | 3 ++- lib/dependencies/WorkerPlugin.js | 3 ++- lib/javascript/JavascriptParser.js | 6 +++++- types.d.ts | 4 +--- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/dependencies/CommonJsImportsParserPlugin.js b/lib/dependencies/CommonJsImportsParserPlugin.js index 5a6e5faa4cd..e74e5c9743b 100644 --- a/lib/dependencies/CommonJsImportsParserPlugin.js +++ b/lib/dependencies/CommonJsImportsParserPlugin.js @@ -236,7 +236,8 @@ class CommonJsImportsParserPlugin { parser.parseCommentOptions(expr.range); if (commentErrors) { - for (const { cause: e, comment } of commentErrors) { + for (const e of commentErrors) { + const { comment } = e; parser.state.module.addWarning( new CommentCompilationWarning( `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, diff --git a/lib/dependencies/ImportParserPlugin.js b/lib/dependencies/ImportParserPlugin.js index 3dc84bd83af..718b0482828 100644 --- a/lib/dependencies/ImportParserPlugin.js +++ b/lib/dependencies/ImportParserPlugin.js @@ -55,7 +55,8 @@ class ImportParserPlugin { parser.parseCommentOptions(expr.range); if (commentErrors) { - for (const { cause: e, comment } of commentErrors) { + for (const e of commentErrors) { + const { comment } = e; parser.state.module.addWarning( new CommentCompilationWarning( `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, diff --git a/lib/dependencies/WorkerPlugin.js b/lib/dependencies/WorkerPlugin.js index a5a51a48154..5b68d84c06a 100644 --- a/lib/dependencies/WorkerPlugin.js +++ b/lib/dependencies/WorkerPlugin.js @@ -203,7 +203,8 @@ class WorkerPlugin { parser.parseCommentOptions(expr.range); if (commentErrors) { - for (const { cause: e, comment } of commentErrors) { + for (const e of commentErrors) { + const { comment } = e; parser.state.module.addWarning( new CommentCompilationWarning( `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index 780c2de03a7..61d3eafe470 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -3635,6 +3635,7 @@ class JavascriptParser extends Parser { return EMPTY_COMMENT_OPTIONS; } let options = {}; + /** @type {unknown[]} */ let errors = []; for (const comment of comments) { const { value } = comment; @@ -3651,7 +3652,10 @@ class JavascriptParser extends Parser { options[key] = val; } } catch (e) { - errors.push({ comment, cause: e }); + const newErr = new Error(String(e.message)); + newErr.stack = String(e.stack); + newErr.comment = comment; + errors.push(newErr); } } } diff --git a/types.d.ts b/types.d.ts index 10e2a474b77..251d0adfd3d 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5476,9 +5476,7 @@ declare class JavascriptParser extends Parser { evaluatedVariable(tagInfo?: any): VariableInfo; parseCommentOptions( range?: any - ): - | { options: null; errors: null } - | { options: object; errors: { comment: any; cause: unknown }[] }; + ): { options: null; errors: null } | { options: object; errors: unknown[] }; extractMemberExpressionChain(expression: MemberExpression): { members: string[]; object: From 4f39c9f65878ef0f5db5754d73157f5f13d56352 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Tue, 22 Nov 2022 12:09:17 +0800 Subject: [PATCH 6/6] fix: type error --- lib/javascript/JavascriptParser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index 61d3eafe470..58bcc4a64b3 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -3654,7 +3654,7 @@ class JavascriptParser extends Parser { } catch (e) { const newErr = new Error(String(e.message)); newErr.stack = String(e.stack); - newErr.comment = comment; + Object.assign(newErr, { comment }); errors.push(newErr); } }