Skip to content

Commit

Permalink
Merge pull request #16500 from Jack-Works/avoid-cross-realm-object
Browse files Browse the repository at this point in the history
refactor: avoid cross-realm objects
  • Loading branch information
sokra committed Dec 5, 2022
2 parents 8241da7 + 4f39c9f commit 4b4ca3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/dependencies/ImportParserPlugin.js
Expand Up @@ -137,7 +137,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(
Expand All @@ -146,13 +146,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(
Expand All @@ -161,7 +161,7 @@ class ImportParserPlugin {
)
);
} else {
exclude = new RegExp(importOptions.webpackExclude);
exclude = importOptions.webpackExclude;
}
}
if (importOptions.webpackExports !== undefined) {
Expand Down
18 changes: 14 additions & 4 deletions lib/javascript/JavascriptParser.js
Expand Up @@ -3635,17 +3635,27 @@ class JavascriptParser extends Parser {
return EMPTY_COMMENT_OPTIONS;
}
let options = {};
/** @type {unknown[]} */
let errors = [];
for (const comment of comments) {
const { value } = comment;
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);
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;
}
} catch (e) {
e.comment = comment;
errors.push(e);
const newErr = new Error(String(e.message));
newErr.stack = String(e.stack);
Object.assign(newErr, { comment });
errors.push(newErr);
}
}
}
Expand Down

0 comments on commit 4b4ca3b

Please sign in to comment.