Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 32 additions & 33 deletions types/loader-utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/// <reference types="node" />

import { loader } from "webpack";
import { LoaderContext } from "webpack";

export type Readonly<T> = {
readonly [P in keyof T]: T[P];
};

export interface LoaderInterpolateOption {
customInterpolateName?: typeof interpolateName;
}

export interface InterpolateOption {
context?: string | undefined;
content?: string | Buffer | undefined;
Expand All @@ -16,36 +20,26 @@ export interface OptionObject {
[key: string]: null | false | true | string;
}

export type HashType = "sha1" | "md4" | "md5" | "sha256" | "sha512";

export type DigestType = "hex" | "base26" | "base32" | "base36" | "base49" | "base52" | "base58" | "base62" | "base64";

/**
* Recommended way to retrieve the options of a loader invocation
* {@link https://github.com/webpack/loader-utils#getoptions}
*/
export function getOptions(loaderContext: loader.LoaderContext): Readonly<OptionObject>;

export type InterpolateNameType = string | ((resourcePath: string, resourceQuery?: string) => string);

export type HashType = "xxhash64" | "sha1" | "md4" | "native-md4" | "md5" | "sha256" | "sha512";

export type DigestType =
| "hex"
| "base26"
| "base32"
| "base36"
| "base49"
| "base52"
| "base58"
| "base62"
| "base64"
| "base64safe";
/**
* Parses a passed string (e.g. loaderContext.resourceQuery) as a query string, and returns an object.
* {@link https://github.com/webpack/loader-utils#parsequery}
* {@link https://https://github.com/webpack/loader-utils#urltorequest}
*/
export function parseQuery(optionString: string): OptionObject;

/**
* Turns a request into a string that can be used inside require() or import while avoiding absolute paths. Use it instead of JSON.stringify(...) if you're generating code inside a loader.
* {@link https://github.com/webpack/loader-utils#stringifyrequest}
*/
export function stringifyRequest(loaderContext: loader.LoaderContext, resource: string): string;

export function getRemainingRequest(loaderContext: loader.LoaderContext): string;

export function getCurrentRequest(loaderContext: loader.LoaderContext): string;

export function isUrlRequest(url: string, root?: string): boolean;

export function parseString(str: string): string;

/**
* Converts some resource URL to a webpack module request.
* {@link https://github.com/webpack/loader-utils#urltorequest}
Expand All @@ -57,12 +51,17 @@ export function urlToRequest(url: string, root?: string): string;
* The template and regular expression are set as query params called name and regExp on the current loader's context.
* {@link https://github.com/webpack/loader-utils#interpolatename}
*/
export function interpolateName(loaderContext: loader.LoaderContext, name: string, options?: any): string;
export function interpolateName(
loaderContext: LoaderContext<LoaderInterpolateOption>,
name: InterpolateNameType,
options?: InterpolateOption,
): string;

/**
* @param buffer
* @param [hashType='md4']
* @param [digestType='hex']
* @param [maxLength=9999]
* @param buffer the content that should be hashed
* @param [hashType='xxhash64'] one of `xxhash64`, `sha1`, `md4`, `md5`, `sha256`, `sha512` or any other node.js supported hash type
* @param [digestType='hex'] one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64safe`
* @param [maxLength=9999] the maximum length in chars
* {@link https://github.com/webpack/loader-utils#gethashdigest}
*/
export function getHashDigest(buffer: Buffer, hashType: HashType, digestType: DigestType, maxLength: number): string;
export function getHashDigest(buffer: Buffer, hashType?: HashType, digestType?: DigestType, maxLength?: number): string;
75 changes: 13 additions & 62 deletions types/loader-utils/loader-utils-tests.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,23 @@
import {
getCurrentRequest,
getHashDigest,
getOptions,
getRemainingRequest,
interpolateName,
isUrlRequest,
parseQuery,
parseString,
stringifyRequest,
urlToRequest,
} from "loader-utils";
import { loader } from "webpack";

parseQuery("?{data:{a:1},isJSON5:true}");
parseString(`"123"`); // "123"
parseString(`'123'`); // "123"
parseString(`123`); // "123"
const jsonStr = JSON.stringify({ a: 1, b: 2 });
const parsed = parseString(jsonStr);
jsonStr === parsed; // true

function loader(this: loader.LoaderContext) {
getOptions(this);
import { getHashDigest, interpolateName, isUrlRequest, type LoaderInterpolateOption, urlToRequest } from "loader-utils";
import type { LoaderContext } from "webpack";

function loader(this: LoaderContext<LoaderInterpolateOption>) {
// get options readonly
const options = getOptions(this);
const options = this.getOptions(this);
// @ts-expect-error
options.prop = {};

getRemainingRequest(this);

getCurrentRequest(this);

stringifyRequest(this, "./test.js");
// "\"./test.js\""

stringifyRequest(this, ".\\test.js");
// "\"./test.js\""

stringifyRequest(this, "test");
// "\"test\""

stringifyRequest(this, "test/lib/index.js");
// "\"test/lib/index.js\""

stringifyRequest(this, "otherLoader?andConfig!test?someConfig");
// "\"otherLoader?andConfig!test?someConfig\""

stringifyRequest(this, "C:\\module\\test.js");
// "\"../../test.js\"" (on Windows, in case the module and the request are on the same drive)

stringifyRequest(this, "C:\\module\\test.js");
// "\"C:\\module\\test.js\"" (on Windows, in case the module and the request are on different drives)

stringifyRequest(this, "\\\\network-drive\\test.js");
// "\"\\\\network-drive\\\\test.js\"" (on Windows, in case the module and the request are on different drives)
urlToRequest("path/to/module.js"); // "./path/to/module.js"
urlToRequest("~path/to/module.js"); // "path/to/module.js"
urlToRequest("/path/to/module.js", "./root"); // "./root/path/to/module.js"
urlToRequest("/path/to/module.js", "~"); // "path/to/module.js"
isUrlRequest("path/to/module.js");
isUrlRequest("~path/to/module.js");
isUrlRequest("/path/to/module.js", "./root");
isUrlRequest("/path/to/module.js", "~");
}

urlToRequest("path/to/module.js"); // "./path/to/module.js"
urlToRequest("~path/to/module.js"); // "path/to/module.js"
urlToRequest("/path/to/module.js", "./root"); // "./root/path/to/module.js"
urlToRequest("/path/to/module.js", "~"); // "path/to/module.js"
isUrlRequest("path/to/module.js");
isUrlRequest("~path/to/module.js");
isUrlRequest("/path/to/module.js", "./root");
isUrlRequest("/path/to/module.js", "~");

function loader2(this: loader.LoaderContext) {
function loader2(this: LoaderContext<LoaderInterpolateOption>) {
// loaderContext.resourcePath = "/app/js/javascript.js"
interpolateName(this, "js/[hash].script.[ext]", { content: "" });
// => js/9473fdd0d880a43c21b7778d34872157.script.js
Expand Down
4 changes: 2 additions & 2 deletions types/loader-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"private": true,
"name": "@types/loader-utils",
"version": "2.0.9999",
"version": "3.0.9999",
"projects": [
"https://github.com/webpack/loader-utils#readme"
],
"dependencies": {
"@types/node": "*",
"@types/webpack": "^4"
"webpack": "^5"
},
"devDependencies": {
"@types/loader-utils": "workspace:."
Expand Down
19 changes: 17 additions & 2 deletions types/react/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,7 @@ declare namespace React {
charSet?: string | undefined;
crossOrigin?: CrossOrigin;
defer?: boolean | undefined;
fetchPriority?: "high" | "low" | "auto" | undefined;
integrity?: string | undefined;
noModule?: boolean | undefined;
referrerPolicy?: HTMLAttributeReferrerPolicy | undefined;
Expand Down Expand Up @@ -3572,7 +3573,21 @@ declare namespace React {
direction?: number | string | undefined;
display?: number | string | undefined;
divisor?: number | string | undefined;
dominantBaseline?: number | string | undefined;
dominantBaseline?:
| "auto"
| "use-script"
| "no-change"
| "reset-size"
| "ideographic"
| "alphabetic"
| "hanging"
| "mathematical"
| "central"
| "middle"
| "text-after-edge"
| "text-before-edge"
| "inherit"
| undefined;
dur?: number | string | undefined;
dx?: number | string | undefined;
dy?: number | string | undefined;
Expand Down Expand Up @@ -3719,7 +3734,7 @@ declare namespace React {
tableValues?: number | string | undefined;
targetX?: number | string | undefined;
targetY?: number | string | undefined;
textAnchor?: string | undefined;
textAnchor?: "start" | "middle" | "end" | "inherit" | undefined;
textDecoration?: number | string | undefined;
textLength?: number | string | undefined;
textRendering?: number | string | undefined;
Expand Down
19 changes: 17 additions & 2 deletions types/react/ts5.0/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3363,6 +3363,7 @@ declare namespace React {
charSet?: string | undefined;
crossOrigin?: CrossOrigin;
defer?: boolean | undefined;
fetchPriority?: "high" | "low" | "auto" | undefined;
integrity?: string | undefined;
noModule?: boolean | undefined;
referrerPolicy?: HTMLAttributeReferrerPolicy | undefined;
Expand Down Expand Up @@ -3570,7 +3571,21 @@ declare namespace React {
direction?: number | string | undefined;
display?: number | string | undefined;
divisor?: number | string | undefined;
dominantBaseline?: number | string | undefined;
dominantBaseline?:
| "auto"
| "use-script"
| "no-change"
| "reset-size"
| "ideographic"
| "alphabetic"
| "hanging"
| "mathematical"
| "central"
| "middle"
| "text-after-edge"
| "text-before-edge"
| "inherit"
| undefined;
dur?: number | string | undefined;
dx?: number | string | undefined;
dy?: number | string | undefined;
Expand Down Expand Up @@ -3717,7 +3732,7 @@ declare namespace React {
tableValues?: number | string | undefined;
targetX?: number | string | undefined;
targetY?: number | string | undefined;
textAnchor?: string | undefined;
textAnchor?: "start" | "middle" | "end" | "inherit" | undefined;
textDecoration?: number | string | undefined;
textLength?: number | string | undefined;
textRendering?: number | string | undefined;
Expand Down
18 changes: 16 additions & 2 deletions types/react/ts5.0/v18/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3733,7 +3733,21 @@ declare namespace React {
direction?: number | string | undefined;
display?: number | string | undefined;
divisor?: number | string | undefined;
dominantBaseline?: number | string | undefined;
dominantBaseline?:
| "auto"
| "use-script"
| "no-change"
| "reset-size"
| "ideographic"
| "alphabetic"
| "hanging"
| "mathematical"
| "central"
| "middle"
| "text-after-edge"
| "text-before-edge"
| "inherit"
| undefined;
dur?: number | string | undefined;
dx?: number | string | undefined;
dy?: number | string | undefined;
Expand Down Expand Up @@ -3880,7 +3894,7 @@ declare namespace React {
tableValues?: number | string | undefined;
targetX?: number | string | undefined;
targetY?: number | string | undefined;
textAnchor?: string | undefined;
textAnchor?: "start" | "middle" | "end" | "inherit" | undefined;
textDecoration?: number | string | undefined;
textLength?: number | string | undefined;
textRendering?: number | string | undefined;
Expand Down
18 changes: 16 additions & 2 deletions types/react/ts5.0/v18/ts5.0/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3734,7 +3734,21 @@ declare namespace React {
direction?: number | string | undefined;
display?: number | string | undefined;
divisor?: number | string | undefined;
dominantBaseline?: number | string | undefined;
dominantBaseline?:
| "auto"
| "use-script"
| "no-change"
| "reset-size"
| "ideographic"
| "alphabetic"
| "hanging"
| "mathematical"
| "central"
| "middle"
| "text-after-edge"
| "text-before-edge"
| "inherit"
| undefined;
dur?: number | string | undefined;
dx?: number | string | undefined;
dy?: number | string | undefined;
Expand Down Expand Up @@ -3881,7 +3895,7 @@ declare namespace React {
tableValues?: number | string | undefined;
targetX?: number | string | undefined;
targetY?: number | string | undefined;
textAnchor?: string | undefined;
textAnchor?: "start" | "middle" | "end" | "inherit" | undefined;
textDecoration?: number | string | undefined;
textLength?: number | string | undefined;
textRendering?: number | string | undefined;
Expand Down
18 changes: 16 additions & 2 deletions types/react/v15/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3311,7 +3311,21 @@ declare namespace React {
direction?: number | string | undefined;
display?: number | string | undefined;
divisor?: number | string | undefined;
dominantBaseline?: number | string | undefined;
dominantBaseline?:
| "auto"
| "use-script"
| "no-change"
| "reset-size"
| "ideographic"
| "alphabetic"
| "hanging"
| "mathematical"
| "central"
| "middle"
| "text-after-edge"
| "text-before-edge"
| "inherit"
| undefined;
dur?: number | string | undefined;
dx?: number | string | undefined;
dy?: number | string | undefined;
Expand Down Expand Up @@ -3457,7 +3471,7 @@ declare namespace React {
tableValues?: number | string | undefined;
targetX?: number | string | undefined;
targetY?: number | string | undefined;
textAnchor?: string | undefined;
textAnchor?: "start" | "middle" | "end" | "inherit" | undefined;
textDecoration?: number | string | undefined;
textLength?: number | string | undefined;
textRendering?: number | string | undefined;
Expand Down
Loading