Skip to content

Commit

Permalink
Merge pull request #15282 from webpack/feature/__webpack_module__
Browse files Browse the repository at this point in the history
add __webpack_module__ and __webpack_module_id__ to the api
  • Loading branch information
sokra committed Jan 31, 2022
2 parents 46e8639 + a962d2c commit 612de99
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
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__")
.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__;

0 comments on commit 612de99

Please sign in to comment.