2021-04-20 16:06:34 +08:00
|
|
|
import type { RawSourceMap } from "source-map";
|
2021-04-20 16:01:46 +08:00
|
|
|
import type { Schema } from "schema-utils/declarations/validate";
|
2021-04-20 02:30:58 +08:00
|
|
|
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";
|
2021-04-20 02:30:58 +08:00
|
|
|
|
2021-04-20 13:05:54 +08:00
|
|
|
export interface LoaderContext {
|
2021-04-20 02:30:58 +08:00
|
|
|
version: number;
|
2021-04-20 16:01:46 +08:00
|
|
|
getOptions(schema: Schema): any;
|
2021-04-20 02:30:58 +08:00
|
|
|
emitWarning(warning: Error | string): void;
|
|
|
|
emitError(error: Error | string): void;
|
2021-04-20 16:06:34 +08:00
|
|
|
getLogger(name: string): Logger;
|
2021-04-20 02:30:58 +08:00
|
|
|
resolve(context: string, request: string, callback: any): any;
|
|
|
|
getResolve(
|
|
|
|
options: Configuration
|
|
|
|
): (context: string, request: string, callback: any) => Promise<any>;
|
|
|
|
emitFile(
|
|
|
|
name: string,
|
|
|
|
content: string,
|
2021-04-20 16:07:59 +08:00
|
|
|
sourceMap?: string,
|
|
|
|
assetInfo?: AssetInfo
|
2021-04-20 02:30:58 +08:00
|
|
|
): void;
|
|
|
|
addBuildDependency(dep: string): void;
|
|
|
|
utils: {
|
|
|
|
absolutify: (context: string, request: string) => string;
|
|
|
|
contextify: (context: string, request: string) => string;
|
|
|
|
};
|
|
|
|
rootContext: string;
|
2021-04-20 16:00:32 +08:00
|
|
|
webpack?: boolean;
|
|
|
|
sourceMap?: boolean;
|
2021-04-20 02:30:58 +08:00
|
|
|
mode: Mode;
|
|
|
|
_module: NormalModule;
|
|
|
|
_compilation: Compilation;
|
|
|
|
_compiler: Compilation.Compiler;
|
|
|
|
fs: InputFileSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The types added to LoaderContextBase by https://github.com/webpack/loader-runner */
|
2021-04-20 13:05:54 +08:00
|
|
|
export interface EmptyContextAdditions {
|
2021-04-20 02:30:58 +08:00
|
|
|
/**
|
|
|
|
* 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,
|
2021-04-20 16:00:51 +08:00
|
|
|
sourceMap?: string | RawSourceMap,
|
|
|
|
additionalData?: Record<string, any>,
|
|
|
|
...args: any[]
|
2021-04-20 02:30:58 +08:00
|
|
|
) => 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;
|
|
|
|
|
2021-04-20 13:05:54 +08:00
|
|
|
readonly previousRequest: string;
|
2021-04-20 02:30:58 +08:00
|
|
|
|
|
|
|
readonly query: any;
|
|
|
|
|
2021-04-20 13:05:54 +08:00
|
|
|
readonly remainingRequest: string;
|
2021-04-20 02:30:58 +08:00
|
|
|
|
2021-04-20 13:05:54 +08:00
|
|
|
readonly request: string;
|
2021-04-20 02:30:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}[];
|
2021-04-20 02:30:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The resource file.
|
|
|
|
* In the example: "/abc/resource.js"
|
|
|
|
*/
|
|
|
|
resourcePath: string;
|
|
|
|
}
|
2021-04-20 13:05:54 +08:00
|
|
|
|
2021-04-20 15:41:42 +08:00
|
|
|
export interface LoaderDefinition {
|
2021-04-20 16:06:34 +08:00
|
|
|
(this: LoaderContext & EmptyContextAdditions, contents: string): string;
|
2021-04-20 16:00:20 +08:00
|
|
|
}
|