Skip to content

Commit 20eeaeb

Browse files
authored
fix: add a static getSourceBasicTypes to prevent errors across multiple version (#20614)
1 parent 915293e commit 20eeaeb

5 files changed

Lines changed: 79 additions & 3 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 a static getSourceBasicTypes method to the Module class to prevent errors across multiple versions.

lib/Module.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,21 @@ class Module extends DependenciesBlock {
12211221
this.codeGenerationDependencies = read();
12221222
super.deserialize(context);
12231223
}
1224+
1225+
// TODO remove in webpack 6
1226+
/**
1227+
* @deprecated In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
1228+
* @param {Module} module the module
1229+
* @returns {ReturnType<Module["getSourceBasicTypes"]>} the source types of the module
1230+
*/
1231+
static getSourceBasicTypes(module) {
1232+
if (!(module instanceof Module)) {
1233+
// https://github.com/webpack/webpack/issues/20597
1234+
// fallback to javascript
1235+
return JAVASCRIPT_TYPES;
1236+
}
1237+
return module.getSourceBasicTypes();
1238+
}
12241239
}
12251240

12261241
makeSerializable(Module, "webpack/lib/Module");

lib/dependencies/HarmonyImportSideEffectDependency.js

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

66
"use strict";
77

8+
const Module = require("../Module");
89
const { JAVASCRIPT_TYPE } = require("../ModuleSourceTypeConstants");
910
const makeSerializable = require("../util/makeSerializable");
1011
const HarmonyImportDependency = require("./HarmonyImportDependency");
@@ -13,7 +14,6 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
1314
/** @typedef {import("../Dependency")} Dependency */
1415
/** @typedef {import("../Dependency").GetConditionFn} GetConditionFn */
1516
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
16-
/** @typedef {import("../Module")} Module */
1717
/** @typedef {import("../ModuleGraph")} ModuleGraph */
1818
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
1919
/** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
@@ -76,7 +76,7 @@ HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDepend
7676

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

79-
if (module && !module.getSourceBasicTypes().has(JAVASCRIPT_TYPE)) {
79+
if (module && !Module.getSourceBasicTypes(module).has(JAVASCRIPT_TYPE)) {
8080
// no need to render import
8181
return;
8282
}

lib/optimize/ConcatenatedModule.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,9 @@ class ConcatenatedModule extends Module {
10031003
if (!(connection.dependency instanceof HarmonyImportDependency)) {
10041004
return false;
10051005
}
1006-
if (!connection.module.getSourceBasicTypes().has(JAVASCRIPT_TYPE)) {
1006+
if (
1007+
!Module.getSourceBasicTypes(connection.module).has(JAVASCRIPT_TYPE)
1008+
) {
10071009
return false;
10081010
}
10091011
return (

types.d.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,6 +3830,12 @@ declare class CssLoadingRuntimeModule extends RuntimeModule {
38303830
* Runtime modules which trigger actions on bootstrap
38313831
*/
38323832
static STAGE_TRIGGER: number;
3833+
3834+
/**
3835+
* In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
3836+
* @deprecated
3837+
*/
3838+
static getSourceBasicTypes(module: Module): ReadonlySet<string>;
38333839
}
38343840
declare interface CssLoadingRuntimeModulePluginHooks {
38353841
createStylesheet: SyncWaterfallHook<[string, Chunk], string>;
@@ -5709,6 +5715,12 @@ declare class ExternalModule extends Module {
57095715
static getExternalModuleNodeCommonjsInitFragment: (
57105716
runtimeTemplate: RuntimeTemplate
57115717
) => InitFragment<ChunkRenderContextJavascriptModulesPlugin>;
5718+
5719+
/**
5720+
* In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
5721+
* @deprecated
5722+
*/
5723+
static getSourceBasicTypes(module: Module): ReadonlySet<string>;
57125724
}
57135725
declare interface ExternalModuleInfo {
57145726
type: "external";
@@ -6448,6 +6460,12 @@ declare class GetChunkFilenameRuntimeModule extends RuntimeModule {
64486460
* Runtime modules which trigger actions on bootstrap
64496461
*/
64506462
static STAGE_TRIGGER: number;
6463+
6464+
/**
6465+
* In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
6466+
* @deprecated
6467+
*/
6468+
static getSourceBasicTypes(module: Module): ReadonlySet<string>;
64516469
}
64526470
declare interface GotHandler<T> {
64536471
(result: T, callback: () => void): void;
@@ -8956,6 +8974,12 @@ declare class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
89568974
* Runtime modules which trigger actions on bootstrap
89578975
*/
89588976
static STAGE_TRIGGER: number;
8977+
8978+
/**
8979+
* In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
8980+
* @deprecated
8981+
*/
8982+
static getSourceBasicTypes(module: Module): ReadonlySet<string>;
89598983
}
89608984
declare interface JsonpCompilationPluginHooks {
89618985
linkPreload: SyncWaterfallHook<[string, Chunk], string>;
@@ -9920,6 +9944,12 @@ declare class LoadScriptRuntimeModule extends HelperRuntimeModule {
99209944
* Runtime modules which trigger actions on bootstrap
99219945
*/
99229946
static STAGE_TRIGGER: number;
9947+
9948+
/**
9949+
* In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
9950+
* @deprecated
9951+
*/
9952+
static getSourceBasicTypes(module: Module): ReadonlySet<string>;
99239953
}
99249954

99259955
/**
@@ -10789,6 +10819,12 @@ declare class Module extends DependenciesBlock {
1078910819
get errors(): any;
1079010820
get warnings(): any;
1079110821
used: any;
10822+
10823+
/**
10824+
* In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
10825+
* @deprecated
10826+
*/
10827+
static getSourceBasicTypes(module: Module): ReadonlySet<string>;
1079210828
}
1079310829
declare class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
1079410830
constructor(runtimeRequirements: ReadonlySet<string>);
@@ -10815,6 +10851,12 @@ declare class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
1081510851
* Runtime modules which trigger actions on bootstrap
1081610852
*/
1081710853
static STAGE_TRIGGER: number;
10854+
10855+
/**
10856+
* In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
10857+
* @deprecated
10858+
*/
10859+
static getSourceBasicTypes(module: Module): ReadonlySet<string>;
1081810860
}
1081910861
declare class ModuleConcatenationPlugin {
1082010862
constructor();
@@ -11936,6 +11978,12 @@ declare class NormalModule extends Module {
1193611978
compilation: Compilation
1193711979
): NormalModuleCompilationHooks;
1193811980
static deserialize(context: ObjectDeserializerContext): NormalModule;
11981+
11982+
/**
11983+
* In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
11984+
* @deprecated
11985+
*/
11986+
static getSourceBasicTypes(module: Module): ReadonlySet<string>;
1193911987
}
1194011988
declare interface NormalModuleCompilationHooks {
1194111989
loader: SyncHook<[AnyLoaderContext, NormalModule]>;
@@ -16610,6 +16658,12 @@ declare class RuntimeModule extends Module {
1661016658
* Runtime modules which trigger actions on bootstrap
1661116659
*/
1661216660
static STAGE_TRIGGER: number;
16661+
16662+
/**
16663+
* In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
16664+
* @deprecated
16665+
*/
16666+
static getSourceBasicTypes(module: Module): ReadonlySet<string>;
1661316667
}
1661416668
declare interface RuntimeRequirementsContext {
1661516669
/**

0 commit comments

Comments
 (0)