Skip to content

Commit 9d76fff

Browse files
authored
refactor: add Module.getSourceBasicTypes for basic JS type detection (#20546)
1 parent a3d7839 commit 9d76fff

8 files changed

Lines changed: 86 additions & 19 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Add `Module.getSourceBasicTypes` to distinguish basic source types and clarify how modules with non-basic source types like `remote` still produce JavaScript output.

lib/Module.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ const makeSerializable = require("./util/makeSerializable");
8080
/** @typedef {KnownSourceType | string} SourceType */
8181
/** @typedef {ReadonlySet<SourceType>} SourceTypes */
8282

83+
/** @typedef {ReadonlySet<typeof JAVASCRIPT_TYPE | string>} BasicSourceTypes */
84+
8385
// TODO webpack 6: compilation will be required in CodeGenerationContext
8486
/**
8587
* @typedef {object} CodeGenerationContext
@@ -966,6 +968,19 @@ class Module extends DependenciesBlock {
966968
return JAVASCRIPT_TYPES;
967969
}
968970

971+
/**
972+
* Basic source types are high-level categories like javascript, css, webassembly, etc.
973+
* We only have built-in knowledge about the javascript basic type here; other basic types may be
974+
* added or changed over time by generators and do not need to be handled or detected here.
975+
*
976+
* Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
977+
* from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
978+
* @returns {BasicSourceTypes} types available (do not mutate)
979+
*/
980+
getSourceBasicTypes() {
981+
return this.getSourceTypes();
982+
}
983+
969984
/**
970985
* @abstract
971986
* @deprecated Use codeGeneration() instead

lib/RuntimeModule.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
const { RawSource } = require("webpack-sources");
99
const OriginalSource = require("webpack-sources").OriginalSource;
1010
const Module = require("./Module");
11-
const { RUNTIME_TYPES } = require("./ModuleSourceTypeConstants");
11+
const {
12+
JAVASCRIPT_TYPES,
13+
RUNTIME_TYPES
14+
} = require("./ModuleSourceTypeConstants");
1215
const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
1316

1417
/** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
@@ -29,6 +32,7 @@ const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
2932
/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
3033
/** @typedef {import("./util/Hash")} Hash */
3134
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
35+
/** @typedef {import("./Module").BasicSourceTypes} BasicSourceTypes */
3236

3337
class RuntimeModule extends Module {
3438
/**
@@ -138,6 +142,19 @@ class RuntimeModule extends Module {
138142
return RUNTIME_TYPES;
139143
}
140144

145+
/**
146+
* Basic source types are high-level categories like javascript, css, webassembly, etc.
147+
* We only have built-in knowledge about the javascript basic type here; other basic types may be
148+
* added or changed over time by generators and do not need to be handled or detected here.
149+
*
150+
* Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
151+
* from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
152+
* @returns {BasicSourceTypes} types available (do not mutate)
153+
*/
154+
getSourceBasicTypes() {
155+
return JAVASCRIPT_TYPES;
156+
}
157+
141158
/**
142159
* @param {CodeGenerationContext} context context for code generation
143160
* @returns {CodeGenerationResult} result

lib/container/RemoteModule.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
const { RawSource } = require("webpack-sources");
99
const Module = require("../Module");
10-
const { REMOTE_AND_SHARE_INIT_TYPES } = require("../ModuleSourceTypeConstants");
10+
const {
11+
JAVASCRIPT_TYPES,
12+
REMOTE_AND_SHARE_INIT_TYPES
13+
} = require("../ModuleSourceTypeConstants");
1114
const { WEBPACK_MODULE_TYPE_REMOTE } = require("../ModuleTypeConstants");
1215
const RuntimeGlobals = require("../RuntimeGlobals");
1316
const makeSerializable = require("../util/makeSerializable");
@@ -34,6 +37,7 @@ const RemoteToExternalDependency = require("./RemoteToExternalDependency");
3437
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
3538
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
3639
/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
40+
/** @typedef {import("../Module").BasicSourceTypes} BasicSourceTypes */
3741

3842
const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
3943

@@ -137,6 +141,19 @@ class RemoteModule extends Module {
137141
return REMOTE_AND_SHARE_INIT_TYPES;
138142
}
139143

144+
/**
145+
* Basic source types are high-level categories like javascript, css, webassembly, etc.
146+
* We only have built-in knowledge about the javascript basic type here; other basic types may be
147+
* added or changed over time by generators and do not need to be handled or detected here.
148+
*
149+
* Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
150+
* from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
151+
* @returns {BasicSourceTypes} types available (do not mutate)
152+
*/
153+
getSourceBasicTypes() {
154+
return JAVASCRIPT_TYPES;
155+
}
156+
140157
/**
141158
* @param {ModuleGraph} moduleGraph the module graph
142159
* @param {boolean | undefined} strict the importing module is strict

lib/dependencies/HarmonyImportSideEffectDependency.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"use strict";
77

8-
const { CSS_TYPE } = require("../ModuleSourceTypeConstants");
8+
const { JAVASCRIPT_TYPE } = require("../ModuleSourceTypeConstants");
99
const makeSerializable = require("../util/makeSerializable");
1010
const HarmonyImportDependency = require("./HarmonyImportDependency");
1111

@@ -62,14 +62,6 @@ makeSerializable(
6262
"webpack/lib/dependencies/HarmonyImportSideEffectDependency"
6363
);
6464

65-
/**
66-
* @param {string[]} sourceTypes the source type of referencing module
67-
* @returns {boolean} returns if need to render executing js code
68-
*/
69-
function noNeedJs(sourceTypes) {
70-
return sourceTypes.every((sourceType) => sourceType === CSS_TYPE);
71-
}
72-
7365
HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDependencyTemplate extends (
7466
HarmonyImportDependency.Template
7567
) {
@@ -84,7 +76,7 @@ HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDepend
8476

8577
const module = /** @type {Module} */ (moduleGraph.getModule(dependency));
8678

87-
if (module && noNeedJs([...module.getSourceTypes()])) {
79+
if (module && !module.getSourceBasicTypes().has(JAVASCRIPT_TYPE)) {
8880
// no need to render import
8981
return;
9082
}

lib/optimize/ConcatenatedModule.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const ConcatenationScope = require("../ConcatenationScope");
1717
const { UsageState } = require("../ExportsInfo");
1818
const Module = require("../Module");
1919
const {
20-
CSS_TYPE,
2120
JAVASCRIPT_TYPE,
2221
JAVASCRIPT_TYPES
2322
} = require("../ModuleSourceTypeConstants");
@@ -1005,11 +1004,7 @@ class ConcatenatedModule extends Module {
10051004
if (!(connection.dependency instanceof HarmonyImportDependency)) {
10061005
return false;
10071006
}
1008-
if (
1009-
[...connection.module.getSourceTypes()].every(
1010-
(sourceType) => sourceType === CSS_TYPE
1011-
)
1012-
) {
1007+
if (!connection.module.getSourceBasicTypes().has(JAVASCRIPT_TYPE)) {
10131008
return false;
10141009
}
10151010
return (

lib/sharing/ConsumeSharedModule.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
const { RawSource } = require("webpack-sources");
99
const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
1010
const Module = require("../Module");
11-
const { CONSUME_SHARED_TYPES } = require("../ModuleSourceTypeConstants");
11+
const {
12+
CONSUME_SHARED_TYPES,
13+
JAVASCRIPT_TYPES
14+
} = require("../ModuleSourceTypeConstants");
1215
const {
1316
WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE
1417
} = require("../ModuleTypeConstants");
@@ -41,6 +44,7 @@ const fallbackModuleCache = new WeakMap();
4144
/** @typedef {import("../util/Hash")} Hash */
4245
/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
4346
/** @typedef {import("../util/semver").SemVerRange} SemVerRange */
47+
/** @typedef {import("../Module").BasicSourceTypes} BasicSourceTypes */
4448

4549
/**
4650
* @typedef {object} ConsumeOptions
@@ -159,6 +163,19 @@ class ConsumeSharedModule extends Module {
159163
return CONSUME_SHARED_TYPES;
160164
}
161165

166+
/**
167+
* Basic source types are high-level categories like javascript, css, webassembly, etc.
168+
* We only have built-in knowledge about the javascript basic type here; other basic types may be
169+
* added or changed over time by generators and do not need to be handled or detected here.
170+
*
171+
* Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
172+
* from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
173+
* @returns {BasicSourceTypes} types available (do not mutate)
174+
*/
175+
getSourceBasicTypes() {
176+
return JAVASCRIPT_TYPES;
177+
}
178+
162179
/**
163180
* @param {ModuleGraph} moduleGraph the module graph
164181
* @returns {Module | null} fallback module

types.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10646,6 +10646,15 @@ declare class Module extends DependenciesBlock {
1064610646
callback: (err?: WebpackError) => void
1064710647
): void;
1064810648
getSourceTypes(): ReadonlySet<string>;
10649+
10650+
/**
10651+
* Basic source types are high-level categories like javascript, css, webassembly, etc.
10652+
* We only have built-in knowledge about the javascript basic type here; other basic types may be
10653+
* added or changed over time by generators and do not need to be handled or detected here.
10654+
* Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
10655+
* from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
10656+
*/
10657+
getSourceBasicTypes(): ReadonlySet<string>;
1064910658
source(
1065010659
dependencyTemplates: DependencyTemplates,
1065110660
runtimeTemplate: RuntimeTemplate,

0 commit comments

Comments
 (0)