webpack/declarations/LoaderContext.d.ts

186 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-04-20 16:06:34 +08:00
import type { RawSourceMap } from "source-map";
2021-04-21 02:21:32 +08:00
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";
2021-04-20 16:06:34 +08:00
import type { Logger } from "../lib/logging/Logger";
export interface NormalModuleLoaderContext {
version: number;
2021-04-20 16:01:46 +08:00
getOptions(schema: Schema): any;
emitWarning(warning: Error | string): void;
emitError(error: Error | string): void;
2021-04-20 16:06:34 +08:00
getLogger(name: string): Logger;
resolve(context: string, request: string, callback: any): any;
getResolve(
options: Configuration
): (context: string, request: string, callback: any) => Promise<any>;
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;
2021-04-20 16:18:59 +08:00
_compiler?: Compilation.Compiler;
fs: InputFileSystem;
}
/** The types added to LoaderContextBase by https://github.com/webpack/loader-runner */
export 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.
*/
2021-04-20 16:18:59 +08:00
async(): WebpackLoaderContextCallback;
/**
* 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;
2021-04-20 16:18:59 +08:00
callback(): WebpackLoaderContextCallback;
/**
* 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;
2021-04-20 13:05:54 +08:00
readonly previousRequest: string;
readonly query: any;
2021-04-20 13:05:54 +08:00
readonly remainingRequest: string;
2021-04-20 13:05:54 +08:00
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]
* }
* ]
*/
2021-04-20 16:06:34 +08:00
loaders: {
request: string;
path: string;
2021-04-20 13:05:54 +08:00
query: string;
fragment: string;
options: any;
ident: string;
normal: any;
pitch: any;
raw: any;
data: any;
pitchExecuted: boolean;
normalExecuted: boolean;
2021-04-20 16:06:34 +08:00
}[];
/**
* The resource file.
* In the example: "/abc/resource.js"
*/
resourcePath: string;
}
2021-04-20 13:05:54 +08:00
2021-04-20 16:18:59 +08:00
type WebpackLoaderContextCallback = (
err: Error | undefined | null,
content?: string | Buffer,
sourceMap?: string | RawSourceMap,
additionalData?: Record<string, any>,
...args: any[]
) => void | undefined;
type LoaderContext = NormalModuleLoaderContext & LoaderRunnerLoaderContext;
declare class EmptyContextAdditions {
_EmptyContextAdditions: true
}
2021-04-21 01:12:16 +08:00
export interface LoaderDefinition {
(this: LoaderContext & EmptyContextAdditions, contents: string): string;
}