Skip to content

Commit 9a1eaa2

Browse files
authored
🤖 Merge PR DefinitelyTyped#73397 feat(loader-utils): Update to v3 by @Gehbt
1 parent 3f0a5cd commit 9a1eaa2

File tree

3 files changed

+47
-97
lines changed

3 files changed

+47
-97
lines changed

types/loader-utils/index.d.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
/// <reference types="node" />
22

3-
import { loader } from "webpack";
3+
import { LoaderContext } from "webpack";
44

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

9+
export interface LoaderInterpolateOption {
10+
customInterpolateName?: typeof interpolateName;
11+
}
12+
913
export interface InterpolateOption {
1014
context?: string | undefined;
1115
content?: string | Buffer | undefined;
@@ -16,36 +20,26 @@ export interface OptionObject {
1620
[key: string]: null | false | true | string;
1721
}
1822

19-
export type HashType = "sha1" | "md4" | "md5" | "sha256" | "sha512";
20-
21-
export type DigestType = "hex" | "base26" | "base32" | "base36" | "base49" | "base52" | "base58" | "base62" | "base64";
22-
23-
/**
24-
* Recommended way to retrieve the options of a loader invocation
25-
* {@link https://github.com/webpack/loader-utils#getoptions}
26-
*/
27-
export function getOptions(loaderContext: loader.LoaderContext): Readonly<OptionObject>;
28-
23+
export type InterpolateNameType = string | ((resourcePath: string, resourceQuery?: string) => string);
24+
25+
export type HashType = "xxhash64" | "sha1" | "md4" | "native-md4" | "md5" | "sha256" | "sha512";
26+
27+
export type DigestType =
28+
| "hex"
29+
| "base26"
30+
| "base32"
31+
| "base36"
32+
| "base49"
33+
| "base52"
34+
| "base58"
35+
| "base62"
36+
| "base64"
37+
| "base64safe";
2938
/**
30-
* Parses a passed string (e.g. loaderContext.resourceQuery) as a query string, and returns an object.
31-
* {@link https://github.com/webpack/loader-utils#parsequery}
39+
* {@link https://https://github.com/webpack/loader-utils#urltorequest}
3240
*/
33-
export function parseQuery(optionString: string): OptionObject;
34-
35-
/**
36-
* 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.
37-
* {@link https://github.com/webpack/loader-utils#stringifyrequest}
38-
*/
39-
export function stringifyRequest(loaderContext: loader.LoaderContext, resource: string): string;
40-
41-
export function getRemainingRequest(loaderContext: loader.LoaderContext): string;
42-
43-
export function getCurrentRequest(loaderContext: loader.LoaderContext): string;
44-
4541
export function isUrlRequest(url: string, root?: string): boolean;
4642

47-
export function parseString(str: string): string;
48-
4943
/**
5044
* Converts some resource URL to a webpack module request.
5145
* {@link https://github.com/webpack/loader-utils#urltorequest}
@@ -57,12 +51,17 @@ export function urlToRequest(url: string, root?: string): string;
5751
* The template and regular expression are set as query params called name and regExp on the current loader's context.
5852
* {@link https://github.com/webpack/loader-utils#interpolatename}
5953
*/
60-
export function interpolateName(loaderContext: loader.LoaderContext, name: string, options?: any): string;
54+
export function interpolateName(
55+
loaderContext: LoaderContext<LoaderInterpolateOption>,
56+
name: InterpolateNameType,
57+
options?: InterpolateOption,
58+
): string;
6159

6260
/**
63-
* @param buffer
64-
* @param [hashType='md4']
65-
* @param [digestType='hex']
66-
* @param [maxLength=9999]
61+
* @param buffer the content that should be hashed
62+
* @param [hashType='xxhash64'] one of `xxhash64`, `sha1`, `md4`, `md5`, `sha256`, `sha512` or any other node.js supported hash type
63+
* @param [digestType='hex'] one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64safe`
64+
* @param [maxLength=9999] the maximum length in chars
65+
* {@link https://github.com/webpack/loader-utils#gethashdigest}
6766
*/
68-
export function getHashDigest(buffer: Buffer, hashType: HashType, digestType: DigestType, maxLength: number): string;
67+
export function getHashDigest(buffer: Buffer, hashType?: HashType, digestType?: DigestType, maxLength?: number): string;

types/loader-utils/loader-utils-tests.ts

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,23 @@
1-
import {
2-
getCurrentRequest,
3-
getHashDigest,
4-
getOptions,
5-
getRemainingRequest,
6-
interpolateName,
7-
isUrlRequest,
8-
parseQuery,
9-
parseString,
10-
stringifyRequest,
11-
urlToRequest,
12-
} from "loader-utils";
13-
import { loader } from "webpack";
14-
15-
parseQuery("?{data:{a:1},isJSON5:true}");
16-
parseString(`"123"`); // "123"
17-
parseString(`'123'`); // "123"
18-
parseString(`123`); // "123"
19-
const jsonStr = JSON.stringify({ a: 1, b: 2 });
20-
const parsed = parseString(jsonStr);
21-
jsonStr === parsed; // true
22-
23-
function loader(this: loader.LoaderContext) {
24-
getOptions(this);
1+
import { getHashDigest, interpolateName, isUrlRequest, type LoaderInterpolateOption, urlToRequest } from "loader-utils";
2+
import type { LoaderContext } from "webpack";
253

4+
function loader(this: LoaderContext<LoaderInterpolateOption>) {
265
// get options readonly
27-
const options = getOptions(this);
6+
const options = this.getOptions(this);
287
// @ts-expect-error
298
options.prop = {};
309

31-
getRemainingRequest(this);
32-
33-
getCurrentRequest(this);
34-
35-
stringifyRequest(this, "./test.js");
36-
// "\"./test.js\""
37-
38-
stringifyRequest(this, ".\\test.js");
39-
// "\"./test.js\""
40-
41-
stringifyRequest(this, "test");
42-
// "\"test\""
43-
44-
stringifyRequest(this, "test/lib/index.js");
45-
// "\"test/lib/index.js\""
46-
47-
stringifyRequest(this, "otherLoader?andConfig!test?someConfig");
48-
// "\"otherLoader?andConfig!test?someConfig\""
49-
50-
stringifyRequest(this, "C:\\module\\test.js");
51-
// "\"../../test.js\"" (on Windows, in case the module and the request are on the same drive)
52-
53-
stringifyRequest(this, "C:\\module\\test.js");
54-
// "\"C:\\module\\test.js\"" (on Windows, in case the module and the request are on different drives)
55-
56-
stringifyRequest(this, "\\\\network-drive\\test.js");
57-
// "\"\\\\network-drive\\\\test.js\"" (on Windows, in case the module and the request are on different drives)
10+
urlToRequest("path/to/module.js"); // "./path/to/module.js"
11+
urlToRequest("~path/to/module.js"); // "path/to/module.js"
12+
urlToRequest("/path/to/module.js", "./root"); // "./root/path/to/module.js"
13+
urlToRequest("/path/to/module.js", "~"); // "path/to/module.js"
14+
isUrlRequest("path/to/module.js");
15+
isUrlRequest("~path/to/module.js");
16+
isUrlRequest("/path/to/module.js", "./root");
17+
isUrlRequest("/path/to/module.js", "~");
5818
}
5919

60-
urlToRequest("path/to/module.js"); // "./path/to/module.js"
61-
urlToRequest("~path/to/module.js"); // "path/to/module.js"
62-
urlToRequest("/path/to/module.js", "./root"); // "./root/path/to/module.js"
63-
urlToRequest("/path/to/module.js", "~"); // "path/to/module.js"
64-
isUrlRequest("path/to/module.js");
65-
isUrlRequest("~path/to/module.js");
66-
isUrlRequest("/path/to/module.js", "./root");
67-
isUrlRequest("/path/to/module.js", "~");
68-
69-
function loader2(this: loader.LoaderContext) {
20+
function loader2(this: LoaderContext<LoaderInterpolateOption>) {
7021
// loaderContext.resourcePath = "/app/js/javascript.js"
7122
interpolateName(this, "js/[hash].script.[ext]", { content: "" });
7223
// => js/9473fdd0d880a43c21b7778d34872157.script.js

types/loader-utils/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"private": true,
33
"name": "@types/loader-utils",
4-
"version": "2.0.9999",
4+
"version": "3.0.9999",
55
"projects": [
66
"https://github.com/webpack/loader-utils#readme"
77
],
88
"dependencies": {
99
"@types/node": "*",
10-
"@types/webpack": "^4"
10+
"webpack": "^5"
1111
},
1212
"devDependencies": {
1313
"@types/loader-utils": "workspace:."

0 commit comments

Comments
 (0)