Skip to content

Commit 45e8873

Browse files
authored
Merge pull request #15397 from webpack/add-normal-module-types
add NormalModuleFactory hooks types
2 parents 9d56890 + c09d869 commit 45e8873

3 files changed

Lines changed: 166 additions & 95 deletions

File tree

lib/NormalModule.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const memoize = require("./util/memoize");
5050
/** @typedef {import("webpack-sources").Source} Source */
5151
/** @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext} NormalModuleLoaderContext */
5252
/** @typedef {import("../declarations/WebpackOptions").Mode} Mode */
53+
/** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
5354
/** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
5455
/** @typedef {import("./ChunkGraph")} ChunkGraph */
5556
/** @typedef {import("./Compiler")} Compiler */
@@ -194,6 +195,25 @@ makeSerializable(
194195
* @property {AsyncSeriesBailHook<[NormalModule, NeedBuildContext], boolean>} needBuild
195196
*/
196197

198+
/**
199+
* @typedef {Object} NormalModuleCreateData
200+
* @property {string=} layer an optional layer in which the module is
201+
* @property {string} type module type
202+
* @property {string} request request string
203+
* @property {string} userRequest request intended by user (without loaders from config)
204+
* @property {string} rawRequest request without resolving
205+
* @property {LoaderItem[]} loaders list of loaders
206+
* @property {string} resource path + query of the real resource
207+
* @property {Record<string, any>=} resourceResolveData resource resolve data
208+
* @property {string} context context directory for resolving
209+
* @property {string=} matchResource path + query of the matched resource (virtual)
210+
* @property {Parser} parser the parser used
211+
* @property {Record<string, any>=} parserOptions the options of the parser used
212+
* @property {Generator} generator the generator used
213+
* @property {Record<string, any>=} generatorOptions the options of the generator used
214+
* @property {ResolveOptions=} resolveOptions options used for resolving requests from this module
215+
*/
216+
197217
/** @type {WeakMap<Compilation, NormalModuleCompilationHooks>} */
198218
const compilationHooksMap = new WeakMap();
199219

@@ -246,22 +266,7 @@ class NormalModule extends Module {
246266
}
247267

248268
/**
249-
* @param {Object} options options object
250-
* @param {string=} options.layer an optional layer in which the module is
251-
* @param {string} options.type module type
252-
* @param {string} options.request request string
253-
* @param {string} options.userRequest request intended by user (without loaders from config)
254-
* @param {string} options.rawRequest request without resolving
255-
* @param {LoaderItem[]} options.loaders list of loaders
256-
* @param {string} options.resource path + query of the real resource
257-
* @param {Record<string, any>=} options.resourceResolveData resource resolve data
258-
* @param {string} options.context context directory for resolving
259-
* @param {string | undefined} options.matchResource path + query of the matched resource (virtual)
260-
* @param {Parser} options.parser the parser used
261-
* @param {object} options.parserOptions the options of the parser used
262-
* @param {Generator} options.generator the generator used
263-
* @param {object} options.generatorOptions the options of the generator used
264-
* @param {Object} options.resolveOptions options used for resolving requests from this module
269+
* @param {NormalModuleCreateData} options options object
265270
*/
266271
constructor({
267272
layer,

lib/NormalModuleFactory.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ const {
3434
} = require("./util/identifier");
3535

3636
/** @typedef {import("../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
37+
/** @typedef {import("../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
3738
/** @typedef {import("./Generator")} Generator */
3839
/** @typedef {import("./ModuleFactory").ModuleFactoryCreateData} ModuleFactoryCreateData */
3940
/** @typedef {import("./ModuleFactory").ModuleFactoryResult} ModuleFactoryResult */
41+
/** @typedef {import("./NormalModule").NormalModuleCreateData} NormalModuleCreateData */
4042
/** @typedef {import("./Parser")} Parser */
4143
/** @typedef {import("./ResolverFactory")} ResolverFactory */
4244
/** @typedef {import("./dependencies/ModuleDependency")} ModuleDependency */
4345
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
4446

47+
/** @typedef {Pick<RuleSetRule, 'type'|'sideEffects'|'parser'|'generator'|'resolve'|'layer'>} ModuleSettings */
48+
/** @typedef {Partial<NormalModuleCreateData & {settings: ModuleSettings}>} CreateData */
49+
4550
/**
4651
* @typedef {Object} ResolveData
4752
* @property {ModuleFactoryCreateData["contextInfo"]} contextInfo
@@ -51,7 +56,7 @@ const {
5156
* @property {Record<string, any> | undefined} assertions
5257
* @property {ModuleDependency[]} dependencies
5358
* @property {string} dependencyType
54-
* @property {Object} createData
59+
* @property {CreateData} createData
5560
* @property {LazySet<string>} fileDependencies
5661
* @property {LazySet<string>} missingDependencies
5762
* @property {LazySet<string>} contextDependencies
@@ -199,7 +204,7 @@ class NormalModuleFactory extends ModuleFactory {
199204
}) {
200205
super();
201206
this.hooks = Object.freeze({
202-
/** @type {AsyncSeriesBailHook<[ResolveData], TODO>} */
207+
/** @type {AsyncSeriesBailHook<[ResolveData], Module | false | void>} */
203208
resolve: new AsyncSeriesBailHook(["resolveData"]),
204209
/** @type {HookMap<AsyncSeriesBailHook<[ResourceDataWithData, ResolveData], true | void>>} */
205210
resolveForScheme: new HookMap(
@@ -209,15 +214,15 @@ class NormalModuleFactory extends ModuleFactory {
209214
resolveInScheme: new HookMap(
210215
() => new AsyncSeriesBailHook(["resourceData", "resolveData"])
211216
),
212-
/** @type {AsyncSeriesBailHook<[ResolveData], TODO>} */
217+
/** @type {AsyncSeriesBailHook<[ResolveData], Module>} */
213218
factorize: new AsyncSeriesBailHook(["resolveData"]),
214-
/** @type {AsyncSeriesBailHook<[ResolveData], TODO>} */
219+
/** @type {AsyncSeriesBailHook<[ResolveData], false | void>} */
215220
beforeResolve: new AsyncSeriesBailHook(["resolveData"]),
216-
/** @type {AsyncSeriesBailHook<[ResolveData], TODO>} */
221+
/** @type {AsyncSeriesBailHook<[ResolveData], false | void>} */
217222
afterResolve: new AsyncSeriesBailHook(["resolveData"]),
218-
/** @type {AsyncSeriesBailHook<[ResolveData["createData"], ResolveData], TODO>} */
223+
/** @type {AsyncSeriesBailHook<[ResolveData["createData"], ResolveData], Module | void>} */
219224
createModule: new AsyncSeriesBailHook(["createData", "resolveData"]),
220-
/** @type {SyncWaterfallHook<[Module, ResolveData["createData"], ResolveData], TODO>} */
225+
/** @type {SyncWaterfallHook<[Module, ResolveData["createData"], ResolveData], Module>} */
221226
module: new SyncWaterfallHook(["module", "createData", "resolveData"]),
222227
createParser: new HookMap(() => new SyncBailHook(["parserOptions"])),
223228
parser: new HookMap(() => new SyncHook(["parser", "parserOptions"])),
@@ -301,7 +306,9 @@ class NormalModuleFactory extends ModuleFactory {
301306
return callback(new Error("Empty dependency (no request)"));
302307
}
303308

304-
createdModule = new NormalModule(createData);
309+
createdModule = new NormalModule(
310+
/** @type {NormalModuleCreateData} */ (createData)
311+
);
305312
}
306313

307314
createdModule = this.hooks.module.call(

types.d.ts

Lines changed: 130 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7251,6 +7251,37 @@ declare interface ModuleReferenceOptions {
72517251
*/
72527252
asiSafe?: boolean;
72537253
}
7254+
declare interface ModuleSettings {
7255+
/**
7256+
* Specifies the layer in which the module should be placed in.
7257+
*/
7258+
layer?: string;
7259+
7260+
/**
7261+
* Module type to use for the module.
7262+
*/
7263+
type?: string;
7264+
7265+
/**
7266+
* Options for the resolver.
7267+
*/
7268+
resolve?: ResolveOptionsWebpackOptions;
7269+
7270+
/**
7271+
* Options for parsing.
7272+
*/
7273+
parser?: { [index: string]: any };
7274+
7275+
/**
7276+
* The options for the module generator.
7277+
*/
7278+
generator?: { [index: string]: any };
7279+
7280+
/**
7281+
* Flags a module as with or without side effects.
7282+
*/
7283+
sideEffects?: boolean;
7284+
}
72547285
declare abstract class ModuleTemplate {
72557286
type: string;
72567287
hooks: Readonly<{
@@ -7502,76 +7533,15 @@ declare class NodeTemplatePlugin {
75027533
}
75037534
type NodeWebpackOptions = false | NodeOptions;
75047535
declare class NormalModule extends Module {
7505-
constructor(__0: {
7506-
/**
7507-
* an optional layer in which the module is
7508-
*/
7509-
layer?: string;
7510-
/**
7511-
* module type
7512-
*/
7513-
type: string;
7514-
/**
7515-
* request string
7516-
*/
7517-
request: string;
7518-
/**
7519-
* request intended by user (without loaders from config)
7520-
*/
7521-
userRequest: string;
7522-
/**
7523-
* request without resolving
7524-
*/
7525-
rawRequest: string;
7526-
/**
7527-
* list of loaders
7528-
*/
7529-
loaders: LoaderItem[];
7530-
/**
7531-
* path + query of the real resource
7532-
*/
7533-
resource: string;
7534-
/**
7535-
* resource resolve data
7536-
*/
7537-
resourceResolveData?: Record<string, any>;
7538-
/**
7539-
* context directory for resolving
7540-
*/
7541-
context: string;
7542-
/**
7543-
* path + query of the matched resource (virtual)
7544-
*/
7545-
matchResource?: string;
7546-
/**
7547-
* the parser used
7548-
*/
7549-
parser: Parser;
7550-
/**
7551-
* the options of the parser used
7552-
*/
7553-
parserOptions: object;
7554-
/**
7555-
* the generator used
7556-
*/
7557-
generator: Generator;
7558-
/**
7559-
* the options of the generator used
7560-
*/
7561-
generatorOptions: object;
7562-
/**
7563-
* options used for resolving requests from this module
7564-
*/
7565-
resolveOptions: Object;
7566-
});
7536+
constructor(__0: NormalModuleCreateData);
75677537
request: string;
75687538
userRequest: string;
75697539
rawRequest: string;
75707540
binary: boolean;
75717541
parser: Parser;
7572-
parserOptions: object;
7542+
parserOptions?: Record<string, any>;
75737543
generator: Generator;
7574-
generatorOptions: object;
7544+
generatorOptions?: Record<string, any>;
75757545
resource: string;
75767546
resourceResolveData?: Record<string, any>;
75777547
matchResource?: string;
@@ -7614,20 +7584,109 @@ declare interface NormalModuleCompilationHooks {
76147584
readResource: HookMap<AsyncSeriesBailHook<[object], string | Buffer>>;
76157585
needBuild: AsyncSeriesBailHook<[NormalModule, NeedBuildContext], boolean>;
76167586
}
7587+
declare interface NormalModuleCreateData {
7588+
/**
7589+
* an optional layer in which the module is
7590+
*/
7591+
layer?: string;
7592+
7593+
/**
7594+
* module type
7595+
*/
7596+
type: string;
7597+
7598+
/**
7599+
* request string
7600+
*/
7601+
request: string;
7602+
7603+
/**
7604+
* request intended by user (without loaders from config)
7605+
*/
7606+
userRequest: string;
7607+
7608+
/**
7609+
* request without resolving
7610+
*/
7611+
rawRequest: string;
7612+
7613+
/**
7614+
* list of loaders
7615+
*/
7616+
loaders: LoaderItem[];
7617+
7618+
/**
7619+
* path + query of the real resource
7620+
*/
7621+
resource: string;
7622+
7623+
/**
7624+
* resource resolve data
7625+
*/
7626+
resourceResolveData?: Record<string, any>;
7627+
7628+
/**
7629+
* context directory for resolving
7630+
*/
7631+
context: string;
7632+
7633+
/**
7634+
* path + query of the matched resource (virtual)
7635+
*/
7636+
matchResource?: string;
7637+
7638+
/**
7639+
* the parser used
7640+
*/
7641+
parser: Parser;
7642+
7643+
/**
7644+
* the options of the parser used
7645+
*/
7646+
parserOptions?: Record<string, any>;
7647+
7648+
/**
7649+
* the generator used
7650+
*/
7651+
generator: Generator;
7652+
7653+
/**
7654+
* the options of the generator used
7655+
*/
7656+
generatorOptions?: Record<string, any>;
7657+
7658+
/**
7659+
* options used for resolving requests from this module
7660+
*/
7661+
resolveOptions?: ResolveOptionsWebpackOptions;
7662+
}
76177663
declare abstract class NormalModuleFactory extends ModuleFactory {
76187664
hooks: Readonly<{
7619-
resolve: AsyncSeriesBailHook<[ResolveData], any>;
7665+
resolve: AsyncSeriesBailHook<[ResolveData], false | void | Module>;
76207666
resolveForScheme: HookMap<
76217667
AsyncSeriesBailHook<[ResourceDataWithData, ResolveData], true | void>
76227668
>;
76237669
resolveInScheme: HookMap<
76247670
AsyncSeriesBailHook<[ResourceDataWithData, ResolveData], true | void>
76257671
>;
7626-
factorize: AsyncSeriesBailHook<[ResolveData], any>;
7627-
beforeResolve: AsyncSeriesBailHook<[ResolveData], any>;
7628-
afterResolve: AsyncSeriesBailHook<[ResolveData], any>;
7629-
createModule: AsyncSeriesBailHook<[Object, ResolveData], any>;
7630-
module: SyncWaterfallHook<[Module, Object, ResolveData], any>;
7672+
factorize: AsyncSeriesBailHook<[ResolveData], Module>;
7673+
beforeResolve: AsyncSeriesBailHook<[ResolveData], false | void>;
7674+
afterResolve: AsyncSeriesBailHook<[ResolveData], false | void>;
7675+
createModule: AsyncSeriesBailHook<
7676+
[
7677+
Partial<NormalModuleCreateData & { settings: ModuleSettings }>,
7678+
ResolveData
7679+
],
7680+
void | Module
7681+
>;
7682+
module: SyncWaterfallHook<
7683+
[
7684+
Module,
7685+
Partial<NormalModuleCreateData & { settings: ModuleSettings }>,
7686+
ResolveData
7687+
],
7688+
Module
7689+
>;
76317690
createParser: HookMap<SyncBailHook<any, any>>;
76327691
parser: HookMap<SyncHook<any>>;
76337692
createGenerator: HookMap<SyncBailHook<any, any>>;
@@ -9429,7 +9488,7 @@ declare interface ResolveData {
94299488
assertions?: Record<string, any>;
94309489
dependencies: ModuleDependency[];
94319490
dependencyType: string;
9432-
createData: Object;
9491+
createData: Partial<NormalModuleCreateData & { settings: ModuleSettings }>;
94339492
fileDependencies: LazySet<string>;
94349493
missingDependencies: LazySet<string>;
94359494
contextDependencies: LazySet<string>;

0 commit comments

Comments
 (0)