8000 fix: do not use heuristic fallback for "module-import" · webpack/webpack@60f1898 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60f1898

Browse files
committed
fix: do not use heuristic fallback for "module-import"
1 parent 66306aa commit 60f1898

File tree

6 files changed

+83
-16
lines changed

6 files changed

+83
-16
lines changed

lib/ExternalModule.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ const getSourceForImportExternal = (
166166
const importName = runtimeTemplate.outputOptions.importFunctionName;
167167
if (
168168
!runtimeTemplate.supportsDynamicImport() &&
169-
(importName === "import" || importName !== "module-import")
169+
(importName === "import" || importName === "module-import")
170170
) {
171171
throw new Error(
172172
"The target environment doesn't support 'import()' so it's not possible to use external type 'import'"
@@ -546,6 +546,25 @@ class ExternalModule extends Module {
546546
return callback(null, !this.buildMeta);
547547
}
548548

549+
/**
550+
* @param {string} externalType raw external type
551+
* @returns {string} resolved external type
552+
*/
553+
getModuleImportType(externalType) {
554+
if (externalType === "module-import") {
555+
if (
556+
this.dependencyMeta &&
557+
/** @type {ImportDependencyMeta} */ (this.dependencyMeta).externalType
558+
) {
559+
return /** @type {ImportDependencyMeta} */ (this.dependencyMeta)
560+
.externalType;
561+
}
562+
return "module";
563+
}
564+
565+
return externalType;
566+
}
567+
549568
/**
550569
* @param {WebpackOptions} options webpack options
551570
* @param {Compilation} compilation the compilation
@@ -597,14 +616,7 @@ class ExternalModule extends Module {
597616
case "module":
598617
case "import":
599618
case "module-import": {
600-
const type =
601-
externalType === "module-import" &&
602-
this.dependencyMeta &&
603-
/** @type {ImportDependencyMeta} */ (this.dependencyMeta).externalType
604-
? /** @type {ImportDependencyMeta} */ (this.dependencyMeta)
605-
.externalType
606-
: externalType;
607-
619+
const type = this.getModuleImportType(externalType);
608620
if (type === "module") {
609621
if (this.buildInfo.module) {
610622
if (!Array.isArray(request) || request.length === 1) {
@@ -742,13 +754,7 @@ class ExternalModule extends Module {
742754
case "module":
743755
case "import":
744756
case "module-import": {
745-
const type =
746-
externalType === "module-import" &&
747-
dependencyMeta &&
748-
/** @type {ImportDependencyMeta} */ (dependencyMeta).externalType
749-
? /** @type {ImportDependencyMeta} */ (dependencyMeta).externalType
750-
: externalType;
751-
757+
const type = this.getModuleImportType(externalType);
752758
if (type === "import") {
753759
return getSourceForImportExternal(
754760
request,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import external0 from "external0"; // module
2+
const external1 = require("external1"); // module
3+
const external2 = require("external2"); // node-commonjs
4+
const external3 = import("external3"); // import
5+
6+
console.log(external0, external1, external2, external3);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("module-import should correctly get fallback type", function() {
5+
const content = fs.readFileSync(path.resolve(__dirname, "a.js"), "utf-8");
6+
expect(content).toContain(`import * as __WEBPACK_EXTERNAL_MODULE_external0__ from "external0";`); // module
7+
expect(content).toContain(`import * as __WEBPACK_EXTERNAL_MODULE_external1__ from "external1";`); // module
8+
expect(content).toContain(`module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("external2");`); // node-commonjs
9+
expect(content).toContain(`module.exports = import("external3");`); // import
10+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
findBundle: (i, options) => ["main.js"]
3+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/** @type {import("../../../../types").Configuration} */
2+
module.exports = {
3+
target: ["web", "es2020"],
4+
node: {
5+
__dirname: false,
6+
__filename: false
7+
},
8+
output: {
9+
module: true,
10+
filename: "[name].js"
11+
},
12+
entry: {
13+
a: "./a",
14+
main: "./index"
15+
},
16+
optimization: {
17+
concatenateModules: true
18+
},
19+
experiments: {
20+
outputModule: true
21+
},
22+
externalsType: "module-import",
23+
externals: [
24+
function (
25+
{ context, request, contextInfo, getResolve, dependencyType },
26+
callback
27+
) {
28+
if (request === "external2") {
29+
return callback(null, "node-commonjs external2");
30+
}
31+
callback();
32+
},
33+
{
34+
external0: "external0",
35+
external1: "external1",
36+
external3: "external3",
37+
fs: "commonjs fs",
38+
path: "commonjs path"
39+
}
40+
]
41+
};

types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4582,6 +4582,7 @@ declare class ExternalModule extends Module {
45824582
externalType: string;
45834583
userRequest: string;
45844584
dependencyMeta?: ImportDependencyMeta | CssImportDependencyMeta;
4585+
getModuleImportType(externalType: string): string;
45854586

45864587
/**
45874588
* restore unsafe cache data

0 commit comments

Comments
 (0)
0