diff --git a/types/loader-utils/index.d.ts b/types/loader-utils/index.d.ts new file mode 100644 index 0000000000..ba6be659f1 --- /dev/null +++ b/types/loader-utils/index.d.ts @@ -0,0 +1,25 @@ +// Type definitions for loader-utils 1.1 +// Project: https://github.com/webpack/loader-utils#readme +// Definitions by: Gyusun Yeom +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + +import { loader } from "webpack"; + +export interface InterpolateOption { + context?: string; + content?: string | Buffer; + regExp?: string | RegExp; +} +export interface OptionObject { + [key: string]: any; +} +export type HashType = "sha1" | "md5" | "sha256" | "sha512"; +export type DigestType = "hex" | "base26" | "base32" | "base36" | "base49" | "base52" | "base58" | "base62" | "base64"; + +export function getOptions(loaderContext: loader.LoaderContext): OptionObject; +export function parseQuery(optionString: string): OptionObject; +export function stringifyRequest(loaderContext: loader.LoaderContext, resource: string): string; +export function urlToRequest(url: string, root?: string): string; +export function interpolateName(loaderContext: loader.LoaderContext, name: string, options?: any): string; +export function getHashDigest(buffer: Buffer, hashType: HashType, digestType: DigestType, maxLength: number): string; diff --git a/types/loader-utils/loader-utils-tests.ts b/types/loader-utils/loader-utils-tests.ts new file mode 100644 index 0000000000..6ce002a5d1 --- /dev/null +++ b/types/loader-utils/loader-utils-tests.ts @@ -0,0 +1,80 @@ +import { loader } from "webpack"; +import { getOptions, parseQuery, stringifyRequest, urlToRequest, interpolateName, getHashDigest } from "loader-utils"; + +parseQuery("?{data:{a:1},isJSON5:true}"); + +function loader(this: loader.LoaderContext) { + getOptions(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" + +function loader2(this: loader.LoaderContext) { +// loaderContext.resourcePath = "/app/js/javascript.js" + interpolateName(this, "js/[hash].script.[ext]", { content: "" }); + // => js/9473fdd0d880a43c21b7778d34872157.script.js + + // loaderContext.resourcePath = "/app/page.html" + interpolateName(this, "html-[hash:6].html", { content: "" }); + // => html-9473fd.html + + // loaderContext.resourcePath = "/app/flash.txt" + interpolateName(this, "[hash]", { content: "" }); + // => c31e9820c001c9c4a86bce33ce43b679 + + // loaderContext.resourcePath = "/app/img/image.gif" + interpolateName(this, "[emoji]", { content: "" }); + // => 👍 + + // loaderContext.resourcePath = "/app/img/image.gif" + interpolateName(this, "[emoji:4]", { content: "" }); + // => 🙍🏢📤🐝 + + // loaderContext.resourcePath = "/app/img/image.png" + interpolateName(this, "[sha512:hash:base64:7].[ext]", { content: "" }); + // => 2BKDTjl.png + // use sha512 hash instead of md5 and with only 7 chars of base64 + + // loaderContext.resourcePath = "/app/img/myself.png" + // loaderContext.query.name = + interpolateName(this, "picture.png"); + // => picture.png + + // loaderContext.resourcePath = "/app/dir/file.png" + interpolateName(this, "[path][name].[ext]?[hash]", { content: "" }); + // => /app/dir/file.png?9473fdd0d880a43c21b7778d34872157 + + // loaderContext.resourcePath = "/app/js/page-home.js" + interpolateName(this, "script-[1].[ext]", { regExp: "page-(.*)\\.js", content: "" }); + // => script-home.js +} + +const buffer = new Buffer(""); +const digestString = getHashDigest(buffer, "sha512", "hex", 32); diff --git a/types/loader-utils/tsconfig.json b/types/loader-utils/tsconfig.json new file mode 100644 index 0000000000..9b57f2b7b2 --- /dev/null +++ b/types/loader-utils/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "loader-utils-tests.ts" + ] +} diff --git a/types/loader-utils/tslint.json b/types/loader-utils/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/loader-utils/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }