Skip to content

Commit

Permalink
refactor(types): more
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 21, 2023
1 parent d9d64b5 commit 3f71468
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 79 deletions.
2 changes: 1 addition & 1 deletion lib/APIPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const GetFullHashRuntimeModule = require("./runtime/GetFullHashRuntimeModule");
/**
* @param {boolean} module true if ES module
* @param {string} importMetaName `import.meta` name
* @returns {Record<string, {expr: string, req: string[], type?: string, assign: boolean}>} replacements
* @returns {Record<string, {expr: string, req: string[] | null, type?: string, assign: boolean}>} replacements
*/
function getReplacements(module, importMetaName) {
return {
Expand Down
4 changes: 2 additions & 2 deletions lib/AsyncDependenciesBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const makeSerializable = require("./util/makeSerializable");

class AsyncDependenciesBlock extends DependenciesBlock {
/**
* @param {ChunkGroupOptions & { entryOptions?: EntryOptions }} groupOptions options for the group
* @param {DependencyLocation=} loc the line of code
* @param {(ChunkGroupOptions & { entryOptions?: EntryOptions }) | null} groupOptions options for the group
* @param {(DependencyLocation | null)=} loc the line of code
* @param {(string | null)=} request the request
*/
constructor(groupOptions, loc, request) {
Expand Down
8 changes: 4 additions & 4 deletions lib/ChunkGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class ChunkGraphModule {
this.entryInChunks = undefined;
/** @type {Set<Chunk> | undefined} */
this.runtimeInChunks = undefined;
/** @type {RuntimeSpecMap<ModuleHashInfo>} */
/** @type {RuntimeSpecMap<ModuleHashInfo> | undefined} */
this.hashes = undefined;
/** @type {string | number} */
this.id = null;
Expand Down Expand Up @@ -1388,7 +1388,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
*/
hasModuleHashes(module, runtime) {
const cgm = this._getChunkGraphModule(module);
const hashes = cgm.hashes;
const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
return hashes && hashes.has(runtime);
}

Expand All @@ -1399,7 +1399,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
*/
getModuleHash(module, runtime) {
const cgm = this._getChunkGraphModule(module);
const hashes = cgm.hashes;
const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
return this._getModuleHashInfo(module, hashes, runtime).hash;
}

Expand All @@ -1410,7 +1410,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
*/
getRenderedModuleHash(module, runtime) {
const cgm = this._getChunkGraphModule(module);
const hashes = cgm.hashes;
const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
return this._getModuleHashInfo(module, hashes, runtime).renderedHash;
}

Expand Down
12 changes: 9 additions & 3 deletions lib/ChunkGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,15 @@ class ChunkGroup {
*/
addOptions(options) {
for (const key of Object.keys(options)) {
if (this.options[key] === undefined) {
this.options[key] = options[key];
} else if (this.options[key] !== options[key]) {
if (
this.options[/** @type {keyof ChunkGroupOptions} */ (key)] === undefined
) {
this.options[key] =
options[/** @type {keyof ChunkGroupOptions} */ (key)];
} else if (
this.options[/** @type {keyof ChunkGroupOptions} */ (key)] !==
options[/** @type {keyof ChunkGroupOptions} */ (key)]
) {
if (key.endsWith("Order")) {
this.options[key] = Math.max(this.options[key], options[key]);
} else {
Expand Down
13 changes: 13 additions & 0 deletions lib/ContextReplacementPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
const ContextElementDependency = require("./dependencies/ContextElementDependency");
const { join } = require("./util/fs");

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

class ContextReplacementPlugin {
/**
* @param {RegExp} resourceRegExp A regular expression that determines which files will be selected
* @param {TODO=} newContentResource A new resource to replace the match
* @param {TODO=} newContentRecursive If true, all subdirectories are searched for matches
* @param {TODO=} newContentRegExp A regular expression that determines which files will be selected
*/
constructor(
resourceRegExp,
newContentResource,
Expand Down Expand Up @@ -49,6 +57,11 @@ class ContextReplacementPlugin {
}
}

/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const resourceRegExp = this.resourceRegExp;
const newContentCallback = this.newContentCallback;
Expand Down
14 changes: 13 additions & 1 deletion lib/DllReferencePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const makePathsRelative = require("./util/identifier").makePathsRelative;
/** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
/** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */
/** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsManifest} DllReferencePluginOptionsManifest */
/** @typedef {import("./Compiler")} Compiler */

const validate = createSchemaValidation(
require("../schemas/plugins/DllReferencePlugin.check.js"),
Expand All @@ -37,6 +38,11 @@ class DllReferencePlugin {
this._compilationData = new WeakMap();
}

/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"DllReferencePlugin",
Expand Down Expand Up @@ -140,7 +146,9 @@ class DllReferencePlugin {
// If there was an error parsing the manifest file, add the
// error as a compilation error to make the compilation fail.
if (data.error) {
compilation.errors.push(data.error);
compilation.errors.push(
/** @type {DllManifestError} */ (data.error)
);
}
compilation.fileDependencies.add(manifest);
}
Expand All @@ -151,6 +159,10 @@ class DllReferencePlugin {
}

class DllManifestError extends WebpackError {
/**
* @param {string} filename filename of the manifest
* @param {string} message error message
*/
constructor(filename, message) {
super();

Expand Down
4 changes: 3 additions & 1 deletion lib/FlagDependencyUsagePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ class FlagDependencyUsagePlugin {
}

while (queue.length) {
const [module, runtime] = queue.dequeue();
const [module, runtime] = /** @type {[Module, RuntimeSpec]} */ (
queue.dequeue()
);
processModule(module, runtime, false);
}
logger.timeEnd("trace exports usage in graph");
Expand Down
Loading

0 comments on commit 3f71468

Please sign in to comment.