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

add __webpack_module__ and __webpack_module_id__ to the api #15282

Merged
merged 1 commit into from Jan 31, 2022
Merged
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
33 changes: 33 additions & 0 deletions lib/APIPlugin.js
Expand Up @@ -201,6 +201,39 @@ class APIPlugin {
)
.setRange(expr.range)
);

parser.hooks.expression
.for("__webpack_module_id__")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe __webpack_module__.id this looks more natural + we could want add another property like __webpack_module__[property]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep

.tap("APIPlugin", expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
"__webpack_module_id__";
const dep = new ConstDependency(
parser.state.module.moduleArgument + ".id",
expr.range,
[RuntimeGlobals.moduleId]
);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
});

parser.hooks.expression
.for("__webpack_module__")
.tap("APIPlugin", expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
"__webpack_module__";
const dep = new ConstDependency(
parser.state.module.moduleArgument,
expr.range,
[RuntimeGlobals.module]
);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
});
parser.hooks.evaluateTypeof
.for("__webpack_module__")
.tap("APIPlugin", evaluateToString("object"));
};

normalModuleFactory.hooks.parser
Expand Down
2 changes: 0 additions & 2 deletions lib/Module.js
Expand Up @@ -400,15 +400,13 @@ class Module extends DependenciesBlock {
// BACKWARD-COMPAT END

/**
* @deprecated moved to .buildInfo.exportsArgument
* @returns {string} name of the exports argument
*/
get exportsArgument() {
return (this.buildInfo && this.buildInfo.exportsArgument) || "exports";
}

/**
* @deprecated moved to .buildInfo.moduleArgument
* @returns {string} name of the module argument
*/
get moduleArgument() {
Expand Down
1 change: 1 addition & 0 deletions test/cases/parsing/api/id.js
@@ -0,0 +1 @@
export default __webpack_module_id__;
18 changes: 18 additions & 0 deletions test/cases/parsing/api/index.js
@@ -0,0 +1,18 @@
import id from "./id";
import mod from "./module";
import modType from "./typeof-module";

it("should support __webpack_module_id__", () => {
expect(typeof id).toMatch(/^(string|number)$/);
expect(id).not.toBe(__webpack_module_id__);
});

it("should support __webpack_module__", () => {
expect(mod.exports).toBeTypeOf("object");
expect(typeof mod.id).toMatch(/^(string|number)$/);
expect(mod).not.toBe(__webpack_module__);
});

it("should support typeof __webpack_module__", () => {
expect(modType).toBe("object");
});
1 change: 1 addition & 0 deletions test/cases/parsing/api/module.js
@@ -0,0 +1 @@
export default __webpack_module__;
1 change: 1 addition & 0 deletions test/cases/parsing/api/typeof-module.js
@@ -0,0 +1 @@
export default typeof __webpack_module__;