fix: types

This commit is contained in:
alexander.akait 2024-02-21 17:55:02 +03:00
parent 6cd0ddbf26
commit a1f46a9ac3
13 changed files with 63 additions and 19 deletions

View File

@ -119,6 +119,7 @@ if (!cli.installed) {
console.error(notify); console.error(notify);
/** @type {string | undefined} */
let packageManager; let packageManager;
if (fs.existsSync(path.resolve(process.cwd(), "yarn.lock"))) { if (fs.existsSync(path.resolve(process.cwd(), "yarn.lock"))) {
@ -171,7 +172,10 @@ if (!cli.installed) {
}')...` }')...`
); );
runCommand(packageManager, installOptions.concat(cli.package)) runCommand(
/** @type {string} */ (packageManager),
installOptions.concat(cli.package)
)
.then(() => { .then(() => {
runCli(cli); runCli(cli);
}) })

View File

@ -70,7 +70,7 @@ const sortOrigin = (a, b) => {
class ChunkGroup { class ChunkGroup {
/** /**
* Creates an instance of ChunkGroup. * Creates an instance of ChunkGroup.
* @param {string|ChunkGroupOptions=} options chunk group options passed to chunkGroup * @param {string | ChunkGroupOptions=} options chunk group options passed to chunkGroup
*/ */
constructor(options) { constructor(options) {
if (typeof options === "string") { if (typeof options === "string") {
@ -219,7 +219,7 @@ class ChunkGroup {
/** /**
* @param {Chunk} oldChunk chunk to be replaced * @param {Chunk} oldChunk chunk to be replaced
* @param {Chunk} newChunk New chunk that will be replaced with * @param {Chunk} newChunk New chunk that will be replaced with
* @returns {boolean} returns true if the replacement was successful * @returns {boolean | undefined} returns true if the replacement was successful
*/ */
replaceChunk(oldChunk, newChunk) { replaceChunk(oldChunk, newChunk) {
const oldIdx = this.chunks.indexOf(oldChunk); const oldIdx = this.chunks.indexOf(oldChunk);

View File

@ -15,6 +15,7 @@ const processAsyncTree = require("./util/processAsyncTree");
/** @typedef {import("../declarations/WebpackOptions").CleanOptions} CleanOptions */ /** @typedef {import("../declarations/WebpackOptions").CleanOptions} CleanOptions */
/** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./logging/Logger").Logger} Logger */ /** @typedef {import("./logging/Logger").Logger} Logger */
/** @typedef {import("./util/fs").IStats} IStats */
/** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */ /** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
/** @typedef {import("./util/fs").StatsCallback} StatsCallback */ /** @typedef {import("./util/fs").StatsCallback} StatsCallback */
@ -196,7 +197,7 @@ const applyDiff = (fs, outputPath, dry, logger, diff, isKept, callback) => {
} }
doStat(fs, path, (err, stats) => { doStat(fs, path, (err, stats) => {
if (err) return handleError(err); if (err) return handleError(err);
if (!stats.isDirectory()) { if (!(/** @type {IStats} */ (stats).isDirectory())) {
push({ push({
type: "unlink", type: "unlink",
filename, filename,

View File

@ -2731,7 +2731,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
const { modules, moduleMemCaches } = this; const { modules, moduleMemCaches } = this;
this.hooks.finishModules.callAsync(modules, err => { this.hooks.finishModules.callAsync(modules, err => {
this.logger.timeEnd("finish modules"); this.logger.timeEnd("finish modules");
if (err) return callback(err); if (err) return callback(/** @type {WebpackError} */ (err));
// extract warnings and errors from modules // extract warnings and errors from modules
this.moduleGraph.freeze("dependency errors"); this.moduleGraph.freeze("dependency errors");

View File

@ -43,6 +43,7 @@ const { isSourceEqual } = require("./util/source");
/** @typedef {import("./Dependency")} Dependency */ /** @typedef {import("./Dependency")} Dependency */
/** @typedef {import("./FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */ /** @typedef {import("./FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */
/** @typedef {import("./Module")} Module */ /** @typedef {import("./Module")} Module */
/** @typedef {import("./logging/createConsoleLogger").LoggingFunction} LoggingFunction */
/** @typedef {import("./util/WeakTupleMap")} WeakTupleMap */ /** @typedef {import("./util/WeakTupleMap")} WeakTupleMap */
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
/** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */ /** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */
@ -253,7 +254,7 @@ class Compiler {
/** @type {ResolverFactory} */ /** @type {ResolverFactory} */
this.resolverFactory = new ResolverFactory(); this.resolverFactory = new ResolverFactory();
/** @type {Logger | undefined} */ /** @type {LoggingFunction | undefined} */
this.infrastructureLogger = undefined; this.infrastructureLogger = undefined;
this.options = options; this.options = options;

View File

@ -41,7 +41,10 @@ class EntryOptionPlugin {
name, name,
desc desc
); );
for (const entry of desc.import) { const descImport =
/** @type {Exclude<EntryDescription["import"], undefined>} */
(desc.import);
for (const entry of descImport) {
new EntryPlugin(context, entry, options).apply(compiler); new EntryPlugin(context, entry, options).apply(compiler);
} }
} }

View File

@ -59,7 +59,12 @@ class EntryPlugin {
static createDependency(entry, options) { static createDependency(entry, options) {
const dep = new EntryDependency(entry); const dep = new EntryDependency(entry);
// TODO webpack 6 remove string option // TODO webpack 6 remove string option
dep.loc = { name: typeof options === "object" ? options.name : options }; dep.loc = {
name:
typeof options === "object"
? /** @type {string} */ (options.name)
: options
};
return dep; return dep;
} }
} }

View File

@ -89,7 +89,7 @@ class Entrypoint extends ChunkGroup {
/** /**
* @param {Chunk} oldChunk chunk to be replaced * @param {Chunk} oldChunk chunk to be replaced
* @param {Chunk} newChunk New chunk that will be replaced with * @param {Chunk} newChunk New chunk that will be replaced with
* @returns {boolean} returns true if the replacement was successful * @returns {boolean | undefined} returns true if the replacement was successful
*/ */
replaceChunk(oldChunk, newChunk) { replaceChunk(oldChunk, newChunk) {
if (this._runtimeChunk === oldChunk) this._runtimeChunk = newChunk; if (this._runtimeChunk === oldChunk) this._runtimeChunk = newChunk;

View File

@ -336,7 +336,7 @@ module.exports = class MultiCompiler {
}); });
}, },
(err, results) => { (err, results) => {
callback(err, /** @type {MultiStats | undefined} */ (results)); callback(err, /** @type {TODO} */ (results));
} }
); );
}; };

View File

@ -12,19 +12,24 @@ const Chunk = require("./Chunk");
const Module = require("./Module"); const Module = require("./Module");
const { parseResource } = require("./util/identifier"); const { parseResource } = require("./util/identifier");
/** @typedef {import("./ChunkGraph")} ChunkGraph */
/** @typedef {import("./Compilation").AssetInfo} AssetInfo */ /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
/** @typedef {import("./Compilation").PathData} PathData */ /** @typedef {import("./Compilation").PathData} PathData */
/** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Compiler")} Compiler */
const REGEXP = /\[\\*([\w:]+)\\*\]/gi; const REGEXP = /\[\\*([\w:]+)\\*\]/gi;
/**
* @param {string | number} id id
* @returns {string | number} result
*/
const prepareId = id => { const prepareId = id => {
if (typeof id !== "string") return id; if (typeof id !== "string") return id;
if (/^"\s\+*.*\+\s*"$/.test(id)) { if (/^"\s\+*.*\+\s*"$/.test(id)) {
const match = /^"\s\+*\s*(.*)\s*\+\s*"$/.exec(id); const match = /^"\s\+*\s*(.*)\s*\+\s*"$/.exec(id);
return `" + (${match[1]} + "").replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_") + "`; return `" + (${/** @type {string[]} */ (match)[1]} + "").replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_") + "`;
} }
return id.replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_"); return id.replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_");
@ -255,13 +260,18 @@ const replacePathVariables = (path, data, assetInfo) => {
const idReplacer = replacer(() => const idReplacer = replacer(() =>
prepareId( prepareId(
module instanceof Module ? chunkGraph.getModuleId(module) : module.id module instanceof Module
? /** @type {ChunkGraph} */ (chunkGraph).getModuleId(module)
: module.id
) )
); );
const moduleHashReplacer = hashLength( const moduleHashReplacer = hashLength(
replacer(() => replacer(() =>
module instanceof Module module instanceof Module
? chunkGraph.getRenderedModuleHash(module, data.runtime) ? /** @type {ChunkGraph} */ (chunkGraph).getRenderedModuleHash(
module,
data.runtime
)
: module.hash : module.hash
), ),
"hashWithLength" in module ? module.hashWithLength : undefined, "hashWithLength" in module ? module.hashWithLength : undefined,
@ -269,7 +279,7 @@ const replacePathVariables = (path, data, assetInfo) => {
"modulehash" "modulehash"
); );
const contentHashReplacer = hashLength( const contentHashReplacer = hashLength(
replacer(data.contentHash), replacer(/** @type {string} */ (data.contentHash)),
undefined, undefined,
assetInfo, assetInfo,
"contenthash" "contenthash"
@ -300,7 +310,7 @@ const replacePathVariables = (path, data, assetInfo) => {
if (typeof data.runtime === "string") { if (typeof data.runtime === "string") {
replacements.set( replacements.set(
"runtime", "runtime",
replacer(() => prepareId(data.runtime)) replacer(() => prepareId(/** @type {string} */ (data.runtime)))
); );
} else { } else {
replacements.set("runtime", replacer("_")); replacements.set("runtime", replacer("_"));

View File

@ -12,6 +12,7 @@ const { LogType } = require("./Logger");
/** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */ /** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
/** @typedef {function(string): boolean} FilterFunction */ /** @typedef {function(string): boolean} FilterFunction */
/** @typedef {function(string, LogTypeEnum, any[]): void} LoggingFunction */
/** /**
* @typedef {Object} LoggerConsole * @typedef {Object} LoggerConsole
@ -76,7 +77,7 @@ const LogLevel = {
/** /**
* @param {LoggerOptions} options options object * @param {LoggerOptions} options options object
* @returns {function(string, LogTypeEnum, any[]): void} logging function * @returns {LoggingFunction} logging function
*/ */
module.exports = ({ level = "info", debug = false, console }) => { module.exports = ({ level = "info", debug = false, console }) => {
const debugFilters = const debugFilters =

View File

@ -156,7 +156,11 @@ const webpack = /** @type {WebpackFunctionSingle & WebpackFunctionMulti} */ (
} else { } else {
compiler.run((err, stats) => { compiler.run((err, stats) => {
compiler.close(err2 => { compiler.close(err2 => {
callback(err || err2, stats); callback(
err || err2,
/** @type {options extends WebpackOptions ? Stats : MultiStats} */
(stats)
);
}); });
}); });
} }

19
types.d.ts vendored
View File

@ -1266,7 +1266,7 @@ declare abstract class ChunkGroup {
* add a chunk into ChunkGroup. Is pushed on or prepended * add a chunk into ChunkGroup. Is pushed on or prepended
*/ */
pushChunk(chunk: Chunk): boolean; pushChunk(chunk: Chunk): boolean;
replaceChunk(oldChunk: Chunk, newChunk: Chunk): boolean; replaceChunk(oldChunk: Chunk, newChunk: Chunk): undefined | boolean;
removeChunk(chunk: Chunk): boolean; removeChunk(chunk: Chunk): boolean;
isInitial(): boolean; isInitial(): boolean;
addChild(group: ChunkGroup): boolean; addChild(group: ChunkGroup): boolean;
@ -2266,7 +2266,7 @@ declare class Compiler {
>; >;
fsStartTime?: number; fsStartTime?: number;
resolverFactory: ResolverFactory; resolverFactory: ResolverFactory;
infrastructureLogger?: WebpackLogger; infrastructureLogger?: (arg0: string, arg1: LogTypeEnum, arg2: any[]) => void;
options: WebpackOptionsNormalized; options: WebpackOptionsNormalized;
context: string; context: string;
requestShortener: RequestShortener; requestShortener: RequestShortener;
@ -7478,6 +7478,21 @@ declare interface LogEntry {
time: number; time: number;
trace?: string[]; trace?: string[];
} }
type LogTypeEnum =
| "error"
| "warn"
| "info"
| "log"
| "debug"
| "profile"
| "trace"
| "group"
| "groupCollapsed"
| "groupEnd"
| "profileEnd"
| "time"
| "clear"
| "status";
declare const MEASURE_END_OPERATION: unique symbol; declare const MEASURE_END_OPERATION: unique symbol;
declare const MEASURE_START_OPERATION: unique symbol; declare const MEASURE_START_OPERATION: unique symbol;
declare interface MainRenderContext { declare interface MainRenderContext {