Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 1, 2024
1 parent b3f89a3 commit 636b5c5
Show file tree
Hide file tree
Showing 31 changed files with 489 additions and 221 deletions.
23 changes: 23 additions & 0 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,26 @@ type RecursiveArrayOrRecord<T> =
| { [index: string]: RecursiveArrayOrRecord<T> }
| Array<RecursiveArrayOrRecord<T>>
| T;

declare module "acorn-import-attributes" {
export function importAttributesOrAssertions(BaseParser: typeof import("acorn").Parser): typeof import("acorn").Parser;
}

declare module "loader-runner" {
export function getContext(resource: string) : string;
export function runLoaders(options: any, callback: (err: Error | null, result: any) => void): void;
}

declare module "watchpack" {
class Watchpack {
aggregatedChanges: Set<string>;
aggregatedRemovals: Set<string>;
constructor(options: import("./declarations/WebpackOptions").WatchOptions);
once(eventName: string, callback: any): void;
watch(options: any): void;
collectTimeInfoEntries(fileTimeInfoEntries: Map<string, number>, contextTimeInfoEntries: Map<string, number>): void;
pause(): void;
close(): void;
}
export = Watchpack;
}
8 changes: 4 additions & 4 deletions declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3480,19 +3480,19 @@ export interface OutputNormalized {
/**
* List of chunk loading types enabled for use by entry points.
*/
enabledChunkLoadingTypes?: EnabledChunkLoadingTypes;
enabledChunkLoadingTypes: EnabledChunkLoadingTypes;
/**
* List of library types enabled for use by entry points.
*/
enabledLibraryTypes?: EnabledLibraryTypes;
enabledLibraryTypes: EnabledLibraryTypes;
/**
* List of wasm loading types enabled for use by entry points.
*/
enabledWasmLoadingTypes?: EnabledWasmLoadingTypes;
enabledWasmLoadingTypes: EnabledWasmLoadingTypes;
/**
* The abilities of the environment where the webpack generated code should run.
*/
environment?: Environment;
environment: Environment;
/**
* Specifies the filename of output files on disk. You must **not** specify an absolute path here, but the path may contain folders separated by '/'! The specified path is joined with the value of the 'output.path' option to determine the location on disk.
*/
Expand Down
51 changes: 29 additions & 22 deletions lib/DynamicEntryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const EntryOptionPlugin = require("./EntryOptionPlugin");
const EntryPlugin = require("./EntryPlugin");
const EntryDependency = require("./dependencies/EntryDependency");

/** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescriptionNormalized */
/** @typedef {import("../declarations/WebpackOptions").EntryDynamicNormalized} EntryDynamic */
/** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
/** @typedef {import("../declarations/WebpackOptions").EntryStaticNormalized} EntryStatic */
Expand Down Expand Up @@ -40,22 +41,27 @@ class DynamicEntryPlugin {
}
);

compiler.hooks.make.tapPromise(
"DynamicEntryPlugin",
(compilation, callback) =>
Promise.resolve(this.entry())
.then(entry => {
const promises = [];
for (const name of Object.keys(entry)) {
const desc = entry[name];
const options = EntryOptionPlugin.entryDescriptionToOptions(
compiler,
name,
desc
);
for (const entry of desc.import) {
promises.push(
new Promise((resolve, reject) => {
compiler.hooks.make.tapPromise("DynamicEntryPlugin", compilation =>
Promise.resolve(this.entry())
.then(entry => {
const promises = [];
for (const name of Object.keys(entry)) {
const desc = entry[name];
const options = EntryOptionPlugin.entryDescriptionToOptions(
compiler,
name,
desc
);
for (const entry of /** @type {NonNullable<EntryDescriptionNormalized["import"]>} */ (
desc.import
)) {
promises.push(
new Promise(
/**
* @param {(value?: any) => void} resolve resolve
* @param {(reason?: Error) => void} reject reject
*/
(resolve, reject) => {
compilation.addEntry(
this.context,
EntryPlugin.createDependency(entry, options),
Expand All @@ -65,13 +71,14 @@ class DynamicEntryPlugin {
resolve();
}
);
})
);
}
}
)
);
}
return Promise.all(promises);
})
.then(x => {})
}
return Promise.all(promises);
})
.then(x => {})
);
}
}
Expand Down
20 changes: 12 additions & 8 deletions lib/FileSystemInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3631,8 +3631,7 @@ class FileSystemInfo {
this._readContext(
{
path,
fromImmutablePath: () =>
/** @type {ContextHash} */ (/** @type {unknown} */ ("")),
fromImmutablePath: () => /** @type {ContextHash | ""} */ (""),
fromManagedItem: info => info || "",
fromSymlink: (file, target, callback) => {
callback(
Expand Down Expand Up @@ -3773,18 +3772,23 @@ class FileSystemInfo {
this._readContext(
{
path,
fromImmutablePath: () => null,
fromImmutablePath: () =>
/** @type {ContextTimestampAndHash | null} */ (null),
fromManagedItem: info => ({
safeTime: 0,
timestampHash: info,
hash: info || ""
}),
fromSymlink: (file, target, callback) => {
callback(null, {
timestampHash: target,
hash: target,
symlinks: new Set([target])
});
callback(
null,
/** @type {TODO} */
({
timestampHash: target,
hash: target,
symlinks: new Set([target])
})
);
},
fromFile: (file, stat, callback) => {
this._getFileTimestampAndHash(file, callback);
Expand Down
15 changes: 10 additions & 5 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,10 @@ class NormalModule extends Module {
webpack: true,
sourceMap: Boolean(this.useSourceMap),
mode: options.mode || "production",
hashFunction: options.output.hashFunction,
hashDigest: options.output.hashDigest,
hashDigestLength: options.output.hashDigestLength,
hashSalt: options.output.hashSalt,
hashFunction: /** @type {TODO} */ (options.output.hashFunction),
hashDigest: /** @type {string} */ (options.output.hashDigest),
hashDigestLength: /** @type {number} */ (options.output.hashDigestLength),
hashSalt: /** @type {string} */ (options.output.hashSalt),
_module: this,
_compilation: compilation,
_compiler: compilation.compiler,
Expand Down Expand Up @@ -951,7 +951,7 @@ class NormalModule extends Module {
/** @type {LoaderContext<any>} */ (loaderContext)
);
} catch (err) {
processResult(err);
processResult(/** @type {Error} */ (err));
return;
}

Expand All @@ -965,6 +965,11 @@ class NormalModule extends Module {
resource: this.resource,
loaders: this.loaders,
context: loaderContext,
/**
* @param {LoaderContext<TODO>} loaderContext the loader context
* @param {string} resourcePath the resource Path
* @param {(err: Error | null, result?: string | Buffer) => void} callback callback
*/
processResource: (loaderContext, resourcePath, callback) => {
const resource = loaderContext.resource;
const scheme = getScheme(resource);
Expand Down
20 changes: 8 additions & 12 deletions lib/NormalModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ const {
/** @typedef {import("./dependencies/ModuleDependency")} ModuleDependency */
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */

/** @typedef {Pick<RuleSetRule, 'type'|'sideEffects'|'parser'|'generator'|'resolve'|'layer'>} ModuleSettings */
/** @typedef {Partial<NormalModuleCreateData & {settings: ModuleSettings}>} CreateData */
/** @typedef {Pick<RuleSetRule, 'type' | 'sideEffects' | 'parser' | 'generator' | 'resolve' | 'layer'>} ModuleSettings */
/** @typedef {Partial<NormalModuleCreateData & { settings: ModuleSettings }>} CreateData */

/**
* @typedef {object} ResolveData
Expand Down Expand Up @@ -373,9 +373,7 @@ class NormalModuleFactory extends ModuleFactory {

// TODO webpack 6 make it required and move javascript/wasm/asset properties to own module
createdModule = this.hooks.createModuleClass
.for(
/** @type {ModuleSettings} */ (createData.settings).type
)
.for(createData.settings.type)
.call(createData, resolveData);

if (!createdModule) {
Expand Down Expand Up @@ -616,14 +614,12 @@ class NormalModuleFactory extends ModuleFactory {
] === "object" &&
settings[/** @type {keyof ModuleSettings} */ (r.type)] !== null
) {
settings[/** @type {keyof ModuleSettings} */ (r.type)] =
cachedCleverMerge(
settings[/** @type {keyof ModuleSettings} */ (r.type)],
r.value
);
settings[r.type] = cachedCleverMerge(
settings[/** @type {keyof ModuleSettings} */ (r.type)],
r.value
);
} else {
settings[/** @type {keyof ModuleSettings} */ (r.type)] =
r.value;
settings[r.type] = r.value;
}
}
}
Expand Down
32 changes: 12 additions & 20 deletions lib/RuntimeTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class RuntimeTemplate {
*/
constructor(compilation, outputOptions, requestShortener) {
this.compilation = compilation;
this.outputOptions = outputOptions || {};
this.outputOptions = /** @type {OutputOptions} */ (outputOptions || {});
this.requestShortener = requestShortener;
this.globalObject =
/** @type {string} */
Expand All @@ -106,55 +106,47 @@ class RuntimeTemplate {
}

supportsConst() {
return /** @type {Environment} */ (this.outputOptions.environment).const;
return this.outputOptions.environment.const;
}

supportsArrowFunction() {
return /** @type {Environment} */ (this.outputOptions.environment)
.arrowFunction;
return this.outputOptions.environment.arrowFunction;
}

supportsAsyncFunction() {
return /** @type {Environment} */ (this.outputOptions.environment)
.asyncFunction;
return this.outputOptions.environment.asyncFunction;
}

supportsOptionalChaining() {
return /** @type {Environment} */ (this.outputOptions.environment)
.optionalChaining;
return this.outputOptions.environment.optionalChaining;
}

supportsForOf() {
return /** @type {Environment} */ (this.outputOptions.environment).forOf;
return this.outputOptions.environment.forOf;
}

supportsDestructuring() {
return /** @type {Environment} */ (this.outputOptions.environment)
.destructuring;
return this.outputOptions.environment.destructuring;
}

supportsBigIntLiteral() {
return /** @type {Environment} */ (this.outputOptions.environment)
.bigIntLiteral;
return this.outputOptions.environment.bigIntLiteral;
}

supportsDynamicImport() {
return /** @type {Environment} */ (this.outputOptions.environment)
.dynamicImport;
return this.outputOptions.environment.dynamicImport;
}

supportsEcmaScriptModuleSyntax() {
return /** @type {Environment} */ (this.outputOptions.environment).module;
return this.outputOptions.environment.module;
}

supportTemplateLiteral() {
return /** @type {Environment} */ (this.outputOptions.environment)
.templateLiteral;
return this.outputOptions.environment.templateLiteral;
}

supportNodePrefixForCoreModules() {
return /** @type {Environment} */ (this.outputOptions.environment)
.nodePrefixForCoreModules;
return this.outputOptions.environment.nodePrefixForCoreModules;
}

/**
Expand Down
Loading

0 comments on commit 636b5c5

Please sign in to comment.