Skip to content

Commit

Permalink
Merge pull request #16904 from webpack/issue-16724
Browse files Browse the repository at this point in the history
Support `createRequire` from `node: module` similar to `createRequire` from `module`
  • Loading branch information
TheLarkInn committed Apr 5, 2023
2 parents b2cd779 + 2145fde commit 276154a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/dependencies/CommonJsImportsParserPlugin.js
Expand Up @@ -444,13 +444,14 @@ class CommonJsImportsParserPlugin {

if (!options.createRequire) return;

let moduleName;
let moduleName = [];
let specifierName;

if (options.createRequire === true) {
moduleName = "module";
moduleName = ["module", "node:module"];
specifierName = "createRequire";
} else {
let moduleName;
const match = /^(.*) from (.*)$/.exec(options.createRequire);
if (match) {
[, specifierName, moduleName] = match;
Expand Down Expand Up @@ -545,7 +546,7 @@ class CommonJsImportsParserPlugin {
},
(statement, source) => {
if (
source !== moduleName ||
!moduleName.includes(source) ||
statement.specifiers.length !== 1 ||
statement.specifiers[0].type !== "ImportSpecifier" ||
statement.specifiers[0].imported.type !== "Identifier" ||
Expand All @@ -570,7 +571,7 @@ class CommonJsImportsParserPlugin {
stage: -10
},
(statement, source, id, name) => {
if (source !== moduleName || id !== specifierName) return;
if (!moduleName.includes(source) || id !== specifierName) return;
parser.tagVariable(name, createRequireSpecifierTag);
return true;
}
Expand Down
1 change: 1 addition & 0 deletions test/configCases/require/module-require/d.js
@@ -0,0 +1 @@
module.exports = 4;
17 changes: 17 additions & 0 deletions test/configCases/require/module-require/index.js
@@ -1,5 +1,6 @@
import { createRequire as _createRequire } from "module";
import { createRequire as __createRequire, builtinModules } from "module";
import { createRequire as ___createRequire} from "node:module";

it("should evaluate require/createRequire", () => {
expect(
Expand All @@ -15,31 +16,47 @@ it("should evaluate require/createRequire", () => {
expect(
(function() { if (typeof require); }).toString()
).toBe('function() { if (true); }');
expect(
(function() { return typeof ___createRequire; }).toString()
).toBe('function() { return "function"; }');
expect(
(function() { if (typeof ___createRequire); }).toString()
).toBe('function() { if (true); }');
});

it("should create require", () => {
const require = _createRequire(import.meta.url);
expect(require("./a")).toBe(1);
expect(_createRequire(import.meta.url)("./c")).toBe(3);
expect(__createRequire(import.meta.url)("./b")).toBe(2);
expect(___createRequire(import.meta.url)("./d")).toBe(4);
const requireNodePrefix = __createRequire(import.meta.url);
expect(requireNodePrefix("./a")).toBe(1);
});

it("should resolve using created require", () => {
const require = _createRequire(import.meta.url);
expect(require.resolve("./b")).toBe("./b.js");
expect(_createRequire(import.meta.url).resolve("./b")).toBe("./b.js");
expect(__createRequire(import.meta.url).resolve("./b")).toBe("./b.js");
expect(___createRequire(import.meta.url).resolve("./b")).toBe("./b.js");
});

it("should provide require.cache", () => {
const _require = _createRequire(import.meta.url);
expect(require.cache).toBe(_require.cache);
expect(require.cache).toBe(_createRequire(import.meta.url).cache);
expect(require.cache).toBe(__createRequire(import.meta.url).cache);
expect(require.cache).toBe(___createRequire(import.meta.url).cache);
});

it("should provide dependency context", () => {
const _require = _createRequire(new URL("./foo/c.js", import.meta.url));
expect(_require("./a")).toBe(4);
const _require1 = _createRequire(new URL("./foo/", import.meta.url));
expect(_require1("./c")).toBe(5);
const _require2 = ___createRequire(new URL("./foo/", import.meta.url));
expect(_require2("./c")).toBe(5);
});

it("should add warning on using as expression", () => {
Expand Down

0 comments on commit 276154a

Please sign in to comment.