Skip to content

Commit

Permalink
fix: more types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Feb 19, 2024
2 parents 5b9f6e4 + 1addd26 commit c8d4b5e
Show file tree
Hide file tree
Showing 27 changed files with 622 additions and 231 deletions.
2 changes: 1 addition & 1 deletion declarations/LoaderContext.d.ts
Expand Up @@ -41,7 +41,7 @@ export interface NormalModuleLoaderContext<OptionsType> {
utils: {
absolutify: (context: string, request: string) => string;
contextify: (context: string, request: string) => string;
createHash: (algorithm?: string) => Hash;
createHash: (algorithm?: string | typeof Hash) => Hash;
};
rootContext: string;
fs: InputFileSystem;
Expand Down
3 changes: 2 additions & 1 deletion lib/ContextModule.js
Expand Up @@ -38,6 +38,7 @@ const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
/** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
/** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
/** @typedef {import("./Module").SourceTypes} SourceTypes */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
Expand Down Expand Up @@ -144,7 +145,7 @@ class ContextModule extends Module {
}

/**
* @returns {Set<string>} types available (do not mutate)
* @returns {SourceTypes} types available (do not mutate)
*/
getSourceTypes() {
return TYPES;
Expand Down
3 changes: 0 additions & 3 deletions lib/CssModule.js
Expand Up @@ -151,9 +151,6 @@ class CssModule extends NormalModule {
return obj;
}

/**
* @param {ObjectDeserializerContext} context context
*/
deserialize(context) {
const { read } = context;
this.cssLayer = read();
Expand Down
3 changes: 2 additions & 1 deletion lib/DelegatedModule.js
Expand Up @@ -25,6 +25,7 @@ const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
/** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
/** @typedef {import("./Module").SourceContext} SourceContext */
/** @typedef {import("./Module").SourceTypes} SourceTypes */
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
Expand Down Expand Up @@ -66,7 +67,7 @@ class DelegatedModule extends Module {
}

/**
* @returns {Set<string>} types available (do not mutate)
* @returns {SourceTypes} types available (do not mutate)
*/
getSourceTypes() {
return TYPES;
Expand Down
3 changes: 2 additions & 1 deletion lib/DllModule.js
Expand Up @@ -22,6 +22,7 @@ const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
/** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
/** @typedef {import("./Module").SourceContext} SourceContext */
/** @typedef {import("./Module").SourceTypes} SourceTypes */
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
Expand Down Expand Up @@ -52,7 +53,7 @@ class DllModule extends Module {
}

/**
* @returns {Set<string>} types available (do not mutate)
* @returns {SourceTypes} types available (do not mutate)
*/
getSourceTypes() {
return TYPES;
Expand Down
3 changes: 2 additions & 1 deletion lib/ExternalModule.js
Expand Up @@ -34,6 +34,7 @@ const { register } = require("./util/serialization");
/** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
/** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
/** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
/** @typedef {import("./Module").SourceTypes} SourceTypes */
/** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
Expand Down Expand Up @@ -415,7 +416,7 @@ class ExternalModule extends Module {
}

/**
* @returns {Set<string>} types available (do not mutate)
* @returns {SourceTypes} types available (do not mutate)
*/
getSourceTypes() {
return this.externalType === "css-import" ? CSS_TYPES : TYPES;
Expand Down
27 changes: 24 additions & 3 deletions lib/Module.js
Expand Up @@ -21,12 +21,14 @@ const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("./ChunkGroup")} ChunkGroup */
/** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
/** @typedef {import("./Compilation")} Compilation */
/** @typedef {import("./Compilation").AssetInfo} AssetInfo */
/** @typedef {import("./ConcatenationScope")} ConcatenationScope */
/** @typedef {import("./Dependency")} Dependency */
/** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
/** @typedef {import("./ExportsInfo").UsageStateType} UsageStateType */
/** @typedef {import("./FileSystemInfo")} FileSystemInfo */
/** @typedef {import("./FileSystemInfo").Snapshot} Snapshot */
/** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
/** @typedef {import("./ModuleTypeConstants").ModuleTypes} ModuleTypes */
/** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
Expand Down Expand Up @@ -100,6 +102,21 @@ const makeSerializable = require("./util/makeSerializable");
* @property {boolean=} sideEffectFree
*/

/**
* @typedef {Object} KnownBuildInfo
* @property {boolean=} cacheable
* @property {boolean=} parsed
* @property {LazySet<string>=} fileDependencies
* @property {LazySet<string>=} contextDependencies
* @property {LazySet<string>=} missingDependencies
* @property {LazySet<string>=} buildDependencies
* @property {(Map<string, string | Set<string>>)=} valueDependencies
* @property {TODO=} hash
* @property {Record<string, Source>=} assets
* @property {Map<string, AssetInfo | undefined>=} assetsInfo
* @property {(Snapshot | null)=} snapshot
*/

/**
* @typedef {Object} NeedBuildContext
* @property {Compilation} compilation
Expand All @@ -108,13 +125,17 @@ const makeSerializable = require("./util/makeSerializable");
*/

/** @typedef {KnownBuildMeta & Record<string, any>} BuildMeta */
/** @typedef {Record<string, any>} BuildInfo */
/** @typedef {KnownBuildInfo & Record<string, any>} BuildInfo */

/**
* @typedef {Object} FactoryMeta
* @property {boolean=} sideEffectFree
*/

/** @typedef {Set<string>} SourceTypes */

/** @typedef {{ factoryMeta: FactoryMeta | undefined, resolveOptions: ResolveOptions | undefined }} UnsafeCacheData */

const EMPTY_RESOLVE_OPTIONS = {};

let debugId = 1000;
Expand Down Expand Up @@ -812,7 +833,7 @@ class Module extends DependenciesBlock {

/**
* @abstract
* @returns {Set<string>} types available (do not mutate)
* @returns {SourceTypes} types available (do not mutate)
*/
getSourceTypes() {
// Better override this method to return the correct types
Expand Down Expand Up @@ -956,7 +977,7 @@ class Module extends DependenciesBlock {
/**
* Module should be unsafe cached. Get data that's needed for that.
* This data will be passed to restoreFromUnsafeCache later.
* @returns {object} cached data
* @returns {UnsafeCacheData} cached data
*/
getUnsafeCacheData() {
return {
Expand Down
46 changes: 36 additions & 10 deletions lib/MultiCompiler.js
Expand Up @@ -19,6 +19,7 @@ const ArrayQueue = require("./util/ArrayQueue");
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./Stats")} Stats */
/** @typedef {import("./Watching")} Watching */
/** @typedef {import("./logging/Logger").Logger} Logger */
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
/** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */
/** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
Expand Down Expand Up @@ -80,7 +81,7 @@ module.exports = class MultiCompiler {
this.dependencies = new WeakMap();
this.running = false;

/** @type {Stats[]} */
/** @type {(Stats | null)[]} */
const compilerStats = this.compilers.map(() => null);
let doneCompilers = 0;
for (let index = 0; index < this.compilers.length; index++) {
Expand All @@ -94,7 +95,9 @@ module.exports = class MultiCompiler {
}
compilerStats[compilerIndex] = stats;
if (doneCompilers === this.compilers.length) {
this.hooks.done.call(new MultiStats(compilerStats));
this.hooks.done.call(
new MultiStats(/** @type {Stats[]} */ (compilerStats))
);
}
});
compiler.hooks.invalid.tap("MultiCompiler", () => {
Expand Down Expand Up @@ -180,6 +183,10 @@ module.exports = class MultiCompiler {
}
}

/**
* @param {string | (function(): string)} name name of the logger, or function called once to get the logger name
* @returns {Logger} a logger with that name
*/
getInfrastructureLogger(name) {
return this.compilers[0].getInfrastructureLogger(name);
}
Expand All @@ -202,6 +209,10 @@ module.exports = class MultiCompiler {
const edges = new Set();
/** @type {string[]} */
const missing = [];
/**
* @param {Compiler} compiler compiler
* @returns {boolean} target was found
*/
const targetFound = compiler => {
for (const edge of edges) {
if (edge.target === compiler) {
Expand Down Expand Up @@ -274,6 +285,10 @@ module.exports = class MultiCompiler {
runWithDependencies(compilers, fn, callback) {
const fulfilledNames = new Set();
let remainingCompilers = compilers;
/**
* @param {string} d dependency
* @returns {boolean} when dependency was fulfilled
*/
const isDependencyFulfilled = d => fulfilledNames.has(d);
const getReadyCompilers = () => {
let readyCompilers = [];
Expand All @@ -291,6 +306,10 @@ module.exports = class MultiCompiler {
}
return readyCompilers;
};
/**
* @param {Callback<MultiStats>} callback callback
* @returns {void}
*/
const runCompilers = callback => {
if (remainingCompilers.length === 0) return callback();
asyncLib.map(
Expand All @@ -302,7 +321,7 @@ module.exports = class MultiCompiler {
runCompilers(callback);
});
},
callback
/** @type {Callback<TODO>} */ (callback)
);
};
runCompilers(callback);
Expand All @@ -316,7 +335,7 @@ module.exports = class MultiCompiler {
* @returns {SetupResult[]} result of setup
*/
_runGraph(setup, run, callback) {
/** @typedef {{ compiler: Compiler, setupResult: SetupResult, result: Stats, state: "pending" | "blocked" | "queued" | "starting" | "running" | "running-outdated" | "done", children: Node[], parents: Node[] }} Node */
/** @typedef {{ compiler: Compiler, setupResult: undefined | SetupResult, result: undefined | Stats, state: "pending" | "blocked" | "queued" | "starting" | "running" | "running-outdated" | "done", children: Node[], parents: Node[] }} Node */

// State transitions for nodes:
// -> blocked (initial)
Expand All @@ -341,12 +360,14 @@ module.exports = class MultiCompiler {
}));
/** @type {Map<string, Node>} */
const compilerToNode = new Map();
for (const node of nodes) compilerToNode.set(node.compiler.name, node);
for (const node of nodes) {
compilerToNode.set(/** @type {string} */ (node.compiler.name), node);
}
for (const node of nodes) {
const dependencies = this.dependencies.get(node.compiler);
if (!dependencies) continue;
for (const dep of dependencies) {
const parent = compilerToNode.get(dep);
const parent = /** @type {Node} */ (compilerToNode.get(dep));
node.parents.push(parent);
parent.children.push(node);
}
Expand All @@ -361,10 +382,10 @@ module.exports = class MultiCompiler {
}
let errored = false;
let running = 0;
const parallelism = this._options.parallelism;
const parallelism = /** @type {number} */ (this._options.parallelism);
/**
* @param {Node} node node
* @param {Error=} err error
* @param {(Error | null)=} err error
* @param {Stats=} stats result
* @returns {void}
*/
Expand Down Expand Up @@ -440,6 +461,7 @@ module.exports = class MultiCompiler {
}
};

/** @type {SetupResult[]} */
const setupResults = [];
nodes.forEach((node, i) => {
setupResults.push(
Expand All @@ -461,15 +483,19 @@ module.exports = class MultiCompiler {
};
const processQueueWorker = () => {
while (running < parallelism && queue.length > 0 && !errored) {
const node = queue.dequeue();
const node = /** @type {Node} */ (queue.dequeue());
if (
node.state === "queued" ||
(node.state === "blocked" &&
node.parents.every(p => p.state === "done"))
) {
running++;
node.state = "starting";
run(node.compiler, node.setupResult, nodeDone.bind(null, node));
run(
node.compiler,
/** @type {SetupResult} */ (node.setupResult),
nodeDone.bind(null, node)
);
node.state = "running";
}
}
Expand Down

0 comments on commit c8d4b5e

Please sign in to comment.