8000 Revert "fix: module-import get fallback from externalsPresets" · webpack/webpack@66306aa · GitHub
[go: up one dir, main page]

Skip to content

Commit 66306aa

Browse files
committed
Revert "fix: module-import get fallback from externalsPresets"
This reverts commit c951c98.
1 parent 8bf907b commit 66306aa

File tree

7 files changed

+92
-152
lines changed

7 files changed

+92
-152
lines changed

lib/ExternalModule.js

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const propertyAccess = require("./util/propertyAccess");
2222
const { register } = require("./util/serialization");
2323

2424
/** @typedef {import("webpack-sources").Source} Source */
25-
/** @typedef {import("../declarations/WebpackOptions").ExternalsPresets} ExternalsPresets */
2625
/** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
2726
/** @typedef {import("./Chunk")} Chunk */
2827
/** @typedef {import("./ChunkGraph")} ChunkGraph */
@@ -54,7 +53,7 @@ const { register } = require("./util/serialization");
5453
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
5554
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
5655

57-
/** @typedef {{ attributes?: ImportAttributes, externalType: "import" | "module" | undefined, externalsPresets: ExternalsPresets | undefined }} ImportDependencyMeta */
56+
/** @typedef {{ attributes?: ImportAttributes, externalType: "import" | "module" | undefined }} ImportDependencyMeta */
5857
/** @typedef {{ layer?: string, supports?: string, media?: string }} CssImportDependencyMeta */
5958

6059
/** @typedef {ImportDependencyMeta | CssImportDependencyMeta} DependencyMeta */
@@ -167,7 +166,7 @@ const getSourceForImportExternal = (
167166
const importName = runtimeTemplate.outputOptions.importFunctionName;
168167
if (
169168
!runtimeTemplate.supportsDynamicImport() &&
170-
(importName === "import" || importName === "module-import")
169+
(importName === "import" || importName !== "module-import")
171170
) {
172171
throw new Error(
173172
"The target environment doesn't support 'import()' so it's not possible to use external type 'import'"
@@ -579,25 +578,6 @@ class ExternalModule extends Module {
579578
canMangle = true;
580579
}
581580
break;
582-
case "module":
583-
if (this.buildInfo.module) {
584-
if (!Array.isArray(request) || request.length === 1) {
585-
this.buildMeta.exportsType = "namespace";
586-
canMangle = true;
587-
}
588-
} else {
589-
this.buildMeta.async = true;
590-
EnvironmentNotSupportAsyncWarning.check(
591-
this,
592-
compilation.runtimeTemplate,
593-
"external module"
594-
);
595-
if (!Array.isArray(request) || request.length === 1) {
596-
this.buildMeta.exportsType = "namespace";
597-
canMangle = false;
598-
}
599-
}
600-
break;
601581
case "script":
602582
this.buildMeta.async = true;
603583
EnvironmentNotSupportAsyncWarning.check(
@@ -614,18 +594,52 @@ class ExternalModule extends Module {
614594
"external promise"
615595
);
616596
break;
597+
case "module":
617598
case "import":
618-
this.buildMeta.async = true;
619-
EnvironmentNotSupportAsyncWarning.check(
620-
this,
621-
compilation.runtimeTemplate,
622-
"external import"
623-
);
624-
if (!Array.isArray(request) || request.length === 1) {
625-
this.buildMeta.exportsType = "namespace";
626-
canMangle = false;
599+
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+
608+
if (type === "module") {
609+
if (this.buildInfo.module) {
610+
if (!Array.isArray(request) || request.length === 1) {
611+
this.buildMeta.exportsType = "namespace";
612+
canMangle = true;
613+
}
614+
} else {
615+
this.buildMeta.async = true;
616+
EnvironmentNotSupportAsyncWarning.check(
617+
this,
618+
compilation.runtimeTemplate,
619+
"external module"
620+
);
621+
if (!Array.isArray(request) || request.length === 1) {
622+
this.buildMeta.exportsType = "namespace";
623+
canMangle = false;
624+
}
625+
}
626+
}
627+
628+
if (type === "import") {
629+
this.buildMeta.async = true;
630+
EnvironmentNotSupportAsyncWarning.check(
631+
this,
632+
compilation.runtimeTemplate,
633+
"external import"
634+
);
635+
if (!Array.isArray(request) || request.length === 1) {
636+
this.buildMeta.exportsType = "namespace";
637+
canMangle = false;
638+
}
627639
}
640+
628641
break;
642+
}
629643
}
630644
this.addDependency(new StaticExportsDependency(true, canMangle));
631645
callback();
@@ -659,36 +673,6 @@ class ExternalModule extends Module {
659673

660674
_getRequestAndExternalType() {
661675
let { request, externalType } = this;
662-
663-
if (externalType === "module-import") {
664-
const dependencyMeta = /** @type {ImportDependencyMeta} */ (
665-
this.dependencyMeta
666-
);
667-
668-
if (dependencyMeta && dependencyMeta.externalType) {
669-
externalType = dependencyMeta.externalType;
670-
} else if (dependencyMeta && dependencyMeta.externalsPresets) {
671-
const presets = dependencyMeta.externalsPresets;
672-
// TODO: what if user set multiple presets?
673-
if (presets.web) {
674-
externalType = "module";
675-
} else if (presets.webAsync) {
676-
externalType = "import";
677-
} else if (
678-
presets.electron ||
679-
presets.electronMain ||
680-
presets.electronPreload ||
681-
presets.electronRenderer ||
682-
presets.node ||
683-
presets.nwjs
684-
) {
685-
externalType = "node-commonjs";
686-
}
687-
} else {
688-
externalType = "commonjs";
689-
}
690-
}
691-
692676
if (typeof request === "object" && !Array.isArray(request))
693677
request = request[externalType];
694678
return { request, externalType };
@@ -753,43 +737,58 @@ class ExternalModule extends Module {
753737
runtimeTemplate
754738
);
755739
}
756-
case "import":
757-
return getSourceForImportExternal(
758-
request,
759-
runtimeTemplate,
760-
/** @type {ImportDependencyMeta} */ (dependencyMeta)
761-
);
762740
case "script":
763741
return getSourceForScriptExternal(request, runtimeTemplate);
764-
case "module": {
765-
if (!(/** @type {BuildInfo} */< 17A6 /span> (this.buildInfo).module)) {
766-
if (!runtimeTemplate.supportsDynamicImport()) {
767-
throw new Error(
768-
`The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script${
769-
runtimeTemplate.supportsEcmaScriptModuleSyntax()
770-
? "\nDid you mean to build a EcmaScript Module ('output.module: true')?"
771-
: ""
772-
}`
773-
);
774-
}
742+
case "module":
743+
case "import":
744+
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+
752+
if (type === "import") {
775753
return getSourceForImportExternal(
776754
request,
777755
runtimeTemplate,
778756
/** @type {ImportDependencyMeta} */ (dependencyMeta)
779757
);
780758
}
781-
if (!runtimeTemplate.supportsEcmaScriptModuleSyntax()) {
782-
throw new Error(
783-
"The target environment doesn't support EcmaScriptModule syntax so it's not possible to use external type 'module'"
759+
760+
if (type === "module") {
761+
if (!(/** @type {BuildInfo} */ (this.buildInfo).module)) {
762+
if (!runtimeTemplate.supportsDynamicImport()) {
763+
throw new Error(
764+
`The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script${
765+
runtimeTemplate.supportsEcmaScriptModuleSyntax()
766+
? "\nDid you mean to build a EcmaScript Module ('output.module: true')?"
767+
: ""
768+
}`
769+
);
770+
}
771+
return getSourceForImportExternal(
772+
request,
773+
runtimeTemplate,
774+
/** @type {ImportDependencyMeta} */ (dependencyMeta)
775+
);
776+
}
777+
if (!runtimeTemplate.supportsEcmaScriptModuleSyntax()) {
778+
throw new Error(
779+
"The target environment doesn't support EcmaScriptModule syntax so it's not possible to use external type 'module'"
780+
);
781+
}
782+
return getSourceForModuleExternal(
783+
request,
784+
moduleGraph.getExportsInfo(this),
785+
runtime,
786+
runtimeTemplate,
787+
/** @type {ImportDependencyMeta} */ (dependencyMeta)
784788
);
785789
}
786-
return getSourceForModuleExternal(
787-
request,
788-
moduleGraph.getExportsInfo(this),
789-
runtime,
790-
runtimeTemplate,
791-
/** @type {ImportDependencyMeta} */ (dependencyMeta)
792-
);
790+
791+
break;
793792
}
794793
case "var":
795794
case "promise":

lib/ExternalModuleFactoryPlugin.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const ImportDependency = require("./dependencies/ImportDependency");
1414
const { resolveByProperty, cachedSetProperty } = require("./util/cleverMerge");
1515

1616
/** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
17-
/** @typedef {import("../declarations/WebpackOptions").ExternalsPresets} ExternalsPresets */
1817
/** @typedef {import("./Compilation").DepConstructor} DepConstructor */
1918
/** @typedef {import("./ExternalModule").DependencyMeta} DependencyMeta */
2019
/** @typedef {import("./Module")} Module */
@@ -61,12 +60,10 @@ class ExternalModuleFactoryPlugin {
6160
/**
6261
* @param {string | undefined} type default external type
6362
* @param {Externals} externals externals config
64-
* @param {ExternalsPresets} [externalsPresets] externals preset config
6563
*/
66-
constructor(type, externals, externalsPresets) {
64+
constructor(type, externals) {
6765
this.type = type;
6866
this.externals = externals;
69-
this.externalsPresets = externalsPresets;
7067
}
7168

7269
/**
@@ -75,7 +72,6 @@ class ExternalModuleFactoryPlugin {
7572
*/
7673
apply(normalModuleFactory) {
7774
const globalType = this.type;
78-
const externalsPresets = this.externalsPresets;
7975
normalModuleFactory.hooks.factorize.tapAsync(
8076
"ExternalModuleFactoryPlugin",
8177
(data, callback) => {
@@ -139,21 +135,14 @@ class ExternalModuleFactoryPlugin {
139135

140136
dependencyMeta = {
141137
attributes: dependency.assertions,
142-
externalType,
143-
externalsPresets
138+
externalType
144139
};
145140
} else if (dependency instanceof CssImportDependency) {
146141
dependencyMeta = {
147142
layer: dependency.layer,
148143
supports: dependency.supports,
149144
media: dependency.media
150145
};
151-
} else {
152-
dependencyMeta = {
153-
attributes: undefined,
154-
externalType: undefined,
155-
externalsPresets
156-
};
157146
}
158147

159148
callback(

lib/ExternalsPlugin.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ class ExternalsPlugin {
2727
*/
2828
apply(compiler) {
2929
compiler.hooks.compile.tap("ExternalsPlugin", ({ normalModuleFactory }) => {
30-
new ExternalModuleFactoryPlugin(
31-
this.type,
32-
this.externals,
33-
compiler.options.externalsPresets
34-
).apply(normalModuleFactory);
30+
new ExternalModuleFactoryPlugin(this.type, this.externals).apply(
31+
normalModuleFactory
32+
);
3533
});
3634
}
3735
}

test/configCases/externals/module-import-externals-presets/index.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/configCases/externals/module-import-externals-presets/test.filter.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

test/configCases/externals/module-import-externals-presets/webpack.config.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

types.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5396,7 +5396,6 @@ type ImportAttributes = Record<string, string> & {};
53965396
declare interface ImportDependencyMeta {
53975397
attributes?: ImportAttributes;
53985398
externalType?: "import" | "module";
5399-
externalsPresets?: ExternalsPresets;
54005399
}
54015400
declare interface ImportModuleOptions {
54025401
/**

0 commit comments

Comments
 (0)
0