Skip to content

Commit

Permalink
Merge pull request #17272 from webpack/more-types
Browse files Browse the repository at this point in the history
refactor(types): more
  • Loading branch information
TheLarkInn committed May 31, 2023
2 parents 6d8d96b + 1df0148 commit 16660c1
Show file tree
Hide file tree
Showing 35 changed files with 283 additions and 128 deletions.
2 changes: 1 addition & 1 deletion lib/ContextModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = class ContextModuleFactory extends ModuleFactory {

/**
* @param {ModuleFactoryCreateData} data data object
* @param {function(Error=, ModuleFactoryResult=): void} callback callback
* @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
* @returns {void}
*/
create(data, callback) {
Expand Down
2 changes: 1 addition & 1 deletion lib/DllModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DllModuleFactory extends ModuleFactory {
}
/**
* @param {ModuleFactoryCreateData} data data object
* @param {function(Error=, ModuleFactoryResult=): void} callback callback
* @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
* @returns {void}
*/
create(data, callback) {
Expand Down
2 changes: 1 addition & 1 deletion lib/HookWebpackError.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const tryRunOrWebpackError = (fn, hook) => {
if (err instanceof WebpackError) {
throw err;
}
throw new HookWebpackError(err, hook);
throw new HookWebpackError(/** @type {Error} */ (err), hook);
}
return r;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/IgnoreErrorModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IgnoreErrorModuleFactory extends ModuleFactory {

/**
* @param {ModuleFactoryCreateData} data data object
* @param {function(Error=, ModuleFactoryResult=): void} callback callback
* @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
* @returns {void}
*/
create(data, callback) {
Expand Down
30 changes: 21 additions & 9 deletions lib/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ const deprecatedNeedRebuild = util.deprecate(

class Module extends DependenciesBlock {
/**
* @param {ModuleTypes} type the module type, when deserializing the type is not known and is an empty string
* @param {string=} context an optional context
* @param {string=} layer an optional layer in which the module is
* @param {ModuleTypes | ""} type the module type, when deserializing the type is not known and is an empty string
* @param {(string | null)=} context an optional context
* @param {(string | null)=} layer an optional layer in which the module is
*/
constructor(type, context = null, layer = null) {
super();
Expand All @@ -151,7 +151,7 @@ class Module extends DependenciesBlock {
this.debugId = debugId++;

// Info from Factory
/** @type {ResolveOptions} */
/** @type {ResolveOptions | undefined} */
this.resolveOptions = EMPTY_RESOLVE_OPTIONS;
/** @type {object | undefined} */
this.factoryMeta = undefined;
Expand All @@ -167,9 +167,9 @@ class Module extends DependenciesBlock {
this._warnings = undefined;
/** @type {WebpackError[] | undefined} */
this._errors = undefined;
/** @type {BuildMeta} */
/** @type {BuildMeta | undefined} */
this.buildMeta = undefined;
/** @type {Record<string, any>} */
/** @type {Record<string, any> | undefined} */
this.buildInfo = undefined;
/** @type {Dependency[] | undefined} */
this.presentationalDependencies = undefined;
Expand Down Expand Up @@ -331,6 +331,10 @@ class Module extends DependenciesBlock {
);
}

/**
* @param {Chunk} chunk the chunk
* @returns {boolean} true, when the module was added
*/
addChunk(chunk) {
const chunkGraph = ChunkGraph.getChunkGraphForModule(
this,
Expand All @@ -342,6 +346,10 @@ class Module extends DependenciesBlock {
return true;
}

/**
* @param {Chunk} chunk the chunk
* @returns {void}
*/
removeChunk(chunk) {
return ChunkGraph.getChunkGraphForModule(
this,
Expand All @@ -350,6 +358,10 @@ class Module extends DependenciesBlock {
).disconnectChunkAndModule(chunk, this);
}

/**
* @param {Chunk} chunk the chunk
* @returns {boolean} true, when the module is in the chunk
*/
isInChunk(chunk) {
return ChunkGraph.getChunkGraphForModule(
this,
Expand Down Expand Up @@ -435,7 +447,7 @@ class Module extends DependenciesBlock {
case "namespace":
return "namespace";
case "default":
switch (this.buildMeta.defaultObject) {
switch (/** @type {BuildMeta} */ (this.buildMeta).defaultObject) {
case "redirect":
return "default-with-named";
case "redirect-warn":
Expand All @@ -447,7 +459,7 @@ class Module extends DependenciesBlock {
if (strict) return "default-with-named";
// Try to figure out value of __esModule by following reexports
const handleDefault = () => {
switch (this.buildMeta.defaultObject) {
switch (/** @type {BuildMeta} */ (this.buildMeta).defaultObject) {
case "redirect":
case "redirect-warn":
return "default-with-named";
Expand Down Expand Up @@ -664,7 +676,7 @@ class Module extends DependenciesBlock {
] of moduleGraph.getIncomingConnectionsByOriginModule(this)) {
if (!connections.some(c => c.isTargetActive(chunk.runtime))) continue;
for (const originChunk of chunkGraph.getModuleChunksIterable(
fromModule
/** @type {Module} */ (fromModule)
)) {
// return true if module this is not reachable from originChunk when ignoring chunk
if (!this.isAccessibleInChunk(chunkGraph, originChunk, chunk))
Expand Down
2 changes: 1 addition & 1 deletion lib/ModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ModuleFactory {
/**
* @abstract
* @param {ModuleFactoryCreateData} data data object
* @param {function(Error=, ModuleFactoryResult=): void} callback callback
* @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
* @returns {void}
*/
create(data, callback) {
Expand Down
2 changes: 1 addition & 1 deletion lib/NormalModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ class NormalModuleFactory extends ModuleFactory {

/**
* @param {ModuleFactoryCreateData} data data object
* @param {function(Error=, ModuleFactoryResult=): void} callback callback
* @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
* @returns {void}
*/
create(data, callback) {
Expand Down
2 changes: 1 addition & 1 deletion lib/NullFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ModuleFactory = require("./ModuleFactory");
class NullFactory extends ModuleFactory {
/**
* @param {ModuleFactoryCreateData} data data object
* @param {function(Error=, ModuleFactoryResult=): void} callback callback
* @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
* @returns {void}
*/
create(data, callback) {
Expand Down
4 changes: 4 additions & 0 deletions lib/container/ContainerEntryModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ class ContainerEntryModule extends Module {
super.serialize(context);
}

/**
* @param {ObjectDeserializerContext} context context
* @returns {ContainerEntryModule} deserialized container entry module
*/
static deserialize(context) {
const { read } = context;
const obj = new ContainerEntryModule(read(), read(), read());
Expand Down
2 changes: 1 addition & 1 deletion lib/container/ContainerEntryModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ContainerEntryModule = require("./ContainerEntryModule");
module.exports = class ContainerEntryModuleFactory extends ModuleFactory {
/**
* @param {ModuleFactoryCreateData} data data object
* @param {function(Error=, ModuleFactoryResult=): void} callback callback
* @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
* @returns {void}
*/
create({ dependencies: [dependency] }, callback) {
Expand Down
7 changes: 7 additions & 0 deletions lib/container/FallbackDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const makeSerializable = require("../util/makeSerializable");
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */

class FallbackDependency extends Dependency {
/**
* @param {string[]} requests requests
*/
constructor(requests) {
super();
this.requests = requests;
Expand Down Expand Up @@ -41,6 +44,10 @@ class FallbackDependency extends Dependency {
super.serialize(context);
}

/**
* @param {ObjectDeserializerContext} context context
* @returns {FallbackDependency} deserialize fallback dependency
*/
static deserialize(context) {
const { read } = context;
const obj = new FallbackDependency(read());
Expand Down
3 changes: 3 additions & 0 deletions lib/container/FallbackItemDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const ModuleDependency = require("../dependencies/ModuleDependency");
const makeSerializable = require("../util/makeSerializable");

class FallbackItemDependency extends ModuleDependency {
/**
* @param {string} request request
*/
constructor(request) {
super(request);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/container/FallbackModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ class FallbackModule extends Module {
super.serialize(context);
}

/**
* @param {ObjectDeserializerContext} context context
* @returns {FallbackModule} deserialized fallback module
*/
static deserialize(context) {
const { read } = context;
const obj = new FallbackModule(read());
Expand Down
2 changes: 1 addition & 1 deletion lib/container/FallbackModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const FallbackModule = require("./FallbackModule");
module.exports = class FallbackModuleFactory extends ModuleFactory {
/**
* @param {ModuleFactoryCreateData} data data object
* @param {function(Error=, ModuleFactoryResult=): void} callback callback
* @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
* @returns {void}
*/
create({ dependencies: [dependency] }, callback) {
Expand Down
4 changes: 4 additions & 0 deletions lib/container/RemoteModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ class RemoteModule extends Module {
super.serialize(context);
}

/**
* @param {ObjectDeserializerContext} context context
* @returns {RemoteModule} deserialized module
*/
static deserialize(context) {
const { read } = context;
const obj = new RemoteModule(read(), read(), read(), read());
Expand Down
1 change: 1 addition & 0 deletions lib/container/RemoteRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const RuntimeGlobals = require("../RuntimeGlobals");
const RuntimeModule = require("../RuntimeModule");
const Template = require("../Template");

/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("./RemoteModule")} RemoteModule */

class RemoteRuntimeModule extends RuntimeModule {
Expand Down
3 changes: 3 additions & 0 deletions lib/container/RemoteToExternalDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const ModuleDependency = require("../dependencies/ModuleDependency");
const makeSerializable = require("../util/makeSerializable");

class RemoteToExternalDependency extends ModuleDependency {
/**
* @param {string} request request
*/
constructor(request) {
super(request);
}
Expand Down
16 changes: 12 additions & 4 deletions lib/esm/ModuleChunkFormatPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const {
} = require("../javascript/JavascriptModulesPlugin");
const { updateHashForEntryStartup } = require("../javascript/StartupHelpers");

/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Entrypoint")} Entrypoint */

class ModuleChunkFormatPlugin {
/**
Expand Down Expand Up @@ -73,7 +75,9 @@ class ModuleChunkFormatPlugin {
chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
);
if (entries.length > 0) {
const runtimeChunk = entries[0][1].getRuntimeChunk();
const runtimeChunk =
/** @type {Entrypoint[][]} */
(entries)[0][1].getRuntimeChunk();
const currentOutputName = compilation
.getPath(
getChunkFilenameTemplate(chunk, compilation.outputOptions),
Expand All @@ -87,6 +91,10 @@ class ModuleChunkFormatPlugin {
// remove filename, we only need the directory
currentOutputName.pop();

/**
* @param {Chunk} chunk the chunk
* @returns {string} the relative path
*/
const getRelativePath = chunk => {
const baseOutputName = currentOutputName.slice();
const chunkOutputName = compilation
Expand Down Expand Up @@ -124,7 +132,7 @@ class ModuleChunkFormatPlugin {
entrySource.add(";\n\n// load runtime\n");
entrySource.add(
`import ${RuntimeGlobals.require} from ${JSON.stringify(
getRelativePath(runtimeChunk)
getRelativePath(/** @type {Chunk} */ (runtimeChunk))
)};\n`
);

Expand All @@ -143,8 +151,8 @@ class ModuleChunkFormatPlugin {
const final = i + 1 === entries.length;
const moduleId = chunkGraph.getModuleId(module);
const chunks = getAllChunks(
entrypoint,
runtimeChunk,
/** @type {Entrypoint} */ (entrypoint),
/** @type {Chunk} */ (runtimeChunk),
undefined
);
for (const chunk of chunks) {
Expand Down
9 changes: 9 additions & 0 deletions lib/esm/ModuleChunkLoadingPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const RuntimeGlobals = require("../RuntimeGlobals");
const ExportWebpackRequireRuntimeModule = require("./ExportWebpackRequireRuntimeModule");
const ModuleChunkLoadingRuntimeModule = require("./ModuleChunkLoadingRuntimeModule");

/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */

class ModuleChunkLoadingPlugin {
Expand All @@ -22,6 +23,10 @@ class ModuleChunkLoadingPlugin {
"ModuleChunkLoadingPlugin",
compilation => {
const globalChunkLoading = compilation.outputOptions.chunkLoading;
/**
* @param {Chunk} chunk chunk to check
* @returns {boolean} true, when the plugin is enabled for the chunk
*/
const isEnabledForChunk = chunk => {
const options = chunk.getEntryOptions();
const chunkLoading =
Expand All @@ -31,6 +36,10 @@ class ModuleChunkLoadingPlugin {
return chunkLoading === "import";
};
const onceForChunkSet = new WeakSet();
/**
* @param {Chunk} chunk chunk to check
* @param {Set<string>} set runtime requirements
*/
const handler = (chunk, set) => {
if (onceForChunkSet.has(chunk)) return;
onceForChunkSet.add(chunk);
Expand Down
18 changes: 10 additions & 8 deletions lib/esm/ModuleChunkLoadingRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const compileBooleanMatcher = require("../util/compileBooleanMatcher");
const { getUndoPath } = require("../util/identifier");

/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../ChunkGraph")} ChunkGraph */

/**
* @typedef {Object} JsonpCompilationPluginHooks
Expand Down Expand Up @@ -69,11 +70,10 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
if (options && options.baseUri) {
return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`;
}
const compilation = /** @type {Compilation} */ (this.compilation);
const {
compilation: {
outputOptions: { importMetaName }
}
} = this;
outputOptions: { importMetaName }
} = compilation;
return `${RuntimeGlobals.baseURI} = new URL(${JSON.stringify(
rootOutputDir
)}, ${importMetaName}.url);`;
Expand All @@ -83,7 +83,9 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
* @returns {string} runtime code
*/
generate() {
const { compilation, chunk, chunkGraph } = this;
const compilation = /** @type {Compilation} */ (this.compilation);
const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
const chunk = /** @type {Chunk} */ (this.chunk);
const {
runtimeTemplate,
outputOptions: { importFunctionName }
Expand All @@ -106,16 +108,16 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
const hasJsMatcher = compileBooleanMatcher(conditionMap);
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);

const outputName = this.compilation.getPath(
getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
const outputName = compilation.getPath(
getChunkFilenameTemplate(chunk, compilation.outputOptions),
{
chunk,
contentHashType: "javascript"
}
);
const rootOutputDir = getUndoPath(
outputName,
/** @type {string} */ (this.compilation.outputOptions.path),
/** @type {string} */ (compilation.outputOptions.path),
true
);

Expand Down

0 comments on commit 16660c1

Please sign in to comment.