Skip to content

Commit

Permalink
improve LoaderContext declaration
Browse files Browse the repository at this point in the history
upgrade tooling
  • Loading branch information
sokra committed Apr 22, 2021
1 parent 7cc4078 commit ea53a23
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 129 deletions.
189 changes: 130 additions & 59 deletions declarations/LoaderContext.d.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<any>;
}

/** 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<OptionsType> {
/**
* Add a directory as dependency of the loader result.
*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 = {
Expand All @@ -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<string | Buffer> | void;
raw?: false;
}
| {
(
this: LoaderContext,
content: Buffer,
sourceMap?: string | RawSourceMap,
additionalData?: AdditionalData
): string | Buffer | Promise<string | Buffer> | void;
raw: true;
};
type LoaderContext<OptionsType> = NormalModuleLoaderContext<OptionsType> &
LoaderRunnerLoaderContext<OptionsType> &
LoaderPluginLoaderContext &
HotModuleReplacementPluginLoaderContext;

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 changes: 9 additions & 0 deletions declarations/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type {
LoaderModule,
RawLoaderDefinition,
LoaderDefinition,
LoaderDefinitionFunction,
PitchLoaderDefinitionFunction,
RawLoaderDefinitionFunction,
LoaderContext
} from "./LoaderContext";
3 changes: 2 additions & 1 deletion generate-types-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 /]
};
2 changes: 0 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions schemas/WebpackOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
Loading

0 comments on commit ea53a23

Please sign in to comment.