From 586d013b91e45ce21b352fcf1bbe4b9829819052 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Fri, 16 Apr 2021 19:28:30 +0100 Subject: [PATCH 01/29] feat: LoaderContext type --- lib/NormalModule.js | 28 +++++++++++++++++++++++++++- types.d.ts | 29 ++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/NormalModule.js b/lib/NormalModule.js index 25dfd77cd06..2183f88bc09 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -44,8 +44,11 @@ const memoize = require("./util/memoize"); /** @typedef {import("source-map").RawSourceMap} SourceMap */ /** @typedef {import("webpack-sources").Source} Source */ +/** @typedef {import("../declarations/WebpackOptions").Loader} Loader */ +/** @typedef {import("../declarations/WebpackOptions").Mode} Mode */ /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */ /** @typedef {import("./ChunkGraph")} ChunkGraph */ +/** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */ /** @typedef {import("./DependencyTemplates")} DependencyTemplates */ /** @typedef {import("./Generator")} Generator */ @@ -61,6 +64,7 @@ const memoize = require("./util/memoize"); /** @typedef {import("./RequestShortener")} RequestShortener */ /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */ /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */ +/** @typedef {import("./logging/Logger").Logger} WebpackLogger */ /** @typedef {import("./util/Hash")} Hash */ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */ @@ -79,6 +83,28 @@ const ABSOLUTE_PATH_REGEX = /^([a-zA-Z]:\\|\\\\|\/)/; * @property {string?} type */ +/** + * @typedef {Object} LoaderContextBase + * @property {(schema: any) => any} getOptions + * @property {(warning: any) => void} emitWarning + * @property {(error: any) => void} emitError + * @property {(context: any, request: any, callback: any) => void} resolve + * @property {(options: any) => (context: any, request: any, callback: any) => Promise} getResolve + * @property {(name: any, content: any, sourceMap: any, assetInfo: any) => void} emitFile + * @property {(dep: any) => void} addBuildDependency + * @property {{ absolutify: (context: any, request: any) => string; contextify: (context: any, request: any) => string; }} utils + * @property {string} rootContext + * @property {boolean} webpack + * @property {boolean} sourceMap + * @property {Mode} mode + * @property {any} _module + * @property {Compilation} _compilation + * @property {Compiler} _compiler + * @property {InputFileSystem} fs + */ + +/** @typedef {LoaderContextBase & Loader} LoaderContext */ + /** * @param {string} context absolute context path * @param {string} source a source path @@ -421,7 +447,7 @@ class NormalModule extends Module { * @param {WebpackOptions} options webpack options * @param {Compilation} compilation the compilation * @param {InputFileSystem} fs file system from reading - * @returns {any} loader context + * @returns {LoaderContext} loader context */ createLoaderContext(resolver, options, compilation, fs) { const { requestShortener } = compilation.runtimeTemplate; diff --git a/types.d.ts b/types.d.ts index 28aba12d2a0..a3f763f2873 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5740,6 +5740,32 @@ declare class LoadScriptRuntimeModule extends HelperRuntimeModule { declare interface Loader { [index: string]: any; } +type LoaderContext = LoaderContextBase & Loader; +declare interface LoaderContextBase { + getOptions: (schema?: any) => any; + emitWarning: (warning?: any) => void; + emitError: (error?: any) => void; + resolve: (context?: any, request?: any, callback?: any) => void; + getResolve: ( + options?: any + ) => (context?: any, request?: any, callback?: any) => Promise; + emitFile: ( + name?: any, + content?: any, + sourceMap?: any, + assetInfo?: any + ) => void; + addBuildDependency: (dep?: any) => void; + utils: { + absolutify: (context?: any, request?: any) => string; + contextify: (context?: any, request?: any) => string; + }; + rootContext: string; + webpack: boolean; + sourceMap: boolean; + mode: Mode; + fs: InputFileSystem; +} declare interface LoaderItem { loader: string; options: any; @@ -5924,6 +5950,7 @@ declare interface MinChunkSizePluginOptions { */ minChunkSize: number; } +type Mode = "development" | "production" | "none"; declare class Module extends DependenciesBlock { constructor(type: string, context?: string, layer?: string); type: string; @@ -6847,7 +6874,7 @@ declare class NormalModule extends Module { options: WebpackOptionsNormalized, compilation: Compilation, fs: InputFileSystem - ): any; + ): LoaderContext; getCurrentLoader(loaderContext?: any, index?: any): null | LoaderItem; createSource( context: string, From a2f8808fee8b8ba381ea54dd4da06e6c109ec57a Mon Sep 17 00:00:00 2001 From: John Reilly Date: Mon, 19 Apr 2021 19:30:58 +0100 Subject: [PATCH 02/29] apply @sokra's feedback and include runtime type --- declarations/LoaderContext.d.ts | 155 ++++++++++++++++++++++++++++++++ lib/NormalModule.js | 26 +----- lib/index.js | 1 + types.d.ts | 149 ++++++++++++++++++++++++++---- 4 files changed, 289 insertions(+), 42 deletions(-) create mode 100644 declarations/LoaderContext.d.ts diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts new file mode 100644 index 00000000000..dfe93adc81f --- /dev/null +++ b/declarations/LoaderContext.d.ts @@ -0,0 +1,155 @@ +import type { AssetInfo, Configuration } from "../lib"; +import Compilation from "../lib/Compilation"; +import NormalModule, { InputFileSystem } from "../lib/NormalModule"; +import type { Mode } from "./WebpackOptions"; + +export interface LoaderContextBase { + version: number; + getOptions(schema: any): any; + emitWarning(warning: Error | string): void; + emitError(error: Error | string): void; + getLogger(name: string): Compilation["logger"]; + resolve(context: string, request: string, callback: any): any; + getResolve( + options: Configuration + ): (context: string, request: string, callback: any) => Promise; + emitFile( + name: string, + content: string, + sourceMap: string, + assetInfo: AssetInfo + ): void; + addBuildDependency(dep: string): void; + utils: { + absolutify: (context: string, request: string) => string; + contextify: (context: string, request: string) => string; + }; + rootContext: string; + webpack: boolean; + sourceMap: boolean; + mode: Mode; + _module: NormalModule; + _compilation: Compilation; + _compiler: Compilation.Compiler; + fs: InputFileSystem; +} + +/** The types added to LoaderContextBase by https://github.com/webpack/loader-runner */ +export interface LoaderContext extends LoaderContextBase { + /** + * Add a directory as dependency of the loader result. + */ + addContextDependency(context: string): void; + + /** + * Adds a file as dependency of the loader result in order to make them watchable. + * For example, html-loader uses this technique as it finds src and src-set attributes. + * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. + */ + addDependency(file: string): void; + + addMissingDependency(context: string): void; + + /** + * Make this loader async. + */ + async(): ( + err: Error | undefined | null, + content?: string | Buffer, + sourceMap?: string | any + ) => void | undefined; + + /** + * Make this loader result cacheable. By default it's not cacheable. + * A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed. + * This means the loader shouldn't have other dependencies than specified with this.addDependency. + * Most loaders are deterministic and cacheable. + */ + cacheable(flag?: boolean): void; + + callback(): void; + + /** + * Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. + */ + clearDependencies(): void; + + /** + * The directory of the module. Can be used as context for resolving other stuff. + * eg '/workspaces/ts-loader/examples/vanilla/src' + */ + context: string; + + readonly currentRequest: string; + + readonly data: any; + /** + * alias of addDependency + * Adds a file as dependency of the loader result in order to make them watchable. + * For example, html-loader uses this technique as it finds src and src-set attributes. + * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. + */ + dependency(file: string): void; + + getContextDependencies(): string[]; + + getDependencies(): string[]; + + getMissingDependencies(): string[]; + + /** + * The index in the loaders array of the current loader. + * In the example: in loader1: 0, in loader2: 1 + */ + loaderIndex: number; + + /** + * Resolves the given request to a module, applies all configured loaders and calls + * back with the generated source, the sourceMap and the module instance (usually an + * instance of NormalModule). Use this function if you need to know the source code + * of another module to generate the result. + */ + loadModule( + request: string, + callback: ( + err: Error | null, + source: string, + sourceMap: any, + module: NormalModule + ) => void + ): void; + + readonly previousRequest: any; + + readonly query: any; + + readonly remainingRequest: any; + + readonly request: any; + + /** + * An array of all the loaders. It is writeable in the pitch phase. + * loaders = [{request: string, path: string, query: string, module: function}] + * + * In the example: + * [ + * { request: "/abc/loader1.js?xyz", + * path: "/abc/loader1.js", + * query: "?xyz", + * module: [Function] + * }, + * { request: "/abc/node_modules/loader2/index.js", + * path: "/abc/node_modules/loader2/index.js", + * query: "", + * module: [Function] + * } + * ] + */ + loaders: { request: string }[]; + + /** + * The resource file. + * In the example: "/abc/resource.js" + */ + resourcePath: string; +} diff --git a/lib/NormalModule.js b/lib/NormalModule.js index 2183f88bc09..94d77cc5e63 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -44,7 +44,7 @@ const memoize = require("./util/memoize"); /** @typedef {import("source-map").RawSourceMap} SourceMap */ /** @typedef {import("webpack-sources").Source} Source */ -/** @typedef {import("../declarations/WebpackOptions").Loader} Loader */ +/** @typedef {import("../declarations/LoaderContext").LoaderContextBase} LoaderContextBase */ /** @typedef {import("../declarations/WebpackOptions").Mode} Mode */ /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */ /** @typedef {import("./ChunkGraph")} ChunkGraph */ @@ -83,28 +83,6 @@ const ABSOLUTE_PATH_REGEX = /^([a-zA-Z]:\\|\\\\|\/)/; * @property {string?} type */ -/** - * @typedef {Object} LoaderContextBase - * @property {(schema: any) => any} getOptions - * @property {(warning: any) => void} emitWarning - * @property {(error: any) => void} emitError - * @property {(context: any, request: any, callback: any) => void} resolve - * @property {(options: any) => (context: any, request: any, callback: any) => Promise} getResolve - * @property {(name: any, content: any, sourceMap: any, assetInfo: any) => void} emitFile - * @property {(dep: any) => void} addBuildDependency - * @property {{ absolutify: (context: any, request: any) => string; contextify: (context: any, request: any) => string; }} utils - * @property {string} rootContext - * @property {boolean} webpack - * @property {boolean} sourceMap - * @property {Mode} mode - * @property {any} _module - * @property {Compilation} _compilation - * @property {Compiler} _compiler - * @property {InputFileSystem} fs - */ - -/** @typedef {LoaderContextBase & Loader} LoaderContext */ - /** * @param {string} context absolute context path * @param {string} source a source path @@ -447,7 +425,7 @@ class NormalModule extends Module { * @param {WebpackOptions} options webpack options * @param {Compilation} compilation the compilation * @param {InputFileSystem} fs file system from reading - * @returns {LoaderContext} loader context + * @returns {LoaderContextBase} loader context */ createLoaderContext(resolver, options, compilation, fs) { const { requestShortener } = compilation.runtimeTemplate; diff --git a/lib/index.js b/lib/index.js index dfcf631fa60..ec839ebfa46 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,6 +8,7 @@ const util = require("util"); const memoize = require("./util/memoize"); +/** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */ /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} EntryNormalized */ /** @typedef {import("../declarations/WebpackOptions").EntryObject} EntryObject */ diff --git a/types.d.ts b/types.d.ts index a3f763f2873..171f6558bf6 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5740,25 +5740,137 @@ declare class LoadScriptRuntimeModule extends HelperRuntimeModule { declare interface Loader { [index: string]: any; } -type LoaderContext = LoaderContextBase & Loader; + +/** + * The types added to LoaderContextBase by https://github.com/webpack/loader-runner + */ +declare interface LoaderContext extends LoaderContextBase { + /** + * Add a directory as dependency of the loader result. + */ + addContextDependency(context: string): void; + + /** + * Adds a file as dependency of the loader result in order to make them watchable. + * For example, html-loader uses this technique as it finds src and src-set attributes. + * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. + */ + addDependency(file: string): void; + addMissingDependency(context: string): void; + + /** + * Make this loader async. + */ + async(): ( + err?: null | Error, + content?: string | Buffer, + sourceMap?: any + ) => undefined | void; + + /** + * Make this loader result cacheable. By default it's not cacheable. + * A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed. + * This means the loader shouldn't have other dependencies than specified with this.addDependency. + * Most loaders are deterministic and cacheable. + */ + cacheable(flag?: boolean): void; + callback(): void; + + /** + * Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. + */ + clearDependencies(): void; + + /** + * The directory of the module. Can be used as context for resolving other stuff. + * eg '/workspaces/ts-loader/examples/vanilla/src' + */ + context: string; + readonly currentRequest: string; + readonly data: any; + + /** + * alias of addDependency + * Adds a file as dependency of the loader result in order to make them watchable. + * For example, html-loader uses this technique as it finds src and src-set attributes. + * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. + */ + dependency(file: string): void; + getContextDependencies(): string[]; + getDependencies(): string[]; + getMissingDependencies(): string[]; + + /** + * The index in the loaders array of the current loader. + * In the example: in loader1: 0, in loader2: 1 + */ + loaderIndex: number; + + /** + * Resolves the given request to a module, applies all configured loaders and calls + * back with the generated source, the sourceMap and the module instance (usually an + * instance of NormalModule). Use this function if you need to know the source code + * of another module to generate the result. + */ + loadModule( + request: string, + callback: ( + err: null | Error, + source: string, + sourceMap: any, + module: NormalModule + ) => void + ): void; + readonly previousRequest: any; + readonly query: any; + readonly remainingRequest: any; + readonly request: any; + + /** + * An array of all the loaders. It is writeable in the pitch phase. + * loaders = [{request: string, path: string, query: string, module: function}] + * In the example: + * [ + * { request: "/abc/loader1.js?xyz", + * path: "/abc/loader1.js", + * query: "?xyz", + * module: [Function] + * }, + * { request: "/abc/node_modules/loader2/index.js", + * path: "/abc/node_modules/loader2/index.js", + * query: "", + * module: [Function] + * } + * ] + */ + loaders: { request: string }[]; + + /** + * The resource file. + * In the example: "/abc/resource.js" + */ + resourcePath: string; +} declare interface LoaderContextBase { - getOptions: (schema?: any) => any; - emitWarning: (warning?: any) => void; - emitError: (error?: any) => void; - resolve: (context?: any, request?: any, callback?: any) => void; - getResolve: ( - options?: any - ) => (context?: any, request?: any, callback?: any) => Promise; - emitFile: ( - name?: any, - content?: any, - sourceMap?: any, - assetInfo?: any - ) => void; - addBuildDependency: (dep?: any) => void; + version: number; + getOptions(schema?: any): any; + emitWarning(warning: string | Error): void; + emitError(error: string | Error): void; + getLogger(name: string): WebpackLogger; + resolve(context: string, request: string, callback?: any): any; + getResolve( + options: Configuration + ): (context: string, request: string, callback?: any) => Promise; + emitFile( + name: string, + content: string, + sourceMap: string, + assetInfo: AssetInfo + ): void; + addBuildDependency(dep: string): void; utils: { - absolutify: (context?: any, request?: any) => string; - contextify: (context?: any, request?: any) => string; + absolutify: (context: string, request: string) => string; + contextify: (context: string, request: string) => string; }; rootContext: string; webpack: boolean; @@ -6874,7 +6986,7 @@ declare class NormalModule extends Module { options: WebpackOptionsNormalized, compilation: Compilation, fs: InputFileSystem - ): LoaderContext; + ): LoaderContextBase; getCurrentLoader(loaderContext?: any, index?: any): null | LoaderItem; createSource( context: string, @@ -11925,6 +12037,7 @@ declare namespace exports { WebpackError, WebpackOptionsApply, WebpackOptionsDefaulter, + LoaderContext, Entry, EntryNormalized, EntryObject, From df44d7d8b733c0e4c9e20244394c2e175137215e Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 06:05:54 +0100 Subject: [PATCH 03/29] attempt @sokras suggestion --- declarations/LoaderContext.d.ts | 29 ++- lib/NormalModule.js | 4 +- lib/index.js | 2 + .../errors/load-module-error/error-loader.js | 1 + types.d.ts | 244 ++++++++++-------- 5 files changed, 159 insertions(+), 121 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index dfe93adc81f..58f73e77ca7 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -3,7 +3,7 @@ import Compilation from "../lib/Compilation"; import NormalModule, { InputFileSystem } from "../lib/NormalModule"; import type { Mode } from "./WebpackOptions"; -export interface LoaderContextBase { +export interface LoaderContext { version: number; getOptions(schema: any): any; emitWarning(warning: Error | string): void; @@ -35,7 +35,7 @@ export interface LoaderContextBase { } /** The types added to LoaderContextBase by https://github.com/webpack/loader-runner */ -export interface LoaderContext extends LoaderContextBase { +export interface EmptyContextAdditions { /** * Add a directory as dependency of the loader result. */ @@ -119,13 +119,13 @@ export interface LoaderContext extends LoaderContextBase { ) => void ): void; - readonly previousRequest: any; + readonly previousRequest: string; readonly query: any; - readonly remainingRequest: any; + readonly remainingRequest: string; - readonly request: any; + readonly request: string; /** * An array of all the loaders. It is writeable in the pitch phase. @@ -145,7 +145,20 @@ export interface LoaderContext extends LoaderContextBase { * } * ] */ - loaders: { request: string }[]; + loaders: { + request: string; + path: string; + query: string; + fragment: string; + options: any; + ident: string; + normal: any; + pitch: any; + raw: any; + data: any; + pitchExecuted: boolean; + normalExecuted: boolean; + }[]; /** * The resource file. @@ -153,3 +166,7 @@ export interface LoaderContext extends LoaderContextBase { */ resourcePath: string; } + +export interface LoaderDefinition { + (this: LoaderContext & ContextAdditions, contents: string): string; +} \ No newline at end of file diff --git a/lib/NormalModule.js b/lib/NormalModule.js index 94d77cc5e63..58d85da4821 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -44,7 +44,7 @@ const memoize = require("./util/memoize"); /** @typedef {import("source-map").RawSourceMap} SourceMap */ /** @typedef {import("webpack-sources").Source} Source */ -/** @typedef {import("../declarations/LoaderContext").LoaderContextBase} LoaderContextBase */ +/** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ /** @typedef {import("../declarations/WebpackOptions").Mode} Mode */ /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */ /** @typedef {import("./ChunkGraph")} ChunkGraph */ @@ -425,7 +425,7 @@ class NormalModule extends Module { * @param {WebpackOptions} options webpack options * @param {Compilation} compilation the compilation * @param {InputFileSystem} fs file system from reading - * @returns {LoaderContextBase} loader context + * @returns {LoaderContext} loader context */ createLoaderContext(resolver, options, compilation, fs) { const { requestShortener } = compilation.runtimeTemplate; diff --git a/lib/index.js b/lib/index.js index ec839ebfa46..cff404ac14d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,7 +8,9 @@ const util = require("util"); const memoize = require("./util/memoize"); +/** @typedef {import("../declarations/LoaderContext").EmptyContextAdditions} EmptyContextAdditions */ /** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ +/** @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */ /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} EntryNormalized */ /** @typedef {import("../declarations/WebpackOptions").EntryObject} EntryObject */ diff --git a/test/cases/errors/load-module-error/error-loader.js b/test/cases/errors/load-module-error/error-loader.js index 114e742cb87..5758c7646c7 100644 --- a/test/cases/errors/load-module-error/error-loader.js +++ b/test/cases/errors/load-module-error/error-loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../types").LoaderDefinition} */ module.exports = function(source) { const callback = this.async(); callback(new Error("err: abc")); diff --git a/types.d.ts b/types.d.ts index 171f6558bf6..c328cacd464 100644 --- a/types.d.ts +++ b/types.d.ts @@ -2841,6 +2841,130 @@ declare class ElectronTargetPlugin { apply(compiler: Compiler): void; } +/** + * The types added to LoaderContextBase by https://github.com/webpack/loader-runner + */ +declare interface EmptyContextAdditions { + /** + * Add a directory as dependency of the loader result. + */ + addContextDependency(context: string): void; + + /** + * Adds a file as dependency of the loader result in order to make them watchable. + * For example, html-loader uses this technique as it finds src and src-set attributes. + * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. + */ + addDependency(file: string): void; + addMissingDependency(context: string): void; + + /** + * Make this loader async. + */ + async(): ( + err?: null | Error, + content?: string | Buffer, + sourceMap?: any + ) => undefined | void; + + /** + * Make this loader result cacheable. By default it's not cacheable. + * A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed. + * This means the loader shouldn't have other dependencies than specified with this.addDependency. + * Most loaders are deterministic and cacheable. + */ + cacheable(flag?: boolean): void; + callback(): void; + + /** + * Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. + */ + clearDependencies(): void; + + /** + * The directory of the module. Can be used as context for resolving other stuff. + * eg '/workspaces/ts-loader/examples/vanilla/src' + */ + context: string; + readonly currentRequest: string; + readonly data: any; + + /** + * alias of addDependency + * Adds a file as dependency of the loader result in order to make them watchable. + * For example, html-loader uses this technique as it finds src and src-set attributes. + * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. + */ + dependency(file: string): void; + getContextDependencies(): string[]; + getDependencies(): string[]; + getMissingDependencies(): string[]; + + /** + * The index in the loaders array of the current loader. + * In the example: in loader1: 0, in loader2: 1 + */ + loaderIndex: number; + + /** + * Resolves the given request to a module, applies all configured loaders and calls + * back with the generated source, the sourceMap and the module instance (usually an + * instance of NormalModule). Use this function if you need to know the source code + * of another module to generate the result. + */ + loadModule( + request: string, + callback: ( + err: null | Error, + source: string, + sourceMap: any, + module: NormalModule + ) => void + ): void; + readonly previousRequest: string; + readonly query: any; + readonly remainingRequest: string; + readonly request: string; + + /** + * An array of all the loaders. It is writeable in the pitch phase. + * loaders = [{request: string, path: string, query: string, module: function}] + * In the example: + * [ + * { request: "/abc/loader1.js?xyz", + * path: "/abc/loader1.js", + * query: "?xyz", + * module: [Function] + * }, + * { request: "/abc/node_modules/loader2/index.js", + * path: "/abc/node_modules/loader2/index.js", + * query: "", + * module: [Function] + * } + * ] + */ + loaders: { + request: string; + path: string; + query: string; + fragment: string; + options: any; + ident: string; + normal: any; + pitch: any; + raw: any; + data: any; + pitchExecuted: boolean; + normalExecuted: boolean; + }[]; + + /** + * The resource file. + * In the example: "/abc/resource.js" + */ + resourcePath: string; +} + /** * No generator options are supported for this module type. */ @@ -5740,118 +5864,7 @@ declare class LoadScriptRuntimeModule extends HelperRuntimeModule { declare interface Loader { [index: string]: any; } - -/** - * The types added to LoaderContextBase by https://github.com/webpack/loader-runner - */ -declare interface LoaderContext extends LoaderContextBase { - /** - * Add a directory as dependency of the loader result. - */ - addContextDependency(context: string): void; - - /** - * Adds a file as dependency of the loader result in order to make them watchable. - * For example, html-loader uses this technique as it finds src and src-set attributes. - * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. - */ - addDependency(file: string): void; - addMissingDependency(context: string): void; - - /** - * Make this loader async. - */ - async(): ( - err?: null | Error, - content?: string | Buffer, - sourceMap?: any - ) => undefined | void; - - /** - * Make this loader result cacheable. By default it's not cacheable. - * A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed. - * This means the loader shouldn't have other dependencies than specified with this.addDependency. - * Most loaders are deterministic and cacheable. - */ - cacheable(flag?: boolean): void; - callback(): void; - - /** - * Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. - */ - clearDependencies(): void; - - /** - * The directory of the module. Can be used as context for resolving other stuff. - * eg '/workspaces/ts-loader/examples/vanilla/src' - */ - context: string; - readonly currentRequest: string; - readonly data: any; - - /** - * alias of addDependency - * Adds a file as dependency of the loader result in order to make them watchable. - * For example, html-loader uses this technique as it finds src and src-set attributes. - * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. - */ - dependency(file: string): void; - getContextDependencies(): string[]; - getDependencies(): string[]; - getMissingDependencies(): string[]; - - /** - * The index in the loaders array of the current loader. - * In the example: in loader1: 0, in loader2: 1 - */ - loaderIndex: number; - - /** - * Resolves the given request to a module, applies all configured loaders and calls - * back with the generated source, the sourceMap and the module instance (usually an - * instance of NormalModule). Use this function if you need to know the source code - * of another module to generate the result. - */ - loadModule( - request: string, - callback: ( - err: null | Error, - source: string, - sourceMap: any, - module: NormalModule - ) => void - ): void; - readonly previousRequest: any; - readonly query: any; - readonly remainingRequest: any; - readonly request: any; - - /** - * An array of all the loaders. It is writeable in the pitch phase. - * loaders = [{request: string, path: string, query: string, module: function}] - * In the example: - * [ - * { request: "/abc/loader1.js?xyz", - * path: "/abc/loader1.js", - * query: "?xyz", - * module: [Function] - * }, - * { request: "/abc/node_modules/loader2/index.js", - * path: "/abc/node_modules/loader2/index.js", - * query: "", - * module: [Function] - * } - * ] - */ - loaders: { request: string }[]; - - /** - * The resource file. - * In the example: "/abc/resource.js" - */ - resourcePath: string; -} -declare interface LoaderContextBase { +declare interface LoaderContext { version: number; getOptions(schema?: any): any; emitWarning(warning: string | Error): void; @@ -5878,6 +5891,9 @@ declare interface LoaderContextBase { mode: Mode; fs: InputFileSystem; } +declare interface LoaderDefinition { + (this: LoaderContext & ContextAdditions, contents: string): string; +} declare interface LoaderItem { loader: string; options: any; @@ -6986,7 +7002,7 @@ declare class NormalModule extends Module { options: WebpackOptionsNormalized, compilation: Compilation, fs: InputFileSystem - ): LoaderContextBase; + ): LoaderContext; getCurrentLoader(loaderContext?: any, index?: any): null | LoaderItem; createSource( context: string, @@ -11978,6 +11994,7 @@ declare namespace exports { export { HttpUriPlugin, HttpsUriPlugin }; } } + export type LoaderDefinition = LoaderDefinition; export type WebpackPluginFunction = ( this: Compiler, compiler: Compiler @@ -12037,6 +12054,7 @@ declare namespace exports { WebpackError, WebpackOptionsApply, WebpackOptionsDefaulter, + EmptyContextAdditions, LoaderContext, Entry, EntryNormalized, From 79da3201ac89b6024df3e871334784b2f9acd285 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 08:41:42 +0100 Subject: [PATCH 04/29] non generics --- declarations/LoaderContext.d.ts | 4 ++-- types.d.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 58f73e77ca7..6314a69d3e5 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -167,6 +167,6 @@ export interface EmptyContextAdditions { resourcePath: string; } -export interface LoaderDefinition { - (this: LoaderContext & ContextAdditions, contents: string): string; +export interface LoaderDefinition { + (this: LoaderContext & EmptyContextAdditions, contents: string): string; } \ No newline at end of file diff --git a/types.d.ts b/types.d.ts index c328cacd464..d23eb4b7d0b 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5891,9 +5891,6 @@ declare interface LoaderContext { mode: Mode; fs: InputFileSystem; } -declare interface LoaderDefinition { - (this: LoaderContext & ContextAdditions, contents: string): string; -} declare interface LoaderItem { loader: string; options: any; @@ -11994,7 +11991,10 @@ declare namespace exports { export { HttpUriPlugin, HttpsUriPlugin }; } } - export type LoaderDefinition = LoaderDefinition; + export type LoaderDefinition = ( + this: LoaderContext & EmptyContextAdditions, + contents: string + ) => string; export type WebpackPluginFunction = ( this: Compiler, compiler: Compiler From 39042a2578257fd8cf8f613ed6484cce721a3016 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 08:52:15 +0100 Subject: [PATCH 05/29] Update lib/index.js Co-authored-by: Tobias Koppers --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index cff404ac14d..c2c76b2e9e4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,7 +10,7 @@ const memoize = require("./util/memoize"); /** @typedef {import("../declarations/LoaderContext").EmptyContextAdditions} EmptyContextAdditions */ /** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ -/** @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ +/** @template T @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */ /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} EntryNormalized */ /** @typedef {import("../declarations/WebpackOptions").EntryObject} EntryObject */ From b967f36a4283481aa81d9512a492ef4581aa2a78 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:00:20 +0100 Subject: [PATCH 06/29] Update declarations/LoaderContext.d.ts Co-authored-by: Tobias Koppers --- declarations/LoaderContext.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 6314a69d3e5..c45bca46b97 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -8,7 +8,7 @@ export interface LoaderContext { getOptions(schema: any): any; emitWarning(warning: Error | string): void; emitError(error: Error | string): void; - getLogger(name: string): Compilation["logger"]; + getLogger(name: string): import("../lib/logging/Logger").Logger; resolve(context: string, request: string, callback: any): any; getResolve( options: Configuration @@ -169,4 +169,4 @@ export interface EmptyContextAdditions { export interface LoaderDefinition { (this: LoaderContext & EmptyContextAdditions, contents: string): string; -} \ No newline at end of file +} From 5ef8acac7cb86275233b5633b94c22d2469e30b5 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:00:32 +0100 Subject: [PATCH 07/29] Update declarations/LoaderContext.d.ts Co-authored-by: Tobias Koppers --- declarations/LoaderContext.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index c45bca46b97..5947ffa1ba5 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -25,8 +25,8 @@ export interface LoaderContext { contextify: (context: string, request: string) => string; }; rootContext: string; - webpack: boolean; - sourceMap: boolean; + webpack?: boolean; + sourceMap?: boolean; mode: Mode; _module: NormalModule; _compilation: Compilation; From 3d45e2b666724a0d9ff7a2c2a787ac98308ac58c Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:00:51 +0100 Subject: [PATCH 08/29] Update declarations/LoaderContext.d.ts Co-authored-by: Tobias Koppers --- declarations/LoaderContext.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 5947ffa1ba5..e3f69046110 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -56,7 +56,9 @@ export interface EmptyContextAdditions { async(): ( err: Error | undefined | null, content?: string | Buffer, - sourceMap?: string | any + sourceMap?: string | RawSourceMap, + additionalData?: Record, + ...args: any[] ) => void | undefined; /** From 1578fd4f2f315c3fad3a665ed52d605209bfa4fe Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:01:46 +0100 Subject: [PATCH 09/29] schema --- declarations/LoaderContext.d.ts | 3 ++- lib/index.js | 2 +- types.d.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 6314a69d3e5..220823d8ce4 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -1,3 +1,4 @@ +import type { Schema } from "schema-utils/declarations/validate"; import type { AssetInfo, Configuration } from "../lib"; import Compilation from "../lib/Compilation"; import NormalModule, { InputFileSystem } from "../lib/NormalModule"; @@ -5,7 +6,7 @@ import type { Mode } from "./WebpackOptions"; export interface LoaderContext { version: number; - getOptions(schema: any): any; + getOptions(schema: Schema): any; emitWarning(warning: Error | string): void; emitError(error: Error | string): void; getLogger(name: string): Compilation["logger"]; diff --git a/lib/index.js b/lib/index.js index c2c76b2e9e4..cff404ac14d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,7 +10,7 @@ const memoize = require("./util/memoize"); /** @typedef {import("../declarations/LoaderContext").EmptyContextAdditions} EmptyContextAdditions */ /** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ -/** @template T @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ +/** @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */ /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} EntryNormalized */ /** @typedef {import("../declarations/WebpackOptions").EntryObject} EntryObject */ diff --git a/types.d.ts b/types.d.ts index d23eb4b7d0b..e66c1a86e2d 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5866,7 +5866,7 @@ declare interface Loader { } declare interface LoaderContext { version: number; - getOptions(schema?: any): any; + getOptions(schema: Schema): any; emitWarning(warning: string | Error): void; emitError(error: string | Error): void; getLogger(name: string): WebpackLogger; From a679319ce62ce13b168fdc27061b52444ccdabc5 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:06:34 +0100 Subject: [PATCH 10/29] bring in suggested types --- declarations/LoaderContext.d.ts | 14 ++++++++------ types.d.ts | 13 ++++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 08937533fcb..ec070e28da6 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -1,15 +1,17 @@ +import type { RawSourceMap } from "source-map"; import type { Schema } from "schema-utils/declarations/validate"; import type { AssetInfo, Configuration } from "../lib"; import Compilation from "../lib/Compilation"; import NormalModule, { InputFileSystem } from "../lib/NormalModule"; import type { Mode } from "./WebpackOptions"; +import type { Logger } from "../lib/logging/Logger"; export interface LoaderContext { version: number; getOptions(schema: Schema): any; emitWarning(warning: Error | string): void; emitError(error: Error | string): void; - getLogger(name: string): import("../lib/logging/Logger").Logger; + getLogger(name: string): Logger; resolve(context: string, request: string, callback: any): any; getResolve( options: Configuration @@ -148,9 +150,9 @@ export interface EmptyContextAdditions { * } * ] */ - loaders: { - request: string; - path: string; + loaders: { + request: string; + path: string; query: string; fragment: string; options: any; @@ -161,7 +163,7 @@ export interface EmptyContextAdditions { data: any; pitchExecuted: boolean; normalExecuted: boolean; - }[]; + }[]; /** * The resource file. @@ -171,5 +173,5 @@ export interface EmptyContextAdditions { } export interface LoaderDefinition { - (this: LoaderContext & EmptyContextAdditions, contents: string): string; + (this: LoaderContext & EmptyContextAdditions, contents: string): string; } diff --git a/types.d.ts b/types.d.ts index e66c1a86e2d..7785c96a228 100644 --- a/types.d.ts +++ b/types.d.ts @@ -83,6 +83,7 @@ import { Extend, ValidationErrorConfiguration } from "schema-utils/declarations/validate"; +import { RawSourceMap } from "source-map/source-map"; import { AsArray, AsyncParallelHook, @@ -2862,9 +2863,11 @@ declare interface EmptyContextAdditions { * Make this loader async. */ async(): ( - err?: null | Error, - content?: string | Buffer, - sourceMap?: any + err: undefined | null | Error, + content: undefined | string | Buffer, + sourceMap: undefined | string | RawSourceMap, + additionalData: undefined | Record, + ...args: any[] ) => undefined | void; /** @@ -5886,8 +5889,8 @@ declare interface LoaderContext { contextify: (context: string, request: string) => string; }; rootContext: string; - webpack: boolean; - sourceMap: boolean; + webpack?: boolean; + sourceMap?: boolean; mode: Mode; fs: InputFileSystem; } From 111e95c191b4d5632e490f762536a5254270392c Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:07:59 +0100 Subject: [PATCH 11/29] Update declarations/LoaderContext.d.ts Co-authored-by: Tobias Koppers --- declarations/LoaderContext.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index ec070e28da6..e409dc12c5f 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -19,8 +19,8 @@ export interface LoaderContext { emitFile( name: string, content: string, - sourceMap: string, - assetInfo: AssetInfo + sourceMap?: string, + assetInfo?: AssetInfo ): void; addBuildDependency(dep: string): void; utils: { From 35eef1e8616719b070a9d3664d031b284c6a1a44 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:08:16 +0100 Subject: [PATCH 12/29] Update declarations/LoaderContext.d.ts Co-authored-by: Tobias Koppers --- declarations/LoaderContext.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index e409dc12c5f..03578af3db7 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -65,7 +65,7 @@ export interface EmptyContextAdditions { ) => void | undefined; /** - * Make this loader result cacheable. By default it's not cacheable. + * Make this loader result cacheable. By default it's cacheable. * A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed. * This means the loader shouldn't have other dependencies than specified with this.addDependency. * Most loaders are deterministic and cacheable. From 3898b38f522ad536c5185c5ec7028de7cfc94017 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:08:36 +0100 Subject: [PATCH 13/29] Update declarations/LoaderContext.d.ts Co-authored-by: Tobias Koppers --- declarations/LoaderContext.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 03578af3db7..6efb34b5000 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -31,9 +31,9 @@ export interface LoaderContext { webpack?: boolean; sourceMap?: boolean; mode: Mode; - _module: NormalModule; - _compilation: Compilation; - _compiler: Compilation.Compiler; + _module?: NormalModule; + _compilation?: Compilation; + _compiler?: Compiler; fs: InputFileSystem; } From 481832d4d5d4bdd5ccb3f3b76f53ee60c9cd3edb Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:09:03 +0100 Subject: [PATCH 14/29] Update declarations/LoaderContext.d.ts Co-authored-by: Tobias Koppers --- declarations/LoaderContext.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 6efb34b5000..81f86cb43f7 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -6,7 +6,7 @@ import NormalModule, { InputFileSystem } from "../lib/NormalModule"; import type { Mode } from "./WebpackOptions"; import type { Logger } from "../lib/logging/Logger"; -export interface LoaderContext { +export interface NormalModuleLoaderContext { version: number; getOptions(schema: Schema): any; emitWarning(warning: Error | string): void; From 57e49bc034ffb06b286c9bd8e60dd756786f5759 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:09:23 +0100 Subject: [PATCH 15/29] Update declarations/LoaderContext.d.ts Co-authored-by: Tobias Koppers --- declarations/LoaderContext.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 81f86cb43f7..1b7ad9e39f5 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -38,7 +38,7 @@ export interface NormalModuleLoaderContext { } /** The types added to LoaderContextBase by https://github.com/webpack/loader-runner */ -export interface EmptyContextAdditions { +export interface LoaderRunnerLoaderContext { /** * Add a directory as dependency of the loader result. */ From 8703248bef542b29f4180a8dcc7687450b852b0f Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:09:58 +0100 Subject: [PATCH 16/29] Update declarations/LoaderContext.d.ts Co-authored-by: Tobias Koppers --- declarations/LoaderContext.d.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 1b7ad9e39f5..2a032e17bb1 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -172,6 +172,12 @@ export interface LoaderRunnerLoaderContext { resourcePath: string; } +type LoaderContext = NormalModuleLoaderContext & LoaderRunnerLoaderContext; + +declare class EmptyContextAdditions { + _EmptyContextAdditions: true +} + export interface LoaderDefinition { (this: LoaderContext & EmptyContextAdditions, contents: string): string; } From 40a5eb17fef3187a6f20ddee43be758ef062300b Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 09:18:59 +0100 Subject: [PATCH 17/29] tried generic workarouuund --- declarations/LoaderContext.d.ts | 24 +-- lib/NormalModule.js | 4 +- lib/index.js | 2 +- types.d.ts | 325 +++++++++++++++++--------------- 4 files changed, 184 insertions(+), 171 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 2a032e17bb1..4d1c4b21e55 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -33,7 +33,7 @@ export interface NormalModuleLoaderContext { mode: Mode; _module?: NormalModule; _compilation?: Compilation; - _compiler?: Compiler; + _compiler?: Compilation.Compiler; fs: InputFileSystem; } @@ -56,13 +56,7 @@ export interface LoaderRunnerLoaderContext { /** * Make this loader async. */ - async(): ( - err: Error | undefined | null, - content?: string | Buffer, - sourceMap?: string | RawSourceMap, - additionalData?: Record, - ...args: any[] - ) => void | undefined; + async(): WebpackLoaderContextCallback; /** * Make this loader result cacheable. By default it's cacheable. @@ -72,7 +66,7 @@ export interface LoaderRunnerLoaderContext { */ cacheable(flag?: boolean): void; - callback(): void; + callback(): WebpackLoaderContextCallback; /** * Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. @@ -172,12 +166,20 @@ export interface LoaderRunnerLoaderContext { resourcePath: string; } +type WebpackLoaderContextCallback = ( + err: Error | undefined | null, + content?: string | Buffer, + sourceMap?: string | RawSourceMap, + additionalData?: Record, + ...args: any[] +) => void | undefined; + type LoaderContext = NormalModuleLoaderContext & LoaderRunnerLoaderContext; declare class EmptyContextAdditions { _EmptyContextAdditions: true } -export interface LoaderDefinition { - (this: LoaderContext & EmptyContextAdditions, contents: string): string; +export interface LoaderDefinition { + (this: LoaderContext & (ContextAdditions extends EmptyContextAdditions ? {} : ContextAdditions), contents: string): string; } diff --git a/lib/NormalModule.js b/lib/NormalModule.js index 58d85da4821..e2797a12ac2 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -44,7 +44,7 @@ const memoize = require("./util/memoize"); /** @typedef {import("source-map").RawSourceMap} SourceMap */ /** @typedef {import("webpack-sources").Source} Source */ -/** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ +/** @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext} NormalModuleLoaderContext */ /** @typedef {import("../declarations/WebpackOptions").Mode} Mode */ /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */ /** @typedef {import("./ChunkGraph")} ChunkGraph */ @@ -425,7 +425,7 @@ class NormalModule extends Module { * @param {WebpackOptions} options webpack options * @param {Compilation} compilation the compilation * @param {InputFileSystem} fs file system from reading - * @returns {LoaderContext} loader context + * @returns {NormalModuleLoaderContext} loader context */ createLoaderContext(resolver, options, compilation, fs) { const { requestShortener } = compilation.runtimeTemplate; diff --git a/lib/index.js b/lib/index.js index cff404ac14d..c2c76b2e9e4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,7 +10,7 @@ const memoize = require("./util/memoize"); /** @typedef {import("../declarations/LoaderContext").EmptyContextAdditions} EmptyContextAdditions */ /** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ -/** @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ +/** @template T @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */ /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} EntryNormalized */ /** @typedef {import("../declarations/WebpackOptions").EntryObject} EntryObject */ diff --git a/types.d.ts b/types.d.ts index 7785c96a228..757361c4f40 100644 --- a/types.d.ts +++ b/types.d.ts @@ -2841,132 +2841,7 @@ declare class ElectronTargetPlugin { */ apply(compiler: Compiler): void; } - -/** - * The types added to LoaderContextBase by https://github.com/webpack/loader-runner - */ -declare interface EmptyContextAdditions { - /** - * Add a directory as dependency of the loader result. - */ - addContextDependency(context: string): void; - - /** - * Adds a file as dependency of the loader result in order to make them watchable. - * For example, html-loader uses this technique as it finds src and src-set attributes. - * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. - */ - addDependency(file: string): void; - addMissingDependency(context: string): void; - - /** - * Make this loader async. - */ - async(): ( - err: undefined | null | Error, - content: undefined | string | Buffer, - sourceMap: undefined | string | RawSourceMap, - additionalData: undefined | Record, - ...args: any[] - ) => undefined | void; - - /** - * Make this loader result cacheable. By default it's not cacheable. - * A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed. - * This means the loader shouldn't have other dependencies than specified with this.addDependency. - * Most loaders are deterministic and cacheable. - */ - cacheable(flag?: boolean): void; - callback(): void; - - /** - * Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. - */ - clearDependencies(): void; - - /** - * The directory of the module. Can be used as context for resolving other stuff. - * eg '/workspaces/ts-loader/examples/vanilla/src' - */ - context: string; - readonly currentRequest: string; - readonly data: any; - - /** - * alias of addDependency - * Adds a file as dependency of the loader result in order to make them watchable. - * For example, html-loader uses this technique as it finds src and src-set attributes. - * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. - */ - dependency(file: string): void; - getContextDependencies(): string[]; - getDependencies(): string[]; - getMissingDependencies(): string[]; - - /** - * The index in the loaders array of the current loader. - * In the example: in loader1: 0, in loader2: 1 - */ - loaderIndex: number; - - /** - * Resolves the given request to a module, applies all configured loaders and calls - * back with the generated source, the sourceMap and the module instance (usually an - * instance of NormalModule). Use this function if you need to know the source code - * of another module to generate the result. - */ - loadModule( - request: string, - callback: ( - err: null | Error, - source: string, - sourceMap: any, - module: NormalModule - ) => void - ): void; - readonly previousRequest: string; - readonly query: any; - readonly remainingRequest: string; - readonly request: string; - - /** - * An array of all the loaders. It is writeable in the pitch phase. - * loaders = [{request: string, path: string, query: string, module: function}] - * In the example: - * [ - * { request: "/abc/loader1.js?xyz", - * path: "/abc/loader1.js", - * query: "?xyz", - * module: [Function] - * }, - * { request: "/abc/node_modules/loader2/index.js", - * path: "/abc/node_modules/loader2/index.js", - * query: "", - * module: [Function] - * } - * ] - */ - loaders: { - request: string; - path: string; - query: string; - fragment: string; - options: any; - ident: string; - normal: any; - pitch: any; - raw: any; - data: any; - pitchExecuted: boolean; - normalExecuted: boolean; - }[]; - - /** - * The resource file. - * In the example: "/abc/resource.js" - */ - resourcePath: string; -} +declare abstract class EmptyContextAdditions {} /** * No generator options are supported for this module type. @@ -5867,32 +5742,12 @@ declare class LoadScriptRuntimeModule extends HelperRuntimeModule { declare interface Loader { [index: string]: any; } -declare interface LoaderContext { - version: number; - getOptions(schema: Schema): any; - emitWarning(warning: string | Error): void; - emitError(error: string | Error): void; - getLogger(name: string): WebpackLogger; - resolve(context: string, request: string, callback?: any): any; - getResolve( - options: Configuration - ): (context: string, request: string, callback?: any) => Promise; - emitFile( - name: string, - content: string, - sourceMap: string, - assetInfo: AssetInfo - ): void; - addBuildDependency(dep: string): void; - utils: { - absolutify: (context: string, request: string) => string; - contextify: (context: string, request: string) => string; - }; - rootContext: string; - webpack?: boolean; - sourceMap?: boolean; - mode: Mode; - fs: InputFileSystem; +type LoaderContext = NormalModuleLoaderContext & LoaderRunnerLoaderContext; +declare interface LoaderDefinition { + ( + this: NormalModuleLoaderContext & LoaderRunnerLoaderContext & {}, + contents: string + ): string; } declare interface LoaderItem { loader: string; @@ -5933,6 +5788,138 @@ declare interface LoaderOptionsPluginOptions { context?: string; }; } + +/** + * The types added to LoaderContextBase by https://github.com/webpack/loader-runner + */ +declare interface LoaderRunnerLoaderContext { + /** + * Add a directory as dependency of the loader result. + */ + addContextDependency(context: string): void; + + /** + * Adds a file as dependency of the loader result in order to make them watchable. + * For example, html-loader uses this technique as it finds src and src-set attributes. + * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. + */ + addDependency(file: string): void; + addMissingDependency(context: string): void; + + /** + * Make this loader async. + */ + async(): ( + err: undefined | null | Error, + content: undefined | string | Buffer, + sourceMap: undefined | string | RawSourceMap, + additionalData: undefined | Record, + ...args: any[] + ) => undefined | void; + + /** + * Make this loader result cacheable. By default it's cacheable. + * A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed. + * This means the loader shouldn't have other dependencies than specified with this.addDependency. + * Most loaders are deterministic and cacheable. + */ + cacheable(flag?: boolean): void; + callback(): ( + err: undefined | null | Error, + content: undefined | string | Buffer, + sourceMap: undefined | string | RawSourceMap, + additionalData: undefined | Record, + ...args: any[] + ) => undefined | void; + + /** + * Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. + */ + clearDependencies(): void; + + /** + * The directory of the module. Can be used as context for resolving other stuff. + * eg '/workspaces/ts-loader/examples/vanilla/src' + */ + context: string; + readonly currentRequest: string; + readonly data: any; + + /** + * alias of addDependency + * Adds a file as dependency of the loader result in order to make them watchable. + * For example, html-loader uses this technique as it finds src and src-set attributes. + * Then, it sets the url's for those attributes as dependencies of the html file that is parsed. + */ + dependency(file: string): void; + getContextDependencies(): string[]; + getDependencies(): string[]; + getMissingDependencies(): string[]; + + /** + * The index in the loaders array of the current loader. + * In the example: in loader1: 0, in loader2: 1 + */ + loaderIndex: number; + + /** + * Resolves the given request to a module, applies all configured loaders and calls + * back with the generated source, the sourceMap and the module instance (usually an + * instance of NormalModule). Use this function if you need to know the source code + * of another module to generate the result. + */ + loadModule( + request: string, + callback: ( + err: null | Error, + source: string, + sourceMap: any, + module: NormalModule + ) => void + ): void; + readonly previousRequest: string; + readonly query: any; + readonly remainingRequest: string; + readonly request: string; + + /** + * An array of all the loaders. It is writeable in the pitch phase. + * loaders = [{request: string, path: string, query: string, module: function}] + * In the example: + * [ + * { request: "/abc/loader1.js?xyz", + * path: "/abc/loader1.js", + * query: "?xyz", + * module: [Function] + * }, + * { request: "/abc/node_modules/loader2/index.js", + * path: "/abc/node_modules/loader2/index.js", + * query: "", + * module: [Function] + * } + * ] + */ + loaders: { + request: string; + path: string; + query: string; + fragment: string; + options: any; + ident: string; + normal: any; + pitch: any; + raw: any; + data: any; + pitchExecuted: boolean; + normalExecuted: boolean; + }[]; + + /** + * The resource file. + * In the example: "/abc/resource.js" + */ + resourcePath: string; +} declare class LoaderTargetPlugin { constructor(target: string); target: string; @@ -7002,7 +6989,7 @@ declare class NormalModule extends Module { options: WebpackOptionsNormalized, compilation: Compilation, fs: InputFileSystem - ): LoaderContext; + ): NormalModuleLoaderContext; getCurrentLoader(loaderContext?: any, index?: any): null | LoaderItem; createSource( context: string, @@ -7078,6 +7065,33 @@ declare abstract class NormalModuleFactory extends ModuleFactory { createGenerator(type?: any, generatorOptions?: object): any; getResolver(type?: any, resolveOptions?: any): ResolverWithOptions; } +declare interface NormalModuleLoaderContext { + version: number; + getOptions(schema: Schema): any; + emitWarning(warning: string | Error): void; + emitError(error: string | Error): void; + getLogger(name: string): WebpackLogger; + resolve(context: string, request: string, callback?: any): any; + getResolve( + options: Configuration + ): (context: string, request: string, callback?: any) => Promise; + emitFile( + name: string, + content: string, + sourceMap?: string, + assetInfo?: AssetInfo + ): void; + addBuildDependency(dep: string): void; + utils: { + absolutify: (context: string, request: string) => string; + contextify: (context: string, request: string) => string; + }; + rootContext: string; + webpack?: boolean; + sourceMap?: boolean; + mode: Mode; + fs: InputFileSystem; +} declare class NormalModuleReplacementPlugin { /** * Create an instance of the plugin @@ -11994,10 +12008,7 @@ declare namespace exports { export { HttpUriPlugin, HttpsUriPlugin }; } } - export type LoaderDefinition = ( - this: LoaderContext & EmptyContextAdditions, - contents: string - ) => string; + export type LoaderDefinition = LoaderDefinition; export type WebpackPluginFunction = ( this: Compiler, compiler: Compiler From 8cd30eb22262e9dcbc4d91732a90c37cf80451d1 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 18:12:16 +0100 Subject: [PATCH 18/29] revert generic approach --- declarations/LoaderContext.d.ts | 4 ++-- lib/index.js | 2 +- types.d.ts | 13 ++++++------- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 4d1c4b21e55..e595539fa2f 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -180,6 +180,6 @@ declare class EmptyContextAdditions { _EmptyContextAdditions: true } -export interface LoaderDefinition { - (this: LoaderContext & (ContextAdditions extends EmptyContextAdditions ? {} : ContextAdditions), contents: string): string; +export interface LoaderDefinition { + (this: LoaderContext & EmptyContextAdditions, contents: string): string; } diff --git a/lib/index.js b/lib/index.js index c2c76b2e9e4..cff404ac14d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,7 +10,7 @@ const memoize = require("./util/memoize"); /** @typedef {import("../declarations/LoaderContext").EmptyContextAdditions} EmptyContextAdditions */ /** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ -/** @template T @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ +/** @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */ /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} EntryNormalized */ /** @typedef {import("../declarations/WebpackOptions").EntryObject} EntryObject */ diff --git a/types.d.ts b/types.d.ts index 757361c4f40..fb2db0e8517 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5743,12 +5743,6 @@ declare interface Loader { [index: string]: any; } type LoaderContext = NormalModuleLoaderContext & LoaderRunnerLoaderContext; -declare interface LoaderDefinition { - ( - this: NormalModuleLoaderContext & LoaderRunnerLoaderContext & {}, - contents: string - ): string; -} declare interface LoaderItem { loader: string; options: any; @@ -12008,7 +12002,12 @@ declare namespace exports { export { HttpUriPlugin, HttpsUriPlugin }; } } - export type LoaderDefinition = LoaderDefinition; + export type LoaderDefinition = ( + this: NormalModuleLoaderContext & + LoaderRunnerLoaderContext & + EmptyContextAdditions, + contents: string + ) => string; export type WebpackPluginFunction = ( this: Compiler, compiler: Compiler From ee0313764d37ccb3fda3e5ddfc5f7815b6ea440c Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 20 Apr 2021 19:21:32 +0100 Subject: [PATCH 19/29] change import --- declarations/LoaderContext.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index e595539fa2f..29b39027910 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -1,5 +1,5 @@ import type { RawSourceMap } from "source-map"; -import type { Schema } from "schema-utils/declarations/validate"; +import type { Schema } from "schema-utils/declarations/ValidationError"; import type { AssetInfo, Configuration } from "../lib"; import Compilation from "../lib/Compilation"; import NormalModule, { InputFileSystem } from "../lib/NormalModule"; From cf1a1fea88f9c1d6886fb55b5423b530ce98e389 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 22 Apr 2021 10:01:20 +0200 Subject: [PATCH 20/29] fixup some types --- declarations/LoaderContext.d.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 29b39027910..01000181b01 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -66,7 +66,7 @@ export interface LoaderRunnerLoaderContext { */ cacheable(flag?: boolean): void; - callback(): WebpackLoaderContextCallback; + callback: WebpackLoaderContextCallback; /** * Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. @@ -120,7 +120,7 @@ export interface LoaderRunnerLoaderContext { readonly previousRequest: string; - readonly query: any; + readonly query: string; readonly remainingRequest: string; @@ -149,12 +149,12 @@ export interface LoaderRunnerLoaderContext { path: string; query: string; fragment: string; - options: any; + options: object | string | undefined; ident: string; - normal: any; - pitch: any; - raw: any; - data: any; + normal: Function | undefined; + pitch: Function | undefined; + raw: boolean | undefined; + data: object | undefined; pitchExecuted: boolean; normalExecuted: boolean; }[]; From 984308ada96c7e1851c28a874279c21cea50c599 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Thu, 22 Apr 2021 09:44:54 +0100 Subject: [PATCH 21/29] generate types with yarn special-lint-fix --- types.d.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/types.d.ts b/types.d.ts index fb2db0e8517..90ca28f38a5 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5818,7 +5818,7 @@ declare interface LoaderRunnerLoaderContext { * Most loaders are deterministic and cacheable. */ cacheable(flag?: boolean): void; - callback(): ( + callback: ( err: undefined | null | Error, content: undefined | string | Buffer, sourceMap: undefined | string | RawSourceMap, @@ -5872,7 +5872,7 @@ declare interface LoaderRunnerLoaderContext { ) => void ): void; readonly previousRequest: string; - readonly query: any; + readonly query: string; readonly remainingRequest: string; readonly request: string; @@ -5898,12 +5898,12 @@ declare interface LoaderRunnerLoaderContext { path: string; query: string; fragment: string; - options: any; + options?: string | object; ident: string; - normal: any; - pitch: any; - raw: any; - data: any; + normal?: Function; + pitch?: Function; + raw?: boolean; + data?: object; pitchExecuted: boolean; normalExecuted: boolean; }[]; From e42915cf49da9f24b688542d83480ca3973a3500 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 22 Apr 2021 10:54:50 +0200 Subject: [PATCH 22/29] clean up types --- declarations/LoaderContext.d.ts | 42 +++++++++++++++++-------- lib/index.js | 1 - types.d.ts | 54 +++++++++++++++++++++------------ 3 files changed, 63 insertions(+), 34 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 01000181b01..62704dd3af3 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -166,20 +166,36 @@ export interface LoaderRunnerLoaderContext { resourcePath: string; } +type AdditionalData = { + webpackAST: object; + [index: string]: any; +}; + type WebpackLoaderContextCallback = ( - err: Error | undefined | null, - content?: string | Buffer, - sourceMap?: string | RawSourceMap, - additionalData?: Record, - ...args: any[] -) => void | undefined; + err: Error | undefined | null, + content?: string | Buffer, + sourceMap?: string | RawSourceMap, + additionalData?: AdditionalData +) => void; type LoaderContext = NormalModuleLoaderContext & LoaderRunnerLoaderContext; -declare class EmptyContextAdditions { - _EmptyContextAdditions: true -} - -export interface LoaderDefinition { - (this: LoaderContext & EmptyContextAdditions, contents: string): string; -} +export type LoaderDefinition = + | { + ( + this: LoaderContext, + content: string, + sourceMap?: string | RawSourceMap, + additionalData?: AdditionalData + ): string | Buffer | Promise | void; + raw?: false; + } + | { + ( + this: LoaderContext, + content: Buffer, + sourceMap?: string | RawSourceMap, + additionalData?: AdditionalData + ): string | Buffer | Promise | void; + raw: true; + }; diff --git a/lib/index.js b/lib/index.js index cff404ac14d..ebf9b368c14 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,7 +8,6 @@ const util = require("util"); const memoize = require("./util/memoize"); -/** @typedef {import("../declarations/LoaderContext").EmptyContextAdditions} EmptyContextAdditions */ /** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ /** @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */ diff --git a/types.d.ts b/types.d.ts index 90ca28f38a5..1e2a4c2901c 100644 --- a/types.d.ts +++ b/types.d.ts @@ -152,6 +152,10 @@ declare class AbstractLibraryPlugin { ): void; static COMMON_LIBRARY_NAME_MESSAGE: string; } +declare interface AdditionalData { + [index: string]: any; + webpackAST: object; +} declare class AggressiveMergingPlugin { constructor(options?: any); options: any; @@ -2841,7 +2845,6 @@ declare class ElectronTargetPlugin { */ apply(compiler: Compiler): void; } -declare abstract class EmptyContextAdditions {} /** * No generator options are supported for this module type. @@ -5743,6 +5746,25 @@ declare interface Loader { [index: string]: any; } type LoaderContext = NormalModuleLoaderContext & LoaderRunnerLoaderContext; +type LoaderDefinition = + | { + ( + this: LoaderContext, + content: string, + sourceMap?: string | RawSourceMap, + additionalData?: AdditionalData + ): string | void | Buffer | Promise; + raw?: false; + } + | { + ( + this: LoaderContext, + content: Buffer, + sourceMap?: string | RawSourceMap, + additionalData?: AdditionalData + ): string | void | Buffer | Promise; + raw: true; + }; declare interface LoaderItem { loader: string; options: any; @@ -5804,12 +5826,11 @@ declare interface LoaderRunnerLoaderContext { * Make this loader async. */ async(): ( - err: undefined | null | Error, - content: undefined | string | Buffer, - sourceMap: undefined | string | RawSourceMap, - additionalData: undefined | Record, - ...args: any[] - ) => undefined | void; + err?: null | Error, + content?: string | Buffer, + sourceMap?: string | RawSourceMap, + additionalData?: AdditionalData + ) => void; /** * Make this loader result cacheable. By default it's cacheable. @@ -5819,12 +5840,11 @@ declare interface LoaderRunnerLoaderContext { */ cacheable(flag?: boolean): void; callback: ( - err: undefined | null | Error, - content: undefined | string | Buffer, - sourceMap: undefined | string | RawSourceMap, - additionalData: undefined | Record, - ...args: any[] - ) => undefined | void; + err?: null | Error, + content?: string | Buffer, + sourceMap?: string | RawSourceMap, + additionalData?: AdditionalData + ) => void; /** * Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. @@ -12002,12 +12022,6 @@ declare namespace exports { export { HttpUriPlugin, HttpsUriPlugin }; } } - export type LoaderDefinition = ( - this: NormalModuleLoaderContext & - LoaderRunnerLoaderContext & - EmptyContextAdditions, - contents: string - ) => string; export type WebpackPluginFunction = ( this: Compiler, compiler: Compiler @@ -12067,8 +12081,8 @@ declare namespace exports { WebpackError, WebpackOptionsApply, WebpackOptionsDefaulter, - EmptyContextAdditions, LoaderContext, + LoaderDefinition, Entry, EntryNormalized, EntryObject, From 7cc40782b77f7f8860b122d58011b5295780e5ff Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 22 Apr 2021 21:43:43 +0200 Subject: [PATCH 23/29] fix some internal types --- lib/Compilation.js | 4 ++-- lib/Compiler.js | 4 ++-- lib/EntryPlugin.js | 2 +- lib/NormalModule.js | 4 +++- lib/SourceMapDevToolPlugin.js | 2 +- types.d.ts | 10 +++++----- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/Compilation.js b/lib/Compilation.js index 74a8f705cb7..85ca0907cdb 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -3963,8 +3963,8 @@ This prevents using hashes of each other and should be avoided.`); * from parent (or top level compiler) and creates a child Compilation * * @param {string} name name of the child compiler - * @param {OutputOptions} outputOptions // Need to convert config schema to types for this - * @param {Array} plugins webpack plugins that will be applied + * @param {OutputOptions=} outputOptions // Need to convert config schema to types for this + * @param {Array=} plugins webpack plugins that will be applied * @returns {Compiler} creates a child Compiler instance */ createChildCompiler(name, outputOptions, plugins) { diff --git a/lib/Compiler.js b/lib/Compiler.js index c1340ab8e3f..09e1f995b9b 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -917,8 +917,8 @@ ${other}`); * @param {Compilation} compilation the compilation * @param {string} compilerName the compiler's name * @param {number} compilerIndex the compiler's index - * @param {OutputOptions} outputOptions the output options - * @param {WebpackPluginInstance[]} plugins the plugins to apply + * @param {OutputOptions=} outputOptions the output options + * @param {WebpackPluginInstance[]=} plugins the plugins to apply * @returns {Compiler} a child compiler */ createChildCompiler( diff --git a/lib/EntryPlugin.js b/lib/EntryPlugin.js index 61c57daf4b3..054bc36d35d 100644 --- a/lib/EntryPlugin.js +++ b/lib/EntryPlugin.js @@ -17,7 +17,7 @@ class EntryPlugin { * * @param {string} context context path * @param {string} entry entry path - * @param {EntryOptions | string} options entry options (passing a string is deprecated) + * @param {EntryOptions | string=} options entry options (passing a string is deprecated) */ constructor(context, entry, options) { this.context = context; diff --git a/lib/NormalModule.js b/lib/NormalModule.js index e2797a12ac2..733da0073b0 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -42,7 +42,7 @@ const { contextify, absolutify } = require("./util/identifier"); const makeSerializable = require("./util/makeSerializable"); const memoize = require("./util/memoize"); -/** @typedef {import("source-map").RawSourceMap} SourceMap */ +/** @typedef {import("source-map").RawSourceMap} RawSourceMap */ /** @typedef {import("webpack-sources").Source} Source */ /** @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext} NormalModuleLoaderContext */ /** @typedef {import("../declarations/WebpackOptions").Mode} Mode */ @@ -69,6 +69,8 @@ const memoize = require("./util/memoize"); /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */ +/** @typedef {Omit & { version: number }} SourceMap */ + const getInvalidDependenciesModuleWarning = memoize(() => require("./InvalidDependenciesModuleWarning") ); diff --git a/lib/SourceMapDevToolPlugin.js b/lib/SourceMapDevToolPlugin.js index 4b26e1cb3e4..f095c7277d5 100644 --- a/lib/SourceMapDevToolPlugin.js +++ b/lib/SourceMapDevToolPlugin.js @@ -18,7 +18,6 @@ const { absolutify } = require("./util/identifier"); const schema = require("../schemas/plugins/SourceMapDevToolPlugin.json"); -/** @typedef {import("source-map").RawSourceMap} SourceMap */ /** @typedef {import("webpack-sources").MapOptions} MapOptions */ /** @typedef {import("webpack-sources").Source} Source */ /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */ @@ -28,6 +27,7 @@ const schema = require("../schemas/plugins/SourceMapDevToolPlugin.json"); /** @typedef {import("./Compilation").AssetInfo} AssetInfo */ /** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Module")} Module */ +/** @typedef {import("./NormalModule").SourceMap} SourceMap */ /** @typedef {import("./util/Hash")} Hash */ /** diff --git a/types.d.ts b/types.d.ts index 1e2a4c2901c..70353a99410 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1648,8 +1648,8 @@ declare class Compilation { */ createChildCompiler( name: string, - outputOptions: OutputNormalized, - plugins: ( + outputOptions?: OutputNormalized, + plugins?: ( | ((this: Compiler, compiler: Compiler) => void) | WebpackPluginInstance )[] @@ -1853,8 +1853,8 @@ declare class Compiler { compilation: Compilation, compilerName: string, compilerIndex: number, - outputOptions: OutputNormalized, - plugins: WebpackPluginInstance[] + outputOptions?: OutputNormalized, + plugins?: WebpackPluginInstance[] ): Compiler; isChild(): boolean; createCompilation(): Compilation; @@ -3033,7 +3033,7 @@ declare class EntryPlugin { * An entry plugin which will handle * creation of the EntryDependency */ - constructor(context: string, entry: string, options: string | EntryOptions); + constructor(context: string, entry: string, options?: string | EntryOptions); context: string; entry: string; options: string | EntryOptions; From ea53a23827d53944d2604063f5ab25280d264068 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 22 Apr 2021 21:45:06 +0200 Subject: [PATCH 24/29] improve LoaderContext declaration upgrade tooling --- declarations/LoaderContext.d.ts | 189 ++++++++++++++++-------- declarations/index.d.ts | 9 ++ generate-types-config.js | 3 +- lib/index.js | 2 - package.json | 2 +- schemas/WebpackOptions.json | 8 +- types.d.ts | 249 ++++++++++++++++++++++++-------- yarn.lock | 20 ++- 8 files changed, 353 insertions(+), 129 deletions(-) create mode 100644 declarations/index.d.ts diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 62704dd3af3..3132cac4729 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -1,21 +1,31 @@ -import type { RawSourceMap } from "source-map"; +import type { SourceMap } from "../lib/NormalModule"; import type { Schema } from "schema-utils/declarations/ValidationError"; -import type { AssetInfo, Configuration } from "../lib"; -import Compilation from "../lib/Compilation"; -import NormalModule, { InputFileSystem } from "../lib/NormalModule"; -import type { Mode } from "./WebpackOptions"; +import type { AssetInfo } from "../lib/Compilation"; +import type { ResolveOptionsWithDependencyType } from "../lib/ResolverFactory"; +import type Compilation from "../lib/Compilation"; +import type Compiler from "../lib/Compiler"; +import type NormalModule, { InputFileSystem } from "../lib/NormalModule"; import type { Logger } from "../lib/logging/Logger"; +import type { + ImportModuleCallback, + ImportModuleOptions +} from "../lib/dependencies/LoaderPlugin"; +import type { Resolver } from "enhanced-resolve"; -export interface NormalModuleLoaderContext { +type ResolveCallback = Parameters[4]; + +/** These properties are added by the NormalModule */ +export interface NormalModuleLoaderContext { version: number; - getOptions(schema: Schema): any; - emitWarning(warning: Error | string): void; - emitError(error: Error | string): void; - getLogger(name: string): Logger; - resolve(context: string, request: string, callback: any): any; + getOptions(schema?: Schema): OptionsType; + emitWarning(warning: Error): void; + emitError(error: Error): void; + getLogger(name?: string): Logger; + resolve(context: string, request: string, callback: ResolveCallback): any; getResolve( - options: Configuration - ): (context: string, request: string, callback: any) => Promise; + options?: ResolveOptionsWithDependencyType + ): ((context: string, request: string, callback: ResolveCallback) => void) & + ((context: string, request: string) => Promise); emitFile( name: string, content: string, @@ -28,17 +38,48 @@ export interface NormalModuleLoaderContext { contextify: (context: string, request: string) => string; }; rootContext: string; - webpack?: boolean; + fs: InputFileSystem; sourceMap?: boolean; - mode: Mode; + mode: "development" | "production" | "none"; + webpack?: boolean; _module?: NormalModule; _compilation?: Compilation; - _compiler?: Compilation.Compiler; - fs: InputFileSystem; + _compiler?: Compiler; +} + +/** These properties are added by the HotModuleReplacementPlugin */ +export interface HotModuleReplacementPluginLoaderContext { + hot?: boolean; +} + +/** These properties are added by the LoaderPlugin */ +export interface LoaderPluginLoaderContext { + /** + * Resolves the given request to a module, applies all configured loaders and calls + * back with the generated source, the sourceMap and the module instance (usually an + * instance of NormalModule). Use this function if you need to know the source code + * of another module to generate the result. + */ + loadModule( + request: string, + callback: ( + err: Error | null, + source: string, + sourceMap: any, + module: NormalModule + ) => void + ): void; + + importModule( + request: string, + options: ImportModuleOptions, + callback: ImportModuleCallback + ): void; + importModule(request: string, options?: ImportModuleOptions): Promise; } -/** The types added to LoaderContextBase by https://github.com/webpack/loader-runner */ -export interface LoaderRunnerLoaderContext { +/** The properties are added by https://github.com/webpack/loader-runner */ +export interface LoaderRunnerLoaderContext { /** * Add a directory as dependency of the loader result. */ @@ -102,25 +143,9 @@ export interface LoaderRunnerLoaderContext { */ loaderIndex: number; - /** - * Resolves the given request to a module, applies all configured loaders and calls - * back with the generated source, the sourceMap and the module instance (usually an - * instance of NormalModule). Use this function if you need to know the source code - * of another module to generate the result. - */ - loadModule( - request: string, - callback: ( - err: Error | null, - source: string, - sourceMap: any, - module: NormalModule - ) => void - ): void; - readonly previousRequest: string; - readonly query: string; + readonly query: string | OptionsType; readonly remainingRequest: string; @@ -160,10 +185,28 @@ export interface LoaderRunnerLoaderContext { }[]; /** - * The resource file. + * The resource path. * In the example: "/abc/resource.js" */ resourcePath: string; + + /** + * The resource query string. + * Example: "?query" + */ + resourceQuery: string; + + /** + * The resource fragment. + * Example: "#frag" + */ + resourceFragment: string; + + /** + * The resource inclusive query and fragment. + * Example: "/abc/resource.js?query#frag" + */ + resource: string; } type AdditionalData = { @@ -174,28 +217,56 @@ type AdditionalData = { type WebpackLoaderContextCallback = ( err: Error | undefined | null, content?: string | Buffer, - sourceMap?: string | RawSourceMap, + sourceMap?: string | SourceMap, additionalData?: AdditionalData ) => void; -type LoaderContext = NormalModuleLoaderContext & LoaderRunnerLoaderContext; - -export type LoaderDefinition = - | { - ( - this: LoaderContext, - content: string, - sourceMap?: string | RawSourceMap, - additionalData?: AdditionalData - ): string | Buffer | Promise | void; - raw?: false; - } - | { - ( - this: LoaderContext, - content: Buffer, - sourceMap?: string | RawSourceMap, - additionalData?: AdditionalData - ): string | Buffer | Promise | void; - raw: true; - }; +type LoaderContext = NormalModuleLoaderContext & + LoaderRunnerLoaderContext & + LoaderPluginLoaderContext & + HotModuleReplacementPluginLoaderContext; + +type PitchLoaderDefinitionFunction = ( + this: LoaderContext & ContextAdditions, + remainingRequest: string, + previousRequest: string, + data: object +) => string | Buffer | Promise | void; + +type LoaderDefinitionFunction = ( + this: LoaderContext & ContextAdditions, + content: string, + sourceMap?: string | SourceMap, + additionalData?: AdditionalData +) => string | Buffer | Promise | void; + +type RawLoaderDefinitionFunction = ( + this: LoaderContext & ContextAdditions, + content: Buffer, + sourceMap?: string | SourceMap, + additionalData?: AdditionalData +) => string | Buffer | Promise | void; + +export type LoaderDefinition< + OptionsType = {}, + ContextAdditions = {} +> = LoaderDefinitionFunction & { + raw?: false; + pitch?: PitchLoaderDefinitionFunction; +}; + +export type RawLoaderDefinition< + OptionsType = {}, + ContextAdditions = {} +> = RawLoaderDefinitionFunction & { + raw: true; + pitch?: PitchLoaderDefinitionFunction; +}; + +export interface LoaderModule { + default?: + | RawLoaderDefinitionFunction + | LoaderDefinitionFunction; + raw?: false; + pitch?: PitchLoaderDefinitionFunction; +} diff --git a/declarations/index.d.ts b/declarations/index.d.ts new file mode 100644 index 00000000000..a9475f0809c --- /dev/null +++ b/declarations/index.d.ts @@ -0,0 +1,9 @@ +export type { + LoaderModule, + RawLoaderDefinition, + LoaderDefinition, + LoaderDefinitionFunction, + PitchLoaderDefinitionFunction, + RawLoaderDefinitionFunction, + LoaderContext +} from "./LoaderContext"; diff --git a/generate-types-config.js b/generate-types-config.js index 59d8b78920c..b8890a8ea85 100644 --- a/generate-types-config.js +++ b/generate-types-config.js @@ -3,5 +3,6 @@ module.exports = { FsStats: /^Stats Import fs/, Configuration: /^WebpackOptions / }, - exclude: [/^devServer in WebpackOptions /] + exclude: [/^devServer in WebpackOptions /], + include: [/^(_module|_compilation|_compiler) in NormalModuleLoaderContext /] }; diff --git a/lib/index.js b/lib/index.js index ebf9b368c14..dfcf631fa60 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,8 +8,6 @@ const util = require("util"); const memoize = require("./util/memoize"); -/** @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext */ -/** @typedef {import("../declarations/LoaderContext").LoaderDefinition} LoaderDefinition */ /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */ /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} EntryNormalized */ /** @typedef {import("../declarations/WebpackOptions").EntryObject} EntryObject */ diff --git a/package.json b/package.json index bdbe86b200d..1da3261ca35 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "style-loader": "^2.0.0", "terser": "^5.5.0", "toml": "^3.0.0", - "tooling": "webpack/tooling#v1.15.0", + "tooling": "webpack/tooling#v1.18.0", "ts-loader": "^8.0.2", "typescript": "^4.2.0-beta", "url-loader": "^4.1.0", diff --git a/schemas/WebpackOptions.json b/schemas/WebpackOptions.json index a63b829380e..b059a4102e7 100644 --- a/schemas/WebpackOptions.json +++ b/schemas/WebpackOptions.json @@ -2325,15 +2325,15 @@ "description": "The test property is a cache group name, but using the test option of the cache group could be intended instead.", "anyOf": [ { - "instanceof": "Function", - "tsType": "Function" + "instanceof": "RegExp", + "tsType": "RegExp" }, { "type": "string" }, { - "instanceof": "RegExp", - "tsType": "RegExp" + "instanceof": "Function", + "tsType": "Function" } ] } diff --git a/types.d.ts b/types.d.ts index 70353a99410..5d888778337 100644 --- a/types.d.ts +++ b/types.d.ts @@ -4204,6 +4204,13 @@ declare class HotModuleReplacementPlugin { apply(compiler: Compiler): void; static getParserHooks(parser: JavascriptParser): HMRJavascriptParserHooks; } + +/** + * These properties are added by the HotModuleReplacementPlugin + */ +declare interface HotModuleReplacementPluginLoaderContext { + hot?: boolean; +} declare class HotUpdateChunk extends Chunk { constructor(); } @@ -4292,6 +4299,17 @@ type IgnorePluginOptions = */ checkResource?: (resource: string, context: string) => boolean; }; +declare interface ImportModuleOptions { + /** + * the target layer + */ + layer?: string; + + /** + * the target public path + */ + publicPath?: string; +} type ImportSource = undefined | null | string | SimpleLiteral | RegExpLiteral; /** @@ -5745,32 +5763,45 @@ declare class LoadScriptRuntimeModule extends HelperRuntimeModule { declare interface Loader { [index: string]: any; } -type LoaderContext = NormalModuleLoaderContext & LoaderRunnerLoaderContext; -type LoaderDefinition = - | { - ( - this: LoaderContext, - content: string, - sourceMap?: string | RawSourceMap, - additionalData?: AdditionalData - ): string | void | Buffer | Promise; - raw?: false; - } - | { - ( - this: LoaderContext, - content: Buffer, - sourceMap?: string | RawSourceMap, - additionalData?: AdditionalData - ): string | void | Buffer | Promise; - raw: true; - }; +type LoaderContext = NormalModuleLoaderContext & + LoaderRunnerLoaderContext & + LoaderPluginLoaderContext & + HotModuleReplacementPluginLoaderContext; +type LoaderDefinition< + OptionsType = {}, + ContextAdditions = {} +> = LoaderDefinitionFunction & { + raw?: false; + pitch?: PitchLoaderDefinitionFunction; +}; +declare interface LoaderDefinitionFunction< + OptionsType = {}, + ContextAdditions = {} +> { + ( + this: NormalModuleLoaderContext & + LoaderRunnerLoaderContext & + LoaderPluginLoaderContext & + HotModuleReplacementPluginLoaderContext & + ContextAdditions, + content: string, + sourceMap?: string | SourceMap, + additionalData?: AdditionalData + ): string | void | Buffer | Promise; +} declare interface LoaderItem { loader: string; options: any; ident: null | string; type: null | string; } +declare interface LoaderModule { + default?: + | RawLoaderDefinitionFunction + | LoaderDefinitionFunction; + raw?: false; + pitch?: PitchLoaderDefinitionFunction; +} declare class LoaderOptionsPlugin { constructor(options?: LoaderOptionsPluginOptions); options: LoaderOptionsPluginOptions; @@ -5806,9 +5837,36 @@ declare interface LoaderOptionsPluginOptions { } /** - * The types added to LoaderContextBase by https://github.com/webpack/loader-runner + * These properties are added by the LoaderPlugin + */ +declare interface LoaderPluginLoaderContext { + /** + * Resolves the given request to a module, applies all configured loaders and calls + * back with the generated source, the sourceMap and the module instance (usually an + * instance of NormalModule). Use this function if you need to know the source code + * of another module to generate the result. + */ + loadModule( + request: string, + callback: ( + err: null | Error, + source: string, + sourceMap: any, + module: NormalModule + ) => void + ): void; + importModule( + request: string, + options: ImportModuleOptions, + callback: (err?: Error, exports?: any) => any + ): void; + importModule(request: string, options?: ImportModuleOptions): Promise; +} + +/** + * The properties are added by https://github.com/webpack/loader-runner */ -declare interface LoaderRunnerLoaderContext { +declare interface LoaderRunnerLoaderContext { /** * Add a directory as dependency of the loader result. */ @@ -5828,7 +5886,7 @@ declare interface LoaderRunnerLoaderContext { async(): ( err?: null | Error, content?: string | Buffer, - sourceMap?: string | RawSourceMap, + sourceMap?: string | SourceMap, additionalData?: AdditionalData ) => void; @@ -5842,7 +5900,7 @@ declare interface LoaderRunnerLoaderContext { callback: ( err?: null | Error, content?: string | Buffer, - sourceMap?: string | RawSourceMap, + sourceMap?: string | SourceMap, additionalData?: AdditionalData ) => void; @@ -5875,24 +5933,8 @@ declare interface LoaderRunnerLoaderContext { * In the example: in loader1: 0, in loader2: 1 */ loaderIndex: number; - - /** - * Resolves the given request to a module, applies all configured loaders and calls - * back with the generated source, the sourceMap and the module instance (usually an - * instance of NormalModule). Use this function if you need to know the source code - * of another module to generate the result. - */ - loadModule( - request: string, - callback: ( - err: null | Error, - source: string, - sourceMap: any, - module: NormalModule - ) => void - ): void; readonly previousRequest: string; - readonly query: string; + readonly query: string | OptionsType; readonly remainingRequest: string; readonly request: string; @@ -5929,10 +5971,28 @@ declare interface LoaderRunnerLoaderContext { }[]; /** - * The resource file. + * The resource path. * In the example: "/abc/resource.js" */ resourcePath: string; + + /** + * The resource query string. + * Example: "?query" + */ + resourceQuery: string; + + /** + * The resource fragment. + * Example: "#frag" + */ + resourceFragment: string; + + /** + * The resource inclusive query and fragment. + * Example: "/abc/resource.js?query#frag" + */ + resource: string; } declare class LoaderTargetPlugin { constructor(target: string); @@ -6079,7 +6139,6 @@ declare interface MinChunkSizePluginOptions { */ minChunkSize: number; } -type Mode = "development" | "production" | "none"; declare class Module extends DependenciesBlock { constructor(type: string, context?: string, layer?: string); type: string; @@ -7003,7 +7062,7 @@ declare class NormalModule extends Module { options: WebpackOptionsNormalized, compilation: Compilation, fs: InputFileSystem - ): NormalModuleLoaderContext; + ): NormalModuleLoaderContext; getCurrentLoader(loaderContext?: any, index?: any): null | LoaderItem; createSource( context: string, @@ -7079,16 +7138,44 @@ declare abstract class NormalModuleFactory extends ModuleFactory { createGenerator(type?: any, generatorOptions?: object): any; getResolver(type?: any, resolveOptions?: any): ResolverWithOptions; } -declare interface NormalModuleLoaderContext { + +/** + * These properties are added by the NormalModule + */ +declare interface NormalModuleLoaderContext { version: number; - getOptions(schema: Schema): any; - emitWarning(warning: string | Error): void; - emitError(error: string | Error): void; - getLogger(name: string): WebpackLogger; - resolve(context: string, request: string, callback?: any): any; + getOptions( + schema?: + | (JSONSchema4 & Extend) + | (JSONSchema6 & Extend) + | (JSONSchema7 & Extend) + ): OptionsType; + emitWarning(warning: Error): void; + emitError(error: Error): void; + getLogger(name?: string): WebpackLogger; + resolve( + context: string, + request: string, + callback: ( + arg0: null | Error, + arg1?: string | false, + arg2?: ResolveRequest + ) => void + ): any; getResolve( - options: Configuration - ): (context: string, request: string, callback?: any) => Promise; + options?: ResolveOptionsWithDependencyType + ): { + ( + context: string, + request: string, + callback: ( + arg0: null | Error, + arg1?: string | false, + arg2?: ResolveRequest + ) => void + ): void; + (context: string, request: string): Promise; + }; emitFile( name: string, content: string, @@ -7101,10 +7188,13 @@ declare interface NormalModuleLoaderContext { contextify: (context: string, request: string) => string; }; rootContext: string; - webpack?: boolean; - sourceMap?: boolean; - mode: Mode; fs: InputFileSystem; + sourceMap?: boolean; + mode: "development" | "production" | "none"; + webpack?: boolean; + _module?: NormalModule; + _compilation?: Compilation; + _compiler?: Compiler; } declare class NormalModuleReplacementPlugin { /** @@ -8225,6 +8315,21 @@ declare interface PerformanceOptions { */ maxEntrypointSize?: number; } +declare interface PitchLoaderDefinitionFunction< + OptionsType = {}, + ContextAdditions = {} +> { + ( + this: NormalModuleLoaderContext & + LoaderRunnerLoaderContext & + LoaderPluginLoaderContext & + HotModuleReplacementPluginLoaderContext & + ContextAdditions, + remainingRequest: string, + previousRequest: string, + data: object + ): string | void | Buffer | Promise; +} type Plugin = | { apply: (arg0: Resolver) => void } | ((this: Resolver, arg1: Resolver) => void); @@ -8441,6 +8546,28 @@ declare interface RawChunkGroupOptions { preloadOrder?: number; prefetchOrder?: number; } +type RawLoaderDefinition< + OptionsType = {}, + ContextAdditions = {} +> = RawLoaderDefinitionFunction & { + raw: true; + pitch?: PitchLoaderDefinitionFunction; +}; +declare interface RawLoaderDefinitionFunction< + OptionsType = {}, + ContextAdditions = {} +> { + ( + this: NormalModuleLoaderContext & + LoaderRunnerLoaderContext & + LoaderPluginLoaderContext & + HotModuleReplacementPluginLoaderContext & + ContextAdditions, + content: Buffer, + sourceMap?: string | SourceMap, + additionalData?: AdditionalData + ): string | void | Buffer | Promise; +} declare class RawSource extends Source { constructor(source: string | Buffer, convertToString?: boolean); isBuffer(): boolean; @@ -10305,6 +10432,7 @@ declare interface SourceData { declare interface SourceLike { source(): string | Buffer; } +type SourceMap = Omit & { version: number }; declare class SourceMapDevToolPlugin { constructor(options?: SourceMapDevToolPluginOptions); sourceMapFilename: string | false; @@ -12081,8 +12209,6 @@ declare namespace exports { WebpackError, WebpackOptionsApply, WebpackOptionsDefaulter, - LoaderContext, - LoaderDefinition, Entry, EntryNormalized, EntryObject, @@ -12115,7 +12241,14 @@ declare namespace exports { StatsModuleReason, StatsModuleTraceDependency, StatsModuleTraceItem, - StatsProfile + StatsProfile, + LoaderModule, + RawLoaderDefinition, + LoaderDefinition, + LoaderDefinitionFunction, + PitchLoaderDefinitionFunction, + RawLoaderDefinitionFunction, + LoaderContext }; } diff --git a/yarn.lock b/yarn.lock index 0c96658a350..735a6f39d1a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1226,6 +1226,16 @@ ajv@^7.0.2: require-from-string "^2.0.2" uri-js "^4.2.2" +ajv@^8.1.0: + version "8.1.0" + resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.1.0.tgz#45d5d3d36c7cdd808930cc3e603cf6200dbeb736" + integrity sha512-B/Sk2Ix7A36fs/ZkuGLIR86EdjbgR6fsAcbx9lOP/QBSXujDNbVmIS/U4Itz5k8fPFDeVZl/zQ/gJW4Jrq6XjQ== + dependencies: + fast-deep-equal "^3.1.1" + json-schema-traverse "^1.0.0" + require-from-string "^2.0.2" + uri-js "^4.2.2" + amdefine@>=0.0.4: version "1.0.1" resolved "https://registry.yarnpkg.com/amdefine/-/amdefine-1.0.1.tgz#4a5282ac164729e93619bcfd3ad151f817ce91f5" @@ -6138,7 +6148,7 @@ terser-webpack-plugin@^5.1.1: source-map "^0.6.1" terser "^5.5.1" -terser@^5.5.0, terser@^5.5.1: +terser@^5.5.0, terser@^5.5.1, terser@^5.6.1: version "5.6.1" resolved "https://registry.yarnpkg.com/terser/-/terser-5.6.1.tgz#a48eeac5300c0a09b36854bf90d9c26fb201973c" integrity sha512-yv9YLFQQ+3ZqgWCUk+pvNJwgUTdlIxUk1WTN+RnaFJe2L7ipG2csPT0ra2XRm7Cs8cxN7QXmK1rFzEwYEQkzXw== @@ -6253,14 +6263,16 @@ toml@^3.0.0: resolved "https://registry.yarnpkg.com/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee" integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w== -tooling@webpack/tooling#v1.15.0: - version "1.15.0" - resolved "https://codeload.github.com/webpack/tooling/tar.gz/1c8c975ada1d94d011a4bc00fb149b168cc60580" +tooling@webpack/tooling#v1.18.0: + version "1.18.0" + resolved "https://codeload.github.com/webpack/tooling/tar.gz/27496b1099c136e4a8bd69b6d4991c3a493d4a4c" dependencies: "@yarnpkg/lockfile" "^1.1.0" + ajv "^8.1.0" commondir "^1.0.1" glob "^7.1.6" json-schema-to-typescript "^9.1.1" + terser "^5.6.1" yargs "^16.1.1" tough-cookie@^2.3.3, tough-cookie@~2.5.0: From 4e70b375799662ffc379f86d7917dea1c1225b30 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 22 Apr 2021 21:45:45 +0200 Subject: [PATCH 25/29] test typings and fix type problems in loaders in the test suite --- test/cases/compile/error-hide-stack/loader.js | 4 ++- test/cases/errors/load-module-cycle/loader.js | 3 ++- test/cases/errors/load-module-error/loader.js | 3 ++- .../loader-error-warning/error-loader.js | 6 +++-- .../loader-error-warning/warning-loader.js | 6 +++-- .../loaders/async/loaders/asyncloader.js | 12 +++++---- .../cases/loaders/async/loaders/syncloader.js | 5 ++-- test/cases/loaders/emit-file/loader.js | 5 ++-- test/cases/loaders/issue-10725/loader.js | 9 ++++--- .../module-description-file/reverseloader.js | 5 ++-- .../loaders/query/loaders/queryloader.js | 18 ++++++++----- test/cases/loaders/resolve/loader.js | 3 ++- test/cases/loaders/utils/loader.js | 1 + .../parsing/context/loaders/queryloader.js | 16 +++++++----- .../parsing/precreated-ast/ast-loader.js | 8 ++++-- .../resolving/context/loaders/queryloader.js | 18 ++++++++----- .../wasm/two-files-loader/wrapper-loader.js | 1 + .../wasm/two-files-loader/wrapper-loader2.js | 1 + .../managed-items/loader.js | 2 ++ .../context-replacement/d/queryloader.js | 16 +++++++----- .../invalid-dependencies/loader.js | 1 + .../dll-plugin/0-create-dll/g-loader.js | 3 ++- .../inner-graph/_helpers/entryLoader.js | 3 ++- .../inner-graph/_helpers/testModuleLoader.js | 3 ++- test/configCases/layer/rules/loader.js | 1 + .../loader-import-module/css/loader.js | 1 + .../loaders/generate-ident/loader1.js | 7 +++-- .../loaders/generate-ident/loader2.js | 5 +++- .../loaders/hot-in-context/loader.js | 5 ++-- .../loaders/mode-default/loader.js | 3 ++- .../loaders/mode-development/loader.js | 3 ++- test/configCases/loaders/mode-none/loader.js | 3 ++- .../loaders/mode-production/loader.js | 3 ++- test/configCases/loaders/options/loader-1.js | 5 ++-- test/configCases/loaders/options/loader-2.js | 5 ++-- test/configCases/loaders/options/loader.js | 7 ++--- .../loaders/pre-post-loader/loader1.js | 5 ++-- .../loaders/pre-post-loader/loader2.js | 5 ++-- .../loaders/pre-post-loader/loader3.js | 5 ++-- .../loaders/remaining-request/loader1.js | 7 +++-- .../loaders/remaining-request/loader2.js | 5 +++- .../loader-a.js | 3 ++- .../loader-b.js | 3 ++- .../many-async-imports/reexport.loader.js | 7 ++--- .../performance/many-exports/file.loader.js | 7 ++--- .../many-exports/reexport.loader.js | 9 ++++--- .../plugins/loader-options-plugin/loader.js | 14 ++++++---- .../race-conditions/load-module/loader.js | 3 ++- .../rebuild/finishModules/loader.js | 1 + .../rebuildWithNewDependencies/loader.js | 1 + test/configCases/records/issue-295/loader.js | 3 ++- .../resolve-merging/override/loader.js | 2 ++ test/configCases/rule-set/chaining/loader.js | 6 ++--- test/configCases/rule-set/compiler/loader.js | 4 +-- test/configCases/rule-set/custom/loader.js | 6 ++--- test/configCases/rule-set/query/loader.js | 6 ++--- .../rule-set/simple-use-array-fn/loader.js | 6 ++--- .../rule-set/simple-use-fn-array/loader.js | 6 ++--- test/configCases/rule-set/simple/loader.js | 6 ++--- .../source-map/no-source-map/loader.js | 2 ++ .../loader-no-source-root.js | 4 ++- .../loader-pre-relative.js | 4 ++- .../loader-source-root-2-slash.js | 4 ++- .../loader-source-root-slash.js | 4 ++- .../loader-source-root-source-slash.js | 4 ++- .../loader-source-root.js | 4 ++- test/fixtures/count-loader.js | 3 ++- test/fixtures/delay-loader.js | 7 ++--- test/fixtures/errors/add-comment-loader.js | 3 ++- test/fixtures/errors/async-error-loader.js | 3 ++- test/fixtures/errors/emit-error-loader.js | 3 ++- test/fixtures/errors/identity-loader.js | 3 ++- .../fixtures/errors/irregular-error-loader.js | 3 ++- test/fixtures/errors/no-return-loader.js | 4 +-- test/fixtures/errors/throw-error-loader.js | 3 ++- .../issue-9706/report-child-assets-loader.js | 1 + test/hotCases/fake-update-loader.js | 3 ++- .../loader-import-module/css/loader.js | 1 + .../recover-after-loader-error/loader.js | 6 ++--- .../0/report-cache-counters-loader.js | 26 ++++++++++++------- .../cache/loader-import-module/0/loader.js | 1 + .../context/loader-context-dep/0/loader.js | 3 ++- test/watchCases/resolve/in-loader/0/loader.js | 4 ++- .../0/warning-loader.js | 3 ++- tsconfig.test.json | 9 ++++++- 85 files changed, 283 insertions(+), 152 deletions(-) diff --git a/test/cases/compile/error-hide-stack/loader.js b/test/cases/compile/error-hide-stack/loader.js index 674e66c655f..b499c32a083 100644 --- a/test/cases/compile/error-hide-stack/loader.js +++ b/test/cases/compile/error-hide-stack/loader.js @@ -1,6 +1,8 @@ -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { var err = new Error("Message"); err.stack = "Stack"; + //@ts-expect-error hideStack is not a property on normal errors err.hideStack = true; throw err; }; diff --git a/test/cases/errors/load-module-cycle/loader.js b/test/cases/errors/load-module-cycle/loader.js index 19a8c699bc9..ed4a740b947 100644 --- a/test/cases/errors/load-module-cycle/loader.js +++ b/test/cases/errors/load-module-cycle/loader.js @@ -1,4 +1,5 @@ -exports.default = function(source) { +/** @type {import("../../../../").LoaderDefinitionFunction} */ +exports.default = function (source) { const ref = JSON.parse(source); const callback = this.async(); this.loadModule("../loader!" + ref, (err, source, sourceMap, module) => { diff --git a/test/cases/errors/load-module-error/loader.js b/test/cases/errors/load-module-error/loader.js index 6001a39c5df..3eb4fa42c63 100644 --- a/test/cases/errors/load-module-error/loader.js +++ b/test/cases/errors/load-module-error/loader.js @@ -1,4 +1,5 @@ -exports.default = function(source) { +/** @type {import("../../../../").LoaderDefinitionFunction} */ +exports.default = function (source) { const callback = this.async(); const ref = JSON.parse(source); this.loadModule("./error-loader!" + ref, (err, source, sourceMap, module) => { diff --git a/test/cases/errors/loader-error-warning/error-loader.js b/test/cases/errors/loader-error-warning/error-loader.js index 175192c08da..8c63082d417 100644 --- a/test/cases/errors/loader-error-warning/error-loader.js +++ b/test/cases/errors/loader-error-warning/error-loader.js @@ -1,4 +1,6 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { + //@ts-expect-error errors must be Errors, string is not recommended and should lead to type error this.emitError(this.query.substr(1)); return source; -} +}; diff --git a/test/cases/errors/loader-error-warning/warning-loader.js b/test/cases/errors/loader-error-warning/warning-loader.js index 05142648f6c..adc1a120a84 100644 --- a/test/cases/errors/loader-error-warning/warning-loader.js +++ b/test/cases/errors/loader-error-warning/warning-loader.js @@ -1,4 +1,6 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { + //@ts-expect-error warnings must be Errors, string is not recommended and should lead to type error this.emitWarning(this.query.substr(1)); return source; -} +}; diff --git a/test/cases/loaders/async/loaders/asyncloader.js b/test/cases/loaders/async/loaders/asyncloader.js index d9be05de326..c6c0eb8d422 100644 --- a/test/cases/loaders/async/loaders/asyncloader.js +++ b/test/cases/loaders/async/loaders/asyncloader.js @@ -1,8 +1,10 @@ -module.exports = function(content) { +/** @type {import("../../../../../").LoaderDefinition} */ +module.exports = function (content) { var cb = this.async(); - if(!cb) throw new Error("Loader should allow async mode"); - if(cb !== this.callback) throw new Error("result of this.async() should be equal to this.callback"); - process.nextTick(function() { + if (!cb) throw new Error("Loader should allow async mode"); + if (cb !== this.callback) + throw new Error("result of this.async() should be equal to this.callback"); + process.nextTick(function () { cb(null, content); }); -}; \ No newline at end of file +}; diff --git a/test/cases/loaders/async/loaders/syncloader.js b/test/cases/loaders/async/loaders/syncloader.js index 0356c896f23..fe0c014dba4 100644 --- a/test/cases/loaders/async/loaders/syncloader.js +++ b/test/cases/loaders/async/loaders/syncloader.js @@ -1,3 +1,4 @@ -module.exports = function(content) { +/** @type {import("../../../../../").LoaderDefinition} */ +module.exports = function (content) { return content; -}; \ No newline at end of file +}; diff --git a/test/cases/loaders/emit-file/loader.js b/test/cases/loaders/emit-file/loader.js index 126cb485d51..c53b3e18fbc 100644 --- a/test/cases/loaders/emit-file/loader.js +++ b/test/cases/loaders/emit-file/loader.js @@ -1,4 +1,5 @@ -module.exports = function(content) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (content) { this.emitFile("extra-file.js", content); return ""; -} +}; diff --git a/test/cases/loaders/issue-10725/loader.js b/test/cases/loaders/issue-10725/loader.js index 7c3bb85cd93..af9af2d2418 100644 --- a/test/cases/loaders/issue-10725/loader.js +++ b/test/cases/loaders/issue-10725/loader.js @@ -2,22 +2,25 @@ const { getRemainingRequest, stringifyRequest } = require("loader-utils"); const loaderPath = require.resolve("./loader"); +/** @type {import("../../../../").LoaderDefinition} */ module.exports = function () { if (this.query === "?load") { return ` import { answer } from "./lib"; export default answer; -` +`; } const matchResource = `${this.resourcePath}.js`; const loader = `${loaderPath}?load`; const remaining = getRemainingRequest(this); - const request = JSON.parse(stringifyRequest(this, `${matchResource}!=!${loader}!${remaining}`)); + const request = JSON.parse( + stringifyRequest(this, `${matchResource}!=!${loader}!${remaining}`) + ); this.async(); this.loadModule(request, (err, source) => { - this.callback(err, source) + this.callback(err, source); }); }; diff --git a/test/cases/loaders/module-description-file/reverseloader.js b/test/cases/loaders/module-description-file/reverseloader.js index 2983d5650c6..4cbb644664a 100644 --- a/test/cases/loaders/module-description-file/reverseloader.js +++ b/test/cases/loaders/module-description-file/reverseloader.js @@ -1,3 +1,4 @@ -module.exports = function(content) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (content) { return content.split("").reverse().join(""); -} +}; diff --git a/test/cases/loaders/query/loaders/queryloader.js b/test/cases/loaders/query/loaders/queryloader.js index 8d606f560f8..f9bb23e1f55 100644 --- a/test/cases/loaders/query/loaders/queryloader.js +++ b/test/cases/loaders/query/loaders/queryloader.js @@ -1,7 +1,11 @@ -module.exports = function(content) { - return "module.exports = " + JSON.stringify({ - resourceQuery: this.resourceQuery, - query: this.query, - prev: content - }); -} +/** @type {import("../../../../../").LoaderDefinition} */ +module.exports = function (content) { + return ( + "module.exports = " + + JSON.stringify({ + resourceQuery: this.resourceQuery, + query: this.query, + prev: content + }) + ); +}; diff --git a/test/cases/loaders/resolve/loader.js b/test/cases/loaders/resolve/loader.js index 3bb4b23d839..53fc4aaf2f1 100644 --- a/test/cases/loaders/resolve/loader.js +++ b/test/cases/loaders/resolve/loader.js @@ -1,5 +1,6 @@ const path = require("path"); -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { const resolve1 = this.getResolve(); const resolve2 = this.getResolve({ extensions: [".xyz", ".js"] diff --git a/test/cases/loaders/utils/loader.js b/test/cases/loaders/utils/loader.js index 3ee7d109250..2d9e6e37073 100644 --- a/test/cases/loaders/utils/loader.js +++ b/test/cases/loaders/utils/loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../").LoaderDefinition} */ module.exports = function () { return `module.exports = { request1: ${JSON.stringify( diff --git a/test/cases/parsing/context/loaders/queryloader.js b/test/cases/parsing/context/loaders/queryloader.js index 02707b2ba37..f9bb23e1f55 100644 --- a/test/cases/parsing/context/loaders/queryloader.js +++ b/test/cases/parsing/context/loaders/queryloader.js @@ -1,7 +1,11 @@ -module.exports = function(content) { - return "module.exports = " + JSON.stringify({ - resourceQuery: this.resourceQuery, - query: this.query, - prev: content - }); +/** @type {import("../../../../../").LoaderDefinition} */ +module.exports = function (content) { + return ( + "module.exports = " + + JSON.stringify({ + resourceQuery: this.resourceQuery, + query: this.query, + prev: content + }) + ); }; diff --git a/test/cases/parsing/precreated-ast/ast-loader.js b/test/cases/parsing/precreated-ast/ast-loader.js index 6293e064441..e150377260e 100644 --- a/test/cases/parsing/precreated-ast/ast-loader.js +++ b/test/cases/parsing/precreated-ast/ast-loader.js @@ -3,7 +3,8 @@ const acorn = require("acorn"); const acornParser = acorn.Parser; -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { const comments = []; const ast = acornParser.parse(source, { @@ -15,9 +16,12 @@ module.exports = function(source) { }); // change something to test if it's really used + //@ts-ignore ast.body[0].expression.right.arguments[0].value = "./ok"; - ast.body[0].expression.right.arguments[0].raw = "\"./ok\""; + //@ts-ignore + ast.body[0].expression.right.arguments[0].raw = '"./ok"'; + //@ts-ignore ast.comments = comments; this.callback(null, source, null, { webpackAST: ast diff --git a/test/cases/resolving/context/loaders/queryloader.js b/test/cases/resolving/context/loaders/queryloader.js index 8d606f560f8..f9bb23e1f55 100644 --- a/test/cases/resolving/context/loaders/queryloader.js +++ b/test/cases/resolving/context/loaders/queryloader.js @@ -1,7 +1,11 @@ -module.exports = function(content) { - return "module.exports = " + JSON.stringify({ - resourceQuery: this.resourceQuery, - query: this.query, - prev: content - }); -} +/** @type {import("../../../../../").LoaderDefinition} */ +module.exports = function (content) { + return ( + "module.exports = " + + JSON.stringify({ + resourceQuery: this.resourceQuery, + query: this.query, + prev: content + }) + ); +}; diff --git a/test/cases/wasm/two-files-loader/wrapper-loader.js b/test/cases/wasm/two-files-loader/wrapper-loader.js index 736d6fa0cf8..827857a6b8a 100644 --- a/test/cases/wasm/two-files-loader/wrapper-loader.js +++ b/test/cases/wasm/two-files-loader/wrapper-loader.js @@ -1,5 +1,6 @@ const stringifyRequest = require("loader-utils").stringifyRequest; +/** @type {import("../../../../").PitchLoaderDefinitionFunction} */ module.exports.pitch = function (remainingRequest) { return ` import { getString as _getString, memory } from ${stringifyRequest( diff --git a/test/cases/wasm/two-files-loader/wrapper-loader2.js b/test/cases/wasm/two-files-loader/wrapper-loader2.js index 478a1f4f427..dde8826aa73 100644 --- a/test/cases/wasm/two-files-loader/wrapper-loader2.js +++ b/test/cases/wasm/two-files-loader/wrapper-loader2.js @@ -1,5 +1,6 @@ const stringifyRequest = require("loader-utils").stringifyRequest; +/** @type {import("../../../../").PitchLoaderDefinitionFunction} */ module.exports.pitch = function (remainingRequest) { return ` import { getString as _getString, memory } from ${stringifyRequest( diff --git a/test/configCases/cache-dependencies/managed-items/loader.js b/test/configCases/cache-dependencies/managed-items/loader.js index 82546670808..3a6935623f6 100644 --- a/test/configCases/cache-dependencies/managed-items/loader.js +++ b/test/configCases/cache-dependencies/managed-items/loader.js @@ -1,4 +1,6 @@ const path = require("path"); + +/** @type {import("../../../../").LoaderDefinition} */ module.exports = function (source) { this.addDependency(path.resolve(__dirname, "node_modules/package/extra.js")); this.addDependency(path.resolve(__dirname, "extra.js")); diff --git a/test/configCases/context-replacement/d/queryloader.js b/test/configCases/context-replacement/d/queryloader.js index 88a2fd607bc..821519145eb 100644 --- a/test/configCases/context-replacement/d/queryloader.js +++ b/test/configCases/context-replacement/d/queryloader.js @@ -1,7 +1,11 @@ -module.exports = function(content) { - return "module.exports = " + JSON.stringify({ - resourceQuery: this.resourceQuery, - query: this.query, - prev: content.replace(/\r\n?/g, "\n") - }); +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (content) { + return ( + "module.exports = " + + JSON.stringify({ + resourceQuery: this.resourceQuery, + query: this.query, + prev: content.replace(/\r\n?/g, "\n") + }) + ); }; diff --git a/test/configCases/deprecations/invalid-dependencies/loader.js b/test/configCases/deprecations/invalid-dependencies/loader.js index 47a39616b26..71212ed09b8 100644 --- a/test/configCases/deprecations/invalid-dependencies/loader.js +++ b/test/configCases/deprecations/invalid-dependencies/loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../").LoaderDefinition} */ module.exports = function (source) { this.addDependency("loader.js"); this.addDependency("../**/dir/*.js"); diff --git a/test/configCases/dll-plugin/0-create-dll/g-loader.js b/test/configCases/dll-plugin/0-create-dll/g-loader.js index 6e64f4af6bb..c6d8a635121 100644 --- a/test/configCases/dll-plugin/0-create-dll/g-loader.js +++ b/test/configCases/dll-plugin/0-create-dll/g-loader.js @@ -1,3 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { return source; }; diff --git a/test/configCases/inner-graph/_helpers/entryLoader.js b/test/configCases/inner-graph/_helpers/entryLoader.js index 992500b06fa..7e129b81030 100644 --- a/test/configCases/inner-graph/_helpers/entryLoader.js +++ b/test/configCases/inner-graph/_helpers/entryLoader.js @@ -1,4 +1,5 @@ -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { const { name, expect, usedExports } = JSON.parse(this.query.slice(1)); return [ `if (Math.random() < 0) require(${JSON.stringify( diff --git a/test/configCases/inner-graph/_helpers/testModuleLoader.js b/test/configCases/inner-graph/_helpers/testModuleLoader.js index 1742c4ad429..b6d54748dda 100644 --- a/test/configCases/inner-graph/_helpers/testModuleLoader.js +++ b/test/configCases/inner-graph/_helpers/testModuleLoader.js @@ -1,4 +1,5 @@ -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { const usedExports = JSON.parse(this.query.slice(1)); return [ `import { ${usedExports diff --git a/test/configCases/layer/rules/loader.js b/test/configCases/layer/rules/loader.js index 34dbc1b1703..7e5acde631c 100644 --- a/test/configCases/layer/rules/loader.js +++ b/test/configCases/layer/rules/loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../").LoaderDefinition<{ value: any }>} */ module.exports = function (source) { const options = this.getOptions(); return `${source} diff --git a/test/configCases/loader-import-module/css/loader.js b/test/configCases/loader-import-module/css/loader.js index d9b5022ab4a..2dae62a4065 100644 --- a/test/configCases/loader-import-module/css/loader.js +++ b/test/configCases/loader-import-module/css/loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../").PitchLoaderDefinitionFunction} */ exports.pitch = async function (remaining) { const result = await this.importModule( this.resourcePath + ".webpack[javascript/auto]" + "!=!" + remaining, diff --git a/test/configCases/loaders/generate-ident/loader1.js b/test/configCases/loaders/generate-ident/loader1.js index 42fea46336a..0d2fcc8b593 100644 --- a/test/configCases/loaders/generate-ident/loader1.js +++ b/test/configCases/loaders/generate-ident/loader1.js @@ -1,3 +1,6 @@ -module.exports.pitch = function(remainingRequest) { - return "module.exports = require(" + JSON.stringify("!!" + remainingRequest) + ");"; +/** @type {import("../../../../").PitchLoaderDefinitionFunction} */ +module.exports.pitch = function (remainingRequest) { + return ( + "module.exports = require(" + JSON.stringify("!!" + remainingRequest) + ");" + ); }; diff --git a/test/configCases/loaders/generate-ident/loader2.js b/test/configCases/loaders/generate-ident/loader2.js index b5b133a9208..40788042801 100644 --- a/test/configCases/loaders/generate-ident/loader2.js +++ b/test/configCases/loaders/generate-ident/loader2.js @@ -1,3 +1,6 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition<{ f(): any }>} */ +module.exports = function (source) { + if (typeof this.query === "string") + throw new Error("query must be an object"); return "module.exports = " + JSON.stringify(this.query.f()); }; diff --git a/test/configCases/loaders/hot-in-context/loader.js b/test/configCases/loaders/hot-in-context/loader.js index b497b8bc45e..608faaddb21 100644 --- a/test/configCases/loaders/hot-in-context/loader.js +++ b/test/configCases/loaders/hot-in-context/loader.js @@ -1,3 +1,4 @@ -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition}} */ +module.exports = function () { return `module.exports = ${JSON.stringify(!!this.hot)};`; -} +}; diff --git a/test/configCases/loaders/mode-default/loader.js b/test/configCases/loaders/mode-default/loader.js index 0083d38fd1b..b9c10626bc4 100644 --- a/test/configCases/loaders/mode-default/loader.js +++ b/test/configCases/loaders/mode-default/loader.js @@ -1,3 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { return `module.exports = "${this.mode}";`; }; diff --git a/test/configCases/loaders/mode-development/loader.js b/test/configCases/loaders/mode-development/loader.js index 0083d38fd1b..b9c10626bc4 100644 --- a/test/configCases/loaders/mode-development/loader.js +++ b/test/configCases/loaders/mode-development/loader.js @@ -1,3 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { return `module.exports = "${this.mode}";`; }; diff --git a/test/configCases/loaders/mode-none/loader.js b/test/configCases/loaders/mode-none/loader.js index 0083d38fd1b..b9c10626bc4 100644 --- a/test/configCases/loaders/mode-none/loader.js +++ b/test/configCases/loaders/mode-none/loader.js @@ -1,3 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { return `module.exports = "${this.mode}";`; }; diff --git a/test/configCases/loaders/mode-production/loader.js b/test/configCases/loaders/mode-production/loader.js index 0083d38fd1b..b9c10626bc4 100644 --- a/test/configCases/loaders/mode-production/loader.js +++ b/test/configCases/loaders/mode-production/loader.js @@ -1,3 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { return `module.exports = "${this.mode}";`; }; diff --git a/test/configCases/loaders/options/loader-1.js b/test/configCases/loaders/options/loader-1.js index f27763418ab..18e183cbbb0 100644 --- a/test/configCases/loaders/options/loader-1.js +++ b/test/configCases/loaders/options/loader-1.js @@ -1,6 +1,7 @@ -const schema = require("./loader-1.options"); +const schema = require("./loader-1.options.json"); -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { const options = this.getOptions(schema); const json = JSON.stringify(options) diff --git a/test/configCases/loaders/options/loader-2.js b/test/configCases/loaders/options/loader-2.js index b1690265227..faea214da83 100644 --- a/test/configCases/loaders/options/loader-2.js +++ b/test/configCases/loaders/options/loader-2.js @@ -1,6 +1,7 @@ -const schema = require("./loader-2.options"); +const schema = require("./loader-2.options.json"); -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { const options = this.getOptions(schema); const json = JSON.stringify(options) diff --git a/test/configCases/loaders/options/loader.js b/test/configCases/loaders/options/loader.js index 5b9386651be..d1bc02fcd3c 100644 --- a/test/configCases/loaders/options/loader.js +++ b/test/configCases/loaders/options/loader.js @@ -1,9 +1,10 @@ -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { const options = this.getOptions(); const json = JSON.stringify(options) - .replace(/\u2028/g, '\\u2028') - .replace(/\u2029/g, '\\u2029'); + .replace(/\u2028/g, "\\u2028") + .replace(/\u2029/g, "\\u2029"); return `module.exports = ${json}`; }; diff --git a/test/configCases/loaders/pre-post-loader/loader1.js b/test/configCases/loaders/pre-post-loader/loader1.js index 71df71135dd..bf6d1335221 100644 --- a/test/configCases/loaders/pre-post-loader/loader1.js +++ b/test/configCases/loaders/pre-post-loader/loader1.js @@ -1,3 +1,4 @@ -module.exports = function(source) { - return source + "module.exports += \" loader1\";\n"; +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { + return source + 'module.exports += " loader1";\n'; }; diff --git a/test/configCases/loaders/pre-post-loader/loader2.js b/test/configCases/loaders/pre-post-loader/loader2.js index 91497b0978b..b611c84f62b 100644 --- a/test/configCases/loaders/pre-post-loader/loader2.js +++ b/test/configCases/loaders/pre-post-loader/loader2.js @@ -1,3 +1,4 @@ -module.exports = function(source) { - return source + "module.exports += \" loader2\";\n"; +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { + return source + 'module.exports += " loader2";\n'; }; diff --git a/test/configCases/loaders/pre-post-loader/loader3.js b/test/configCases/loaders/pre-post-loader/loader3.js index 32f164287a0..ec526cbac53 100644 --- a/test/configCases/loaders/pre-post-loader/loader3.js +++ b/test/configCases/loaders/pre-post-loader/loader3.js @@ -1,3 +1,4 @@ -module.exports = function(source) { - return source + "module.exports += \" loader3\";\n"; +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { + return source + 'module.exports += " loader3";\n'; }; diff --git a/test/configCases/loaders/remaining-request/loader1.js b/test/configCases/loaders/remaining-request/loader1.js index 42fea46336a..0d2fcc8b593 100644 --- a/test/configCases/loaders/remaining-request/loader1.js +++ b/test/configCases/loaders/remaining-request/loader1.js @@ -1,3 +1,6 @@ -module.exports.pitch = function(remainingRequest) { - return "module.exports = require(" + JSON.stringify("!!" + remainingRequest) + ");"; +/** @type {import("../../../../").PitchLoaderDefinitionFunction} */ +module.exports.pitch = function (remainingRequest) { + return ( + "module.exports = require(" + JSON.stringify("!!" + remainingRequest) + ");" + ); }; diff --git a/test/configCases/loaders/remaining-request/loader2.js b/test/configCases/loaders/remaining-request/loader2.js index b5b133a9208..40788042801 100644 --- a/test/configCases/loaders/remaining-request/loader2.js +++ b/test/configCases/loaders/remaining-request/loader2.js @@ -1,3 +1,6 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition<{ f(): any }>} */ +module.exports = function (source) { + if (typeof this.query === "string") + throw new Error("query must be an object"); return "module.exports = " + JSON.stringify(this.query.f()); }; diff --git a/test/configCases/module-name/different-issuers-for-same-module/loader-a.js b/test/configCases/module-name/different-issuers-for-same-module/loader-a.js index bd8581ca4a4..4e8352ee90e 100644 --- a/test/configCases/module-name/different-issuers-for-same-module/loader-a.js +++ b/test/configCases/module-name/different-issuers-for-same-module/loader-a.js @@ -1,3 +1,4 @@ -module.exports = function(src) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (src) { return `module.exports = "loader-a" + module.id`; }; diff --git a/test/configCases/module-name/different-issuers-for-same-module/loader-b.js b/test/configCases/module-name/different-issuers-for-same-module/loader-b.js index 5365e2fd355..7fa193f020f 100644 --- a/test/configCases/module-name/different-issuers-for-same-module/loader-b.js +++ b/test/configCases/module-name/different-issuers-for-same-module/loader-b.js @@ -1,3 +1,4 @@ -module.exports = function(src) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (src) { return `module.exports = "loader-b" + module.id`; }; diff --git a/test/configCases/performance/many-async-imports/reexport.loader.js b/test/configCases/performance/many-async-imports/reexport.loader.js index f44ceced67a..3105e517fef 100644 --- a/test/configCases/performance/many-async-imports/reexport.loader.js +++ b/test/configCases/performance/many-async-imports/reexport.loader.js @@ -1,7 +1,8 @@ -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { var str = "export default Promise.all([\n"; - for(var i = 0; i < 6; i++) { - for(var j = 0; j < 2; j++) { + for (var i = 0; i < 6; i++) { + for (var j = 0; j < 2; j++) { str += `import("./reexport.loader.js!?${i}"),\n`; } } diff --git a/test/configCases/performance/many-exports/file.loader.js b/test/configCases/performance/many-exports/file.loader.js index 6ec2268c91d..1dd13c65f5c 100644 --- a/test/configCases/performance/many-exports/file.loader.js +++ b/test/configCases/performance/many-exports/file.loader.js @@ -1,7 +1,8 @@ -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { var str = ""; - for(var i = 0; i < 1000; i++) { + for (var i = 0; i < 1000; i++) { str += `export var a${i} = ${i};\n`; } return str; -} +}; diff --git a/test/configCases/performance/many-exports/reexport.loader.js b/test/configCases/performance/many-exports/reexport.loader.js index af755e8686a..e4a2a31352a 100644 --- a/test/configCases/performance/many-exports/reexport.loader.js +++ b/test/configCases/performance/many-exports/reexport.loader.js @@ -1,9 +1,10 @@ -module.exports = function() { - var str = "import * as i from \"./file.loader.js!\";\n"; +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { + var str = 'import * as i from "./file.loader.js!";\n'; str += "var sum = 0;\n"; - for(var i = 0; i < 1000; i++) { + for (var i = 0; i < 1000; i++) { str += `sum += i.a${i};\n`; } str += "export default sum;\n"; return str; -} +}; diff --git a/test/configCases/plugins/loader-options-plugin/loader.js b/test/configCases/plugins/loader-options-plugin/loader.js index 7374ef2b557..8fcf0774460 100644 --- a/test/configCases/plugins/loader-options-plugin/loader.js +++ b/test/configCases/plugins/loader-options-plugin/loader.js @@ -1,6 +1,10 @@ -module.exports = function() { - return "module.exports = " + JSON.stringify({ - minimize: this.minimize, - jsfile: this.jsfile - }); +/** @type {import("../../../../").LoaderDefinition<{}, { minimize: boolean, jsfile: boolean }>} */ +module.exports = function () { + return ( + "module.exports = " + + JSON.stringify({ + minimize: this.minimize, + jsfile: this.jsfile + }) + ); }; diff --git a/test/configCases/race-conditions/load-module/loader.js b/test/configCases/race-conditions/load-module/loader.js index 444c2c9dad3..b741c194e99 100644 --- a/test/configCases/race-conditions/load-module/loader.js +++ b/test/configCases/race-conditions/load-module/loader.js @@ -1,4 +1,5 @@ -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { const callback = this.async(); let finished = false; this.loadModule("./module.js", (err, result) => { diff --git a/test/configCases/rebuild/finishModules/loader.js b/test/configCases/rebuild/finishModules/loader.js index 80d125d3902..347e3b5be32 100644 --- a/test/configCases/rebuild/finishModules/loader.js +++ b/test/configCases/rebuild/finishModules/loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../").LoaderDefinition<{}, { shouldReplace: boolean }>} */ module.exports = function (source) { if (this.shouldReplace) { this._module.buildInfo._isReplaced = true; diff --git a/test/configCases/rebuild/rebuildWithNewDependencies/loader.js b/test/configCases/rebuild/rebuildWithNewDependencies/loader.js index fc39654810e..f33697b4f1a 100644 --- a/test/configCases/rebuild/rebuildWithNewDependencies/loader.js +++ b/test/configCases/rebuild/rebuildWithNewDependencies/loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../").LoaderDefinition<{}, { shouldReplace: boolean }>} */ module.exports = function (source) { if (this.shouldReplace) { this._module.buildInfo._isReplaced = true; diff --git a/test/configCases/records/issue-295/loader.js b/test/configCases/records/issue-295/loader.js index 6e64f4af6bb..c6d8a635121 100644 --- a/test/configCases/records/issue-295/loader.js +++ b/test/configCases/records/issue-295/loader.js @@ -1,3 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { return source; }; diff --git a/test/configCases/resolve-merging/override/loader.js b/test/configCases/resolve-merging/override/loader.js index 7e124f2381d..961891c5e19 100644 --- a/test/configCases/resolve-merging/override/loader.js +++ b/test/configCases/resolve-merging/override/loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../").LoaderDefinition} */ module.exports = async function () { const defaultResolve = this.getResolve({}); const overrideResolve = this.getResolve({ @@ -20,6 +21,7 @@ module.exports = async function () { expect(await defaultResolve(undefined, "package2").catch(e => "ok")).toBe( "ok" ); + // @ts-expect-error undefined should not be a valid type expect(await defaultResolve(undefined).catch(e => "ok")).toBe("ok"); return ` export { default as a } from ${JSON.stringify(resolved1)}; diff --git a/test/configCases/rule-set/chaining/loader.js b/test/configCases/rule-set/chaining/loader.js index f78d43c0b78..84836e5dae5 100644 --- a/test/configCases/rule-set/chaining/loader.js +++ b/test/configCases/rule-set/chaining/loader.js @@ -1,8 +1,8 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */ +module.exports = function (source) { var query = this.query; - if(typeof query === "object" && typeof query.get === "function") { + if (typeof query === "object" && typeof query.get === "function") { query = query.get(); } return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");"; }; - diff --git a/test/configCases/rule-set/compiler/loader.js b/test/configCases/rule-set/compiler/loader.js index 196d1ef49db..b57f18e5dae 100644 --- a/test/configCases/rule-set/compiler/loader.js +++ b/test/configCases/rule-set/compiler/loader.js @@ -1,4 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { return "module.exports = " + JSON.stringify("loader matched"); }; - diff --git a/test/configCases/rule-set/custom/loader.js b/test/configCases/rule-set/custom/loader.js index f78d43c0b78..84836e5dae5 100644 --- a/test/configCases/rule-set/custom/loader.js +++ b/test/configCases/rule-set/custom/loader.js @@ -1,8 +1,8 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */ +module.exports = function (source) { var query = this.query; - if(typeof query === "object" && typeof query.get === "function") { + if (typeof query === "object" && typeof query.get === "function") { query = query.get(); } return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");"; }; - diff --git a/test/configCases/rule-set/query/loader.js b/test/configCases/rule-set/query/loader.js index f78d43c0b78..84836e5dae5 100644 --- a/test/configCases/rule-set/query/loader.js +++ b/test/configCases/rule-set/query/loader.js @@ -1,8 +1,8 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */ +module.exports = function (source) { var query = this.query; - if(typeof query === "object" && typeof query.get === "function") { + if (typeof query === "object" && typeof query.get === "function") { query = query.get(); } return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");"; }; - diff --git a/test/configCases/rule-set/simple-use-array-fn/loader.js b/test/configCases/rule-set/simple-use-array-fn/loader.js index f78d43c0b78..84836e5dae5 100644 --- a/test/configCases/rule-set/simple-use-array-fn/loader.js +++ b/test/configCases/rule-set/simple-use-array-fn/loader.js @@ -1,8 +1,8 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */ +module.exports = function (source) { var query = this.query; - if(typeof query === "object" && typeof query.get === "function") { + if (typeof query === "object" && typeof query.get === "function") { query = query.get(); } return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");"; }; - diff --git a/test/configCases/rule-set/simple-use-fn-array/loader.js b/test/configCases/rule-set/simple-use-fn-array/loader.js index f78d43c0b78..84836e5dae5 100644 --- a/test/configCases/rule-set/simple-use-fn-array/loader.js +++ b/test/configCases/rule-set/simple-use-fn-array/loader.js @@ -1,8 +1,8 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */ +module.exports = function (source) { var query = this.query; - if(typeof query === "object" && typeof query.get === "function") { + if (typeof query === "object" && typeof query.get === "function") { query = query.get(); } return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");"; }; - diff --git a/test/configCases/rule-set/simple/loader.js b/test/configCases/rule-set/simple/loader.js index f78d43c0b78..84836e5dae5 100644 --- a/test/configCases/rule-set/simple/loader.js +++ b/test/configCases/rule-set/simple/loader.js @@ -1,8 +1,8 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */ +module.exports = function (source) { var query = this.query; - if(typeof query === "object" && typeof query.get === "function") { + if (typeof query === "object" && typeof query.get === "function") { query = query.get(); } return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");"; }; - diff --git a/test/configCases/source-map/no-source-map/loader.js b/test/configCases/source-map/no-source-map/loader.js index 5cac8966d3a..84613ab8e99 100644 --- a/test/configCases/source-map/no-source-map/loader.js +++ b/test/configCases/source-map/no-source-map/loader.js @@ -1,4 +1,5 @@ const path = require("path"); +/** @type {import("../../../../").LoaderDefinition} */ module.exports = function () { this.callback(null, "module.exports = 'ok';", { version: 3, @@ -6,6 +7,7 @@ module.exports = function () { sourceRoot: path.join(__dirname, "folder"), sources: ["test1.txt"], sourcesContent: ["Test"], + names: [], mappings: "AAAA" }); }; diff --git a/test/configCases/source-map/relative-source-maps-by-loader/loader-no-source-root.js b/test/configCases/source-map/relative-source-maps-by-loader/loader-no-source-root.js index e30048a3e41..f2ca2e44e87 100644 --- a/test/configCases/source-map/relative-source-maps-by-loader/loader-no-source-root.js +++ b/test/configCases/source-map/relative-source-maps-by-loader/loader-no-source-root.js @@ -1,10 +1,12 @@ const path = require("path"); -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { this.callback(null, "module.exports = 'ok';", { version: 3, file: "/should/be/removed", sources: [path.join(__dirname, "folder", "test5.txt")], sourcesContent: ["Test"], + names: [], mappings: "AAAA" }); }; diff --git a/test/configCases/source-map/relative-source-maps-by-loader/loader-pre-relative.js b/test/configCases/source-map/relative-source-maps-by-loader/loader-pre-relative.js index 6fe9431cedb..e70ef8ec6ca 100644 --- a/test/configCases/source-map/relative-source-maps-by-loader/loader-pre-relative.js +++ b/test/configCases/source-map/relative-source-maps-by-loader/loader-pre-relative.js @@ -1,9 +1,11 @@ -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { this.callback(null, "module.exports = 'ok';", { version: 3, file: "/should/be/removed", sources: ["webpack://./folder/test6.txt"], sourcesContent: ["Test"], + names: [], mappings: "AAAA" }); }; diff --git a/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-2-slash.js b/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-2-slash.js index e5c552d40f9..eb49d6a3ce5 100644 --- a/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-2-slash.js +++ b/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-2-slash.js @@ -1,11 +1,13 @@ const path = require("path"); -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { this.callback(null, "module.exports = 'ok';", { version: 3, file: "/should/be/removed", sourceRoot: path.join(__dirname, "folder") + "/", sources: ["/test4.txt"], sourcesContent: ["Test"], + names: [], mappings: "AAAA" }); }; diff --git a/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-slash.js b/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-slash.js index 0641ad6d563..2fb7f62e1f5 100644 --- a/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-slash.js +++ b/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-slash.js @@ -1,11 +1,13 @@ const path = require("path"); -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { this.callback(null, "module.exports = 'ok';", { version: 3, file: "/should/be/removed", sourceRoot: path.join(__dirname, "folder") + "/", sources: ["test3.txt"], sourcesContent: ["Test"], + names: [], mappings: "AAAA" }); }; diff --git a/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-source-slash.js b/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-source-slash.js index dbb0e20bb53..5e25c38b2b3 100644 --- a/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-source-slash.js +++ b/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root-source-slash.js @@ -1,11 +1,13 @@ const path = require("path"); -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { this.callback(null, "module.exports = 'ok';", { version: 3, file: "/should/be/removed", sourceRoot: path.join(__dirname, "folder"), sources: ["/test2.txt"], sourcesContent: ["Test"], + names: [], mappings: "AAAA" }); }; diff --git a/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root.js b/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root.js index a46ea3ff546..84613ab8e99 100644 --- a/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root.js +++ b/test/configCases/source-map/relative-source-maps-by-loader/loader-source-root.js @@ -1,11 +1,13 @@ const path = require("path"); -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { this.callback(null, "module.exports = 'ok';", { version: 3, file: "/should/be/removed", sourceRoot: path.join(__dirname, "folder"), sources: ["test1.txt"], sourcesContent: ["Test"], + names: [], mappings: "AAAA" }); }; diff --git a/test/fixtures/count-loader.js b/test/fixtures/count-loader.js index ced2018eef9..9da5ce227be 100644 --- a/test/fixtures/count-loader.js +++ b/test/fixtures/count-loader.js @@ -1,5 +1,6 @@ let counter = 0; -module.exports = function() { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () { return `module.exports = ${counter++};`; }; diff --git a/test/fixtures/delay-loader.js b/test/fixtures/delay-loader.js index 3c6573548fe..01e71f3fb6d 100644 --- a/test/fixtures/delay-loader.js +++ b/test/fixtures/delay-loader.js @@ -1,6 +1,7 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { var cb = this.async(); - setTimeout(function() { + setTimeout(function () { cb(null, source); }, 500); -}; \ No newline at end of file +}; diff --git a/test/fixtures/errors/add-comment-loader.js b/test/fixtures/errors/add-comment-loader.js index 1cfa533d6a7..586bfbce576 100644 --- a/test/fixtures/errors/add-comment-loader.js +++ b/test/fixtures/errors/add-comment-loader.js @@ -1,3 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { return source + "// some comment"; }; diff --git a/test/fixtures/errors/async-error-loader.js b/test/fixtures/errors/async-error-loader.js index a0eba8a9d54..9826e36c3b7 100644 --- a/test/fixtures/errors/async-error-loader.js +++ b/test/fixtures/errors/async-error-loader.js @@ -1,4 +1,5 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { const callback = this.async(); const error = new Error("this is a callback error"); callback(error, source); diff --git a/test/fixtures/errors/emit-error-loader.js b/test/fixtures/errors/emit-error-loader.js index 57164e2206c..1cd648e665d 100644 --- a/test/fixtures/errors/emit-error-loader.js +++ b/test/fixtures/errors/emit-error-loader.js @@ -1,4 +1,5 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { this.emitWarning(new Error("this is a warning")); this.emitError(new Error("this is an error")); return source; diff --git a/test/fixtures/errors/identity-loader.js b/test/fixtures/errors/identity-loader.js index 6e64f4af6bb..c6d8a635121 100644 --- a/test/fixtures/errors/identity-loader.js +++ b/test/fixtures/errors/identity-loader.js @@ -1,3 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { return source; }; diff --git a/test/fixtures/errors/irregular-error-loader.js b/test/fixtures/errors/irregular-error-loader.js index 8b38ff7eff0..6ed0ba935ef 100644 --- a/test/fixtures/errors/irregular-error-loader.js +++ b/test/fixtures/errors/irregular-error-loader.js @@ -1,4 +1,5 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { const empty = null; const emptyError = new Error(); this.emitWarning(empty); diff --git a/test/fixtures/errors/no-return-loader.js b/test/fixtures/errors/no-return-loader.js index 0a4b3bfaa71..63c5d351ef1 100644 --- a/test/fixtures/errors/no-return-loader.js +++ b/test/fixtures/errors/no-return-loader.js @@ -1,2 +1,2 @@ -module.exports = function(){ -} +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function () {}; diff --git a/test/fixtures/errors/throw-error-loader.js b/test/fixtures/errors/throw-error-loader.js index 3142eedc09d..59014e2a1b3 100644 --- a/test/fixtures/errors/throw-error-loader.js +++ b/test/fixtures/errors/throw-error-loader.js @@ -1,3 +1,4 @@ -module.exports = function(source) { +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { throw new Error("this is a thrown error"); }; diff --git a/test/hotCases/child-compiler/issue-9706/report-child-assets-loader.js b/test/hotCases/child-compiler/issue-9706/report-child-assets-loader.js index df27ff875b6..95bbe37ed0d 100644 --- a/test/hotCases/child-compiler/issue-9706/report-child-assets-loader.js +++ b/test/hotCases/child-compiler/issue-9706/report-child-assets-loader.js @@ -5,6 +5,7 @@ const { const compilerCache = new WeakMap(); +/** @type {import("../../../../").LoaderDefinition} */ module.exports = function (source) { let childCompiler = compilerCache.get(this._compiler); if (childCompiler === undefined) { diff --git a/test/hotCases/fake-update-loader.js b/test/hotCases/fake-update-loader.js index e2b1884bd74..705d8feedad 100644 --- a/test/hotCases/fake-update-loader.js +++ b/test/hotCases/fake-update-loader.js @@ -1,4 +1,5 @@ -module.exports = function(source) { +/** @type {import("../../").LoaderDefinition<{}, { updateIndex: number }>} */ +module.exports = function (source) { var idx = this.updateIndex; var items = source.split(/---+\r?\n/g); if (items.length > 1) { diff --git a/test/hotCases/loader-import-module/css/loader.js b/test/hotCases/loader-import-module/css/loader.js index 927bbc669c6..c4bf30e37f1 100644 --- a/test/hotCases/loader-import-module/css/loader.js +++ b/test/hotCases/loader-import-module/css/loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../").PitchLoaderDefinitionFunction} */ exports.pitch = async function (remaining) { const result = await this.importModule( this.resourcePath + ".webpack[javascript/auto]" + "!=!" + remaining diff --git a/test/hotCases/recover/recover-after-loader-error/loader.js b/test/hotCases/recover/recover-after-loader-error/loader.js index c6713cb9fcb..4f935bbc6ad 100644 --- a/test/hotCases/recover/recover-after-loader-error/loader.js +++ b/test/hotCases/recover/recover-after-loader-error/loader.js @@ -1,5 +1,5 @@ -module.exports = function(source) { - if(source.indexOf("error") >= 0) - throw new Error(source.trim()); +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { + if (source.indexOf("error") >= 0) throw new Error(source.trim()); return source; }; diff --git a/test/watchCases/cache/child-compilation-cache/0/report-cache-counters-loader.js b/test/watchCases/cache/child-compilation-cache/0/report-cache-counters-loader.js index 6fc034fe3a0..adedcfa8531 100644 --- a/test/watchCases/cache/child-compilation-cache/0/report-cache-counters-loader.js +++ b/test/watchCases/cache/child-compilation-cache/0/report-cache-counters-loader.js @@ -4,30 +4,38 @@ var cacheMap = new WeakMap(); const getCache = (associate, path) => { let o = cacheMap.get(associate); - if(o === undefined) { + if (o === undefined) { o = new Map(); cacheMap.set(associate, o); } let c = o.get(path); - if(c === undefined) { + if (c === undefined) { c = { counter: 0 }; o.set(path, c); } return c; }; -module.exports = function(source) { - if(map.has(currentWatchStepModule.step)) return map.get(currentWatchStepModule.step); +/** @type {import("../../../../../").LoaderDefinition} */ +module.exports = function (source) { + if (map.has(currentWatchStepModule.step)) + return map.get(currentWatchStepModule.step); - const compilationCache = getCache(this._compiler.root, this._compilation.compilerPath); + const compilationCache = getCache( + this._compiler.root, + this._compilation.compilerPath + ); compilationCache.counter++; - var childCompiler = this._compilation.createChildCompiler("my-compiler " + source.trim(), { - filename: "test" - }); + var childCompiler = this._compilation.createChildCompiler( + "my-compiler " + source.trim(), + { + filename: "test" + } + ); var callback = this.async(); childCompiler.runAsChild((err, entries, compilation) => { - if(err) return callback(err); + if (err) return callback(err); const childCache = getCache(this._compiler.root, compilation.compilerPath); childCache.counter++; diff --git a/test/watchCases/cache/loader-import-module/0/loader.js b/test/watchCases/cache/loader-import-module/0/loader.js index 095a8850439..fde06f26f49 100644 --- a/test/watchCases/cache/loader-import-module/0/loader.js +++ b/test/watchCases/cache/loader-import-module/0/loader.js @@ -1,3 +1,4 @@ +/** @type {import("../../../../../").PitchLoaderDefinitionFunction} */ exports.pitch = async function (remaining) { const result = await this.importModule( `${this.resourcePath}.webpack[javascript/auto]!=!${remaining}` diff --git a/test/watchCases/context/loader-context-dep/0/loader.js b/test/watchCases/context/loader-context-dep/0/loader.js index e81465f2a2a..ed1f2044e2f 100644 --- a/test/watchCases/context/loader-context-dep/0/loader.js +++ b/test/watchCases/context/loader-context-dep/0/loader.js @@ -1,7 +1,8 @@ const path = require("path"); const directory = path.resolve(__dirname, "directory"); -module.exports = function() { +/** @type {import("../../../../../").LoaderDefinition} */ +module.exports = function () { this.addContextDependency(directory); const callback = this.async(); this.fs.readdir(directory, (err, files) => { diff --git a/test/watchCases/resolve/in-loader/0/loader.js b/test/watchCases/resolve/in-loader/0/loader.js index 51c0656c735..d43844f852c 100644 --- a/test/watchCases/resolve/in-loader/0/loader.js +++ b/test/watchCases/resolve/in-loader/0/loader.js @@ -1,7 +1,9 @@ -module.exports = function() { +/** @type {import("../../../../../").LoaderDefinition} */ +module.exports = function () { const callback = this.async(); this.resolve(this.context, "./file", (err, file) => { if (err) return callback(err); + if (!file) return callback(new Error("Resolving failed")); this.fs.readFile(file, (err, result) => { if (err) return callback(err); callback( diff --git a/test/watchCases/warnings/warnings-contribute-to-hash/0/warning-loader.js b/test/watchCases/warnings/warnings-contribute-to-hash/0/warning-loader.js index a5206b8bdac..dbf6abe1827 100644 --- a/test/watchCases/warnings/warnings-contribute-to-hash/0/warning-loader.js +++ b/test/watchCases/warnings/warnings-contribute-to-hash/0/warning-loader.js @@ -1,4 +1,5 @@ -module.exports = function(source) { +/** @type {import("../../../../../").LoaderDefinition} */ +module.exports = function (source) { this.emitWarning(new Error(source.trim())); return ""; }; diff --git a/tsconfig.test.json b/tsconfig.test.json index 23a3c6e6f7b..e6e76890abe 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -12,5 +12,12 @@ "types": ["node", "jest"], "esModuleInterop": true }, - "include": ["test/**/webpack.config.js", "declarations.test.d.ts"] + "include": [ + "test/**/webpack.config.js", + "test/cases/**/*loader*.js", + "test/watchCases/**/*loader*.js", + "test/configCases/**/*loader*.js", + "test/hotCases/**/*loader*.js", + "declarations.test.d.ts" + ] } From 5b21dd2f898f920b7324ae49828f6f7175cdc812 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 22 Apr 2021 21:53:28 +0200 Subject: [PATCH 26/29] fix import problem --- declarations/LoaderContext.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 3132cac4729..db84e7aeb53 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -4,7 +4,8 @@ import type { AssetInfo } from "../lib/Compilation"; import type { ResolveOptionsWithDependencyType } from "../lib/ResolverFactory"; import type Compilation from "../lib/Compilation"; import type Compiler from "../lib/Compiler"; -import type NormalModule, { InputFileSystem } from "../lib/NormalModule"; +import type NormalModule from "../lib/NormalModule"; +import type { InputFileSystem } from "../lib/util/fs"; import type { Logger } from "../lib/logging/Logger"; import type { ImportModuleCallback, From 77f625ab0993b52be8891bfa303aceaa855fd9cb Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 7 May 2021 17:50:08 +0200 Subject: [PATCH 27/29] upgrade tooling and update types.d.ts fixed SourceMap import Schema from validate function --- declarations/LoaderContext.d.ts | 6 +++-- lib/NormalModule.js | 12 ++++++++-- package.json | 2 +- types.d.ts | 41 +++++++++++++++------------------ yarn.lock | 6 ++--- 5 files changed, 36 insertions(+), 31 deletions(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index db84e7aeb53..d2076288a07 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -1,5 +1,5 @@ import type { SourceMap } from "../lib/NormalModule"; -import type { Schema } from "schema-utils/declarations/ValidationError"; +import type { validate } from "schema-utils"; import type { AssetInfo } from "../lib/Compilation"; import type { ResolveOptionsWithDependencyType } from "../lib/ResolverFactory"; import type Compilation from "../lib/Compilation"; @@ -14,11 +14,13 @@ import type { import type { Resolver } from "enhanced-resolve"; type ResolveCallback = Parameters[4]; +type Schema = Parameters[0]; /** These properties are added by the NormalModule */ export interface NormalModuleLoaderContext { version: number; - getOptions(schema?: Schema): OptionsType; + getOptions(): any; + getOptions(schema: Schema): OptionsType; emitWarning(warning: Error): void; emitError(error: Error): void; getLogger(name?: string): Logger; diff --git a/lib/NormalModule.js b/lib/NormalModule.js index dfe715306e7..29320e89a08 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -41,7 +41,6 @@ const { contextify, absolutify } = require("./util/identifier"); const makeSerializable = require("./util/makeSerializable"); const memoize = require("./util/memoize"); -/** @typedef {import("source-map").RawSourceMap} RawSourceMap */ /** @typedef {import("webpack-sources").Source} Source */ /** @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext} NormalModuleLoaderContext */ /** @typedef {import("../declarations/WebpackOptions").Mode} Mode */ @@ -68,7 +67,16 @@ const memoize = require("./util/memoize"); /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */ -/** @typedef {Omit & { version: number }} SourceMap */ +/** + * @typedef {Object} SourceMap + * @property {number} version + * @property {string[]} sources + * @property {string} mappings + * @property {string=} file + * @property {string=} sourceRoot + * @property {string[]=} sourcesContent + * @property {string[]=} names + */ const getInvalidDependenciesModuleWarning = memoize(() => require("./InvalidDependenciesModuleWarning") diff --git a/package.json b/package.json index 17673309bd1..b55796c3203 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "style-loader": "^2.0.0", "terser": "^5.5.0", "toml": "^3.0.0", - "tooling": "webpack/tooling#v1.18.0", + "tooling": "webpack/tooling#v1.19.0", "ts-loader": "^8.0.2", "typescript": "^4.2.0-beta", "url-loader": "^4.1.0", diff --git a/types.d.ts b/types.d.ts index 5c9d90de3ae..d304bcae708 100644 --- a/types.d.ts +++ b/types.d.ts @@ -78,13 +78,8 @@ import { WithStatement, YieldExpression } from "estree"; -import { JSONSchema4, JSONSchema6, JSONSchema7 } from "json-schema"; -import { default as ValidationError } from "schema-utils/declarations/ValidationError"; -import { - Extend, - ValidationErrorConfiguration -} from "schema-utils/declarations/validate"; -import { RawSourceMap } from "source-map/source-map"; +import { ValidationError, validate } from "schema-utils"; +import { ValidationErrorConfiguration } from "schema-utils/declarations/validate"; import { AsArray, AsyncParallelHook, @@ -4188,7 +4183,7 @@ declare interface HashedModuleIdsPluginOptions { /** * The encoding to use when generating the hash, defaults to 'base64'. All encodings from Node.JS' hash.digest are supported. */ - hashDigest?: "hex" | "latin1" | "base64"; + hashDigest?: "base64" | "latin1" | "hex"; /** * The prefix length of the hash digest to use, defaults to 4. @@ -7162,12 +7157,8 @@ declare abstract class NormalModuleFactory extends ModuleFactory { */ declare interface NormalModuleLoaderContext { version: number; - getOptions( - schema?: - | (JSONSchema4 & Extend) - | (JSONSchema6 & Extend) - | (JSONSchema7 & Extend) - ): OptionsType; + getOptions(): OptionsType; + getOptions(schema: Parameters[0]): OptionsType; emitWarning(warning: Error): void; emitError(error: Error): void; getLogger(name?: string): WebpackLogger; @@ -10171,10 +10162,6 @@ declare interface RuntimeValueOptions { buildDependencies?: string[]; version?: string | (() => string); } -type Schema = - | (JSONSchema4 & Extend) - | (JSONSchema6 & Extend) - | (JSONSchema7 & Extend); declare interface ScopeInfo { definitions: StackedMap; topLevelScope: boolean | "arrow"; @@ -10450,7 +10437,15 @@ declare interface SourceData { declare interface SourceLike { source(): string | Buffer; } -type SourceMap = Omit & { version: number }; +declare interface SourceMap { + version: number; + sources: string[]; + mappings: string; + file?: string; + sourceRoot?: string; + sourcesContent?: string[]; + names?: string[]; +} declare class SourceMapDevToolPlugin { constructor(options?: SourceMapDevToolPluginOptions); sourceMapFilename: string | false; @@ -11840,8 +11835,8 @@ declare namespace exports { }; export const validate: (options?: any) => void; export const validateSchema: ( - schema: Schema, - options: object | object[], + schema: Parameters[0], + options: Parameters[1], validationConfiguration?: ValidationErrorConfiguration ) => void; export const version: string; @@ -11967,8 +11962,6 @@ declare namespace exports { Unknown: 3; Used: 4; }>; - export const WebpackOptionsValidationError: ValidationError; - export const ValidationError: ValidationError; export namespace cache { export { MemoryCachePlugin }; } @@ -12227,6 +12220,8 @@ declare namespace exports { WebpackError, WebpackOptionsApply, WebpackOptionsDefaulter, + ValidationError as WebpackOptionsValidationError, + ValidationError, Entry, EntryNormalized, EntryObject, diff --git a/yarn.lock b/yarn.lock index 285b564701e..34c2820a82e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6263,9 +6263,9 @@ toml@^3.0.0: resolved "https://registry.yarnpkg.com/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee" integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w== -tooling@webpack/tooling#v1.18.0: - version "1.18.0" - resolved "https://codeload.github.com/webpack/tooling/tar.gz/27496b1099c136e4a8bd69b6d4991c3a493d4a4c" +tooling@webpack/tooling#v1.19.0: + version "1.19.0" + resolved "https://codeload.github.com/webpack/tooling/tar.gz/6b7567edcd6d93f5e5dc1df8364e0b1204edcac3" dependencies: "@yarnpkg/lockfile" "^1.1.0" ajv "^8.1.0" From b6693feeffac73997684fba3d3eefef23e96c507 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 7 May 2021 18:10:57 +0200 Subject: [PATCH 28/29] getOptions returns OptionsType --- declarations/LoaderContext.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index d2076288a07..43b215e023e 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -19,7 +19,7 @@ type Schema = Parameters[0]; /** These properties are added by the NormalModule */ export interface NormalModuleLoaderContext { version: number; - getOptions(): any; + getOptions(): OptionsType; getOptions(schema: Schema): OptionsType; emitWarning(warning: Error): void; emitError(error: Error): void; From 442a1ebca5c3025f78785d561a85aab227ecc957 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 7 May 2021 18:14:25 +0200 Subject: [PATCH 29/29] rename validate function to avoid conflict --- generate-types-config.js | 1 + types.d.ts | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/generate-types-config.js b/generate-types-config.js index b8890a8ea85..b1a47a8285e 100644 --- a/generate-types-config.js +++ b/generate-types-config.js @@ -1,6 +1,7 @@ module.exports = { nameMapping: { FsStats: /^Stats Import fs/, + validateFunction: /^validate Import/, Configuration: /^WebpackOptions / }, exclude: [/^devServer in WebpackOptions /], diff --git a/types.d.ts b/types.d.ts index 38cbc661530..6b96379610e 100644 --- a/types.d.ts +++ b/types.d.ts @@ -79,7 +79,7 @@ import { WithStatement, YieldExpression } from "estree"; -import { ValidationError, validate } from "schema-utils"; +import { ValidationError, validate as validateFunction } from "schema-utils"; import { ValidationErrorConfiguration } from "schema-utils/declarations/validate"; import { AsArray, @@ -7181,7 +7181,7 @@ declare abstract class NormalModuleFactory extends ModuleFactory { declare interface NormalModuleLoaderContext { version: number; getOptions(): OptionsType; - getOptions(schema: Parameters[0]): OptionsType; + getOptions(schema: Parameters[0]): OptionsType; emitWarning(warning: Error): void; emitError(error: Error): void; getLogger(name?: string): WebpackLogger; @@ -11929,8 +11929,8 @@ declare namespace exports { }; export const validate: (options?: any) => void; export const validateSchema: ( - schema: Parameters[0], - options: Parameters[1], + schema: Parameters[0], + options: Parameters[1], validationConfiguration?: ValidationErrorConfiguration ) => void; export const version: string;