Merge pull request #17836 from Perlmint/new/loader-utils

add new types loader-utils
This commit is contained in:
Ryan Cavanaugh
2017-07-13 10:51:58 -07:00
committed by GitHub
4 changed files with 128 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
// Type definitions for loader-utils 1.1
// Project: https://github.com/webpack/loader-utils#readme
// Definitions by: Gyusun Yeom <https://github.com/Perlmint>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
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;
+80
View File
@@ -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);
+22
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }