Skip to content

Commit

Permalink
perf: reuse VM context across webpack magic comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Feb 1, 2024
2 parents cc6a628 + 7cbf145 commit c6e8b70
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/javascript/JavascriptParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ class JavascriptParser extends Parser {
/** @type {WeakMap<Expression, Set<string>> | undefined} */
this.destructuringAssignmentProperties = undefined;
this.currentTagData = undefined;
this.magicCommentContext = vm.createContext(undefined, {
name: "Webpack Magic Comment Parser",
codeGeneration: { strings: false, wasm: false }
});
this._initializeEvaluating();
}

Expand Down Expand Up @@ -4465,7 +4469,10 @@ class JavascriptParser extends Parser {
// try compile only if webpack options comment is present
try {
for (let [key, val] of Object.entries(
vm.runInNewContext(`(function(){return {${value}};})()`)
vm.runInContext(
`(function(){return {${value}};})()`,
this.magicCommentContext
)
)) {
if (typeof val === "object" && val !== null) {
if (val.constructor.name === "RegExp") val = new RegExp(val);
Expand Down
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import {
SyncWaterfallHook
} from "tapable";
import { SecureContextOptions, TlsOptions } from "tls";
import { Context } from "vm";

declare class AbstractLibraryPlugin<T> {
constructor(__0: {
Expand Down Expand Up @@ -5739,6 +5740,7 @@ declare class JavascriptParser extends Parser {
| ExportAllDeclaration;
destructuringAssignmentProperties?: WeakMap<Expression, Set<string>>;
currentTagData: any;
magicCommentContext: Context;
destructuringAssignmentPropertiesFor(
node: Expression
): undefined | Set<string>;
Expand Down

0 comments on commit c6e8b70

Please sign in to comment.