Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parser hook calls for compiler plugins #18128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/optimize/InnerGraph.js
Expand Up @@ -266,12 +266,11 @@ exports.tagTopLevelSymbol = (parser, name) => {
const innerGraphState = getState(parser.state);
if (!innerGraphState) return;

parser.defineVariable(name);

const existingTag = /** @type {TopLevelSymbol} */ (
parser.getTagData(name, topLevelSymbolTag)
);
if (existingTag) {
parser.defineVariable(name);
return existingTag;
}

Expand Down
76 changes: 76 additions & 0 deletions test/CompilerParserHooks.test.js
@@ -0,0 +1,76 @@
"use strict";

require("./helpers/warmup-webpack");

const path = require("path");
const { createFsFromVolume, Volume } = require("memfs");
const { JAVASCRIPT_MODULE_TYPE_ESM } = require("../lib/ModuleTypeConstants");
const pluginName = "CompilerParserHooksTest";

describe("Compiler Parser Plugin", () => {
function runWebpackWithEntry(entry, setupParser) {
return new Promise((resolve, reject) => {
const webpack = require("..");
const compiler = webpack({
context: path.join(__dirname, "fixtures"),
entry: entry,
output: {
path: "/directory",
filename: "bundle.js"
},
plugins: [
{
apply(compiler) {
compiler.hooks.thisCompilation.tap(
pluginName,
(_, { normalModuleFactory }) => {
normalModuleFactory.hooks.parser
.for(JAVASCRIPT_MODULE_TYPE_ESM)
.tap(pluginName, parser => {
setupParser(parser);
});
}
);
}
}
]
});
compiler.outputFileSystem = createFsFromVolume(new Volume());
compiler.run(err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

describe("top level symbol", () => {
it("should trigger new hook for classes", async () => {
let foundExpression;
await runWebpackWithEntry("./top-level-symbols.mjs", parser => {
parser.hooks.new.for("TopLevelClass").tap(pluginName, expr => {
foundExpression = expr;
});
});
expect(foundExpression).toBeTruthy();
expect(foundExpression.type).toBe("NewExpression");
expect(foundExpression.callee.type).toBe("Identifier");
expect(foundExpression.callee.name).toBe("TopLevelClass");
});

it("should trigger call hooks for functions", async () => {
let foundExpression;
await runWebpackWithEntry("./top-level-symbols.mjs", parser => {
parser.hooks.call.for("topLevelFunction").tap(pluginName, expr => {
foundExpression = expr;
});
});
expect(foundExpression).toBeTruthy();
expect(foundExpression.type).toBe("CallExpression");
expect(foundExpression.callee.type).toBe("Identifier");
expect(foundExpression.callee.name).toBe("topLevelFunction");
});
});
});
4 changes: 4 additions & 0 deletions test/fixtures/top-level-symbols.mjs
@@ -0,0 +1,4 @@
class TopLevelClass { }
function topLevelFunction() { }
new TopLevelClass();
topLevelFunction();