improve LoaderContext declaration

upgrade tooling
This commit is contained in:
Tobias Koppers 2021-04-22 21:45:06 +02:00
parent 7cc40782b7
commit ea53a23827
8 changed files with 352 additions and 128 deletions

View File

@ -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<Resolver["resolve"]>[4];
/** These properties are added by the NormalModule */
export interface NormalModuleLoaderContext<OptionsType> {
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<any>;
options?: ResolveOptionsWithDependencyType
): ((context: string, request: string, callback: ResolveCallback) => void) &
((context: string, request: string) => Promise<string>);
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;
}
/** The types added to LoaderContextBase by https://github.com/webpack/loader-runner */
export interface LoaderRunnerLoaderContext {
/** 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<any>;
}
/** The properties are added by https://github.com/webpack/loader-runner */
export interface LoaderRunnerLoaderContext<OptionsType> {
/**
* 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;
type LoaderContext<OptionsType> = NormalModuleLoaderContext<OptionsType> &
LoaderRunnerLoaderContext<OptionsType> &
LoaderPluginLoaderContext &
HotModuleReplacementPluginLoaderContext;
export type LoaderDefinition =
| {
(
this: LoaderContext,
content: string,
sourceMap?: string | RawSourceMap,
additionalData?: AdditionalData
): string | Buffer | Promise<string | Buffer> | void;
raw?: false;
}
| {
(
this: LoaderContext,
content: Buffer,
sourceMap?: string | RawSourceMap,
additionalData?: AdditionalData
): string | Buffer | Promise<string | Buffer> | void;
raw: true;
};
type PitchLoaderDefinitionFunction<OptionsType = {}, ContextAdditions = {}> = (
this: LoaderContext<OptionsType> & ContextAdditions,
remainingRequest: string,
previousRequest: string,
data: object
) => string | Buffer | Promise<string | Buffer> | void;
type LoaderDefinitionFunction<OptionsType = {}, ContextAdditions = {}> = (
this: LoaderContext<OptionsType> & ContextAdditions,
content: string,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
) => string | Buffer | Promise<string | Buffer> | void;
type RawLoaderDefinitionFunction<OptionsType = {}, ContextAdditions = {}> = (
this: LoaderContext<OptionsType> & ContextAdditions,
content: Buffer,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
) => string | Buffer | Promise<string | Buffer> | void;
export type LoaderDefinition<
OptionsType = {},
ContextAdditions = {}
> = LoaderDefinitionFunction<OptionsType, ContextAdditions> & {
raw?: false;
pitch?: PitchLoaderDefinitionFunction<OptionsType, ContextAdditions>;
};
export type RawLoaderDefinition<
OptionsType = {},
ContextAdditions = {}
> = RawLoaderDefinitionFunction<OptionsType, ContextAdditions> & {
raw: true;
pitch?: PitchLoaderDefinitionFunction<OptionsType, ContextAdditions>;
};
export interface LoaderModule<OptionsType = {}, ContextAdditions = {}> {
default?:
| RawLoaderDefinitionFunction<OptionsType, ContextAdditions>
| LoaderDefinitionFunction<OptionsType, ContextAdditions>;
raw?: false;
pitch?: PitchLoaderDefinitionFunction<OptionsType, ContextAdditions>;
}

9
declarations/index.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
export type {
LoaderModule,
RawLoaderDefinition,
LoaderDefinition,
LoaderDefinitionFunction,
PitchLoaderDefinitionFunction,
RawLoaderDefinitionFunction,
LoaderContext
} from "./LoaderContext";

View File

@ -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 /]
};

View File

@ -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 */

View File

@ -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",

View File

@ -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"
}
]
}

249
types.d.ts vendored
View File

@ -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<string | Buffer>;
raw?: false;
}
| {
(
this: LoaderContext,
content: Buffer,
sourceMap?: string | RawSourceMap,
additionalData?: AdditionalData
): string | void | Buffer | Promise<string | Buffer>;
raw: true;
};
type LoaderContext<OptionsType> = NormalModuleLoaderContext<OptionsType> &
LoaderRunnerLoaderContext<OptionsType> &
LoaderPluginLoaderContext &
HotModuleReplacementPluginLoaderContext;
type LoaderDefinition<
OptionsType = {},
ContextAdditions = {}
> = LoaderDefinitionFunction<OptionsType, ContextAdditions> & {
raw?: false;
pitch?: PitchLoaderDefinitionFunction<OptionsType, ContextAdditions>;
};
declare interface LoaderDefinitionFunction<
OptionsType = {},
ContextAdditions = {}
> {
(
this: NormalModuleLoaderContext<OptionsType> &
LoaderRunnerLoaderContext<OptionsType> &
LoaderPluginLoaderContext &
HotModuleReplacementPluginLoaderContext &
ContextAdditions,
content: string,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
): string | void | Buffer | Promise<string | Buffer>;
}
declare interface LoaderItem {
loader: string;
options: any;
ident: null | string;
type: null | string;
}
declare interface LoaderModule<OptionsType = {}, ContextAdditions = {}> {
default?:
| RawLoaderDefinitionFunction<OptionsType, ContextAdditions>
| LoaderDefinitionFunction<OptionsType, ContextAdditions>;
raw?: false;
pitch?: PitchLoaderDefinitionFunction<OptionsType, ContextAdditions>;
}
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 LoaderRunnerLoaderContext {
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<any>;
}
/**
* The properties are added by https://github.com/webpack/loader-runner
*/
declare interface LoaderRunnerLoaderContext<OptionsType> {
/**
* 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<any>;
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<OptionsType> {
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<any>;
options?: ResolveOptionsWithDependencyType
): {
(
context: string,
request: string,
callback: (
arg0: null | Error,
arg1?: string | false,
arg2?: ResolveRequest
) => void
): void;
(context: string, request: string): Promise<string>;
};
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<OptionsType> &
LoaderRunnerLoaderContext<OptionsType> &
LoaderPluginLoaderContext &
HotModuleReplacementPluginLoaderContext &
ContextAdditions,
remainingRequest: string,
previousRequest: string,
data: object
): string | void | Buffer | Promise<string | Buffer>;
}
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<OptionsType, ContextAdditions> & {
raw: true;
pitch?: PitchLoaderDefinitionFunction<OptionsType, ContextAdditions>;
};
declare interface RawLoaderDefinitionFunction<
OptionsType = {},
ContextAdditions = {}
> {
(
this: NormalModuleLoaderContext<OptionsType> &
LoaderRunnerLoaderContext<OptionsType> &
LoaderPluginLoaderContext &
HotModuleReplacementPluginLoaderContext &
ContextAdditions,
content: Buffer,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
): string | void | Buffer | Promise<string | Buffer>;
}
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<RawSourceMap, "version"> & { 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
};
}

View File

@ -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: