From 5f85f74928043f124a2f29c8736deed3636e87ed Mon Sep 17 00:00:00 2001 From: Franklin Tse Date: Tue, 28 Aug 2018 02:48:04 +0800 Subject: [PATCH 1/7] Initial commit --- types/webpack-assets-manifest/index.d.ts | 64 +++++++++++++++++++ types/webpack-assets-manifest/tsconfig.json | 23 +++++++ types/webpack-assets-manifest/tslint.json | 3 + .../webpack-assets-manifest-tests.ts | 0 4 files changed, 90 insertions(+) create mode 100644 types/webpack-assets-manifest/index.d.ts create mode 100644 types/webpack-assets-manifest/tsconfig.json create mode 100644 types/webpack-assets-manifest/tslint.json create mode 100644 types/webpack-assets-manifest/webpack-assets-manifest-tests.ts diff --git a/types/webpack-assets-manifest/index.d.ts b/types/webpack-assets-manifest/index.d.ts new file mode 100644 index 0000000000..9d09f23b54 --- /dev/null +++ b/types/webpack-assets-manifest/index.d.ts @@ -0,0 +1,64 @@ +// Type definitions for webpack-assets-manifest 3.0 +// Project: https://github.com/webdeveric/webpack-assets-manifest +// Definitions by: Franklin Tse +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import { Plugin } from "webpack"; +import { SyncHook, SyncWaterfallHook } from "tapable"; + +declare class WebpackAssetsManifest extends Plugin { + constructor(options?: WebpackAssetsManifest.Options); + hooks: { + apply: SyncHook; + customize: SyncWaterfallHook; + transform: SyncWaterfallHook; + done: SyncHook; + options: SyncWaterfallHook; + afterOptions: SyncHook; + }; + options: WebpackAssetsManifest.Options; + defaultOptions: WebpackAssetsManifest.Options; + isMerging: boolean; + getExtension(filename: string): string; + fixKey(key: T): T; + isHMR(filename: string): boolean; + setRaw(key: string, value: string): this; + set(key: string, value: string): this; + has(key: string): boolean; + get(key: string, defaultValue?: string): any; + delete(key: string): boolean; + getOutputPath(): string; + getPublicPath(filename: string): string; + getProxy(raw?: boolean): ProxyHandler; +} + +declare namespace WebpackAssetsManifest { + interface Options { + assets?: object; + output?: string; + replacer?: null | string[] | ((key: string, value: string) => number | string | boolean | null | object | undefined); + space?: number; + writeToDisk?: boolean; + fileExtRegex?: RegExp | null | false; + sortManifest?: boolean | ((a: any, b: any) => number); + merge?: boolean | "customize"; + publicPath?: string | boolean | null | (((filename: string, manifest: WebpackAssetsManifest) => string)); + apply?: ((manifest: WebpackAssetsManifest) => void) | null; + customize?: ((entry: Entry, original: object, manifest: WebpackAssetsManifest, asset: object) => Entry | false) | null; + transform?: ((assets: object, manifest: WebpackAssetsManifest) => any) | null; + done?: ((manifest: WebpackAssetsManifest, stats: object) => void) | null; + entrypoints?: boolean; + entrypointsKey?: string | false; + integrity?: boolean; + integrityHashes?: string[]; + integrityPropertyName?: string; + } + + interface Entry { + key: string; + value: string; + } +} + +export = WebpackAssetsManifest; diff --git a/types/webpack-assets-manifest/tsconfig.json b/types/webpack-assets-manifest/tsconfig.json new file mode 100644 index 0000000000..cbd18f692e --- /dev/null +++ b/types/webpack-assets-manifest/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "webpack-assets-manifest-tests.ts" + ] +} diff --git a/types/webpack-assets-manifest/tslint.json b/types/webpack-assets-manifest/tslint.json new file mode 100644 index 0000000000..b4b47a0378 --- /dev/null +++ b/types/webpack-assets-manifest/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} diff --git a/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts b/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts new file mode 100644 index 0000000000..e69de29bb2 From a5aae459aca4ea2f5ce1ef932d5bb6193de112b3 Mon Sep 17 00:00:00 2001 From: Franklin Tse Date: Tue, 28 Aug 2018 11:43:53 +0800 Subject: [PATCH 2/7] Add AnyObject --- types/webpack-assets-manifest/index.d.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/types/webpack-assets-manifest/index.d.ts b/types/webpack-assets-manifest/index.d.ts index 9d09f23b54..5a1e76dd02 100644 --- a/types/webpack-assets-manifest/index.d.ts +++ b/types/webpack-assets-manifest/index.d.ts @@ -37,7 +37,7 @@ declare namespace WebpackAssetsManifest { interface Options { assets?: object; output?: string; - replacer?: null | string[] | ((key: string, value: string) => number | string | boolean | null | object | undefined); + replacer?: null | ReadonlyArray | ((key: string, value: string) => number | string | boolean | null | object | undefined); space?: number; writeToDisk?: boolean; fileExtRegex?: RegExp | null | false; @@ -45,13 +45,13 @@ declare namespace WebpackAssetsManifest { merge?: boolean | "customize"; publicPath?: string | boolean | null | (((filename: string, manifest: WebpackAssetsManifest) => string)); apply?: ((manifest: WebpackAssetsManifest) => void) | null; - customize?: ((entry: Entry, original: object, manifest: WebpackAssetsManifest, asset: object) => Entry | false) | null; - transform?: ((assets: object, manifest: WebpackAssetsManifest) => any) | null; - done?: ((manifest: WebpackAssetsManifest, stats: object) => void) | null; + customize?: ((entry: Entry, original: AnyObject, manifest: WebpackAssetsManifest, asset: AnyObject) => Entry | false) | null; + transform?: ((assets: AnyObject, manifest: WebpackAssetsManifest) => any) | null; + done?: ((manifest: WebpackAssetsManifest, stats: AnyObject) => void) | null; entrypoints?: boolean; entrypointsKey?: string | false; integrity?: boolean; - integrityHashes?: string[]; + integrityHashes?: ReadonlyArray; integrityPropertyName?: string; } @@ -59,6 +59,10 @@ declare namespace WebpackAssetsManifest { key: string; value: string; } + + interface AnyObject extends Object { + [index: string]: any; + } } export = WebpackAssetsManifest; From 0739786b7fd5c38b9b869df2523fa86142262ead Mon Sep 17 00:00:00 2001 From: Franklin Tse Date: Tue, 28 Aug 2018 12:31:26 +0800 Subject: [PATCH 3/7] Add comments --- types/webpack-assets-manifest/index.d.ts | 87 ++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/types/webpack-assets-manifest/index.d.ts b/types/webpack-assets-manifest/index.d.ts index 5a1e76dd02..488de0050f 100644 --- a/types/webpack-assets-manifest/index.d.ts +++ b/types/webpack-assets-manifest/index.d.ts @@ -9,49 +9,128 @@ import { SyncHook, SyncWaterfallHook } from "tapable"; declare class WebpackAssetsManifest extends Plugin { constructor(options?: WebpackAssetsManifest.Options); + + /** https://github.com/webdeveric/webpack-assets-manifest#hooks */ hooks: { apply: SyncHook; - customize: SyncWaterfallHook; - transform: SyncWaterfallHook; - done: SyncHook; + + /** + * The `SyncWaterfallHook` class supports 3 type parameters only but this hook actually has 4 parameters. The type of 4th parameter is `AnyObject`. + * + * Refer to https://github.com/webdeveric/webpack-assets-manifest#hooks for details + * */ + customize: SyncWaterfallHook; + + transform: SyncWaterfallHook; + + done: SyncHook; + options: SyncWaterfallHook; + afterOptions: SyncHook; }; + + /** https://github.com/webdeveric/webpack-assets-manifest#options-read-the-schema */ options: WebpackAssetsManifest.Options; + + /** https://github.com/webdeveric/webpack-assets-manifest#options-read-the-schema */ defaultOptions: WebpackAssetsManifest.Options; + + /** Determine if the manifest data is currently being merged */ isMerging: boolean; + + /** Get the file extension */ getExtension(filename: string): string; - fixKey(key: T): T; + + /** Replace backslash with forward slash */ + fixKey(key: string): string; + + /** Determine if the filename matches the HMR filename pattern */ isHMR(filename: string): boolean; + + /** Add item to assets without modifying the key or value */ setRaw(key: string, value: string): this; + + /** Add an item to the manifest */ set(key: string, value: string): this; + + /** Determine if an item exist in the manifest */ has(key: string): boolean; + + /** Get an item from the manifest */ get(key: string, defaultValue?: string): any; + + /** Delete an item from the manifest */ delete(key: string): boolean; + + /** Get the file system path to the manifest */ getOutputPath(): string; + + /** Get the public path for the filename */ getPublicPath(filename: string): string; + + /** + * Get a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler) for the manifest + * + * @param raw - Use `setRaw` instead of `set` + * */ getProxy(raw?: boolean): ProxyHandler; } declare namespace WebpackAssetsManifest { interface Options { + /** https://github.com/webdeveric/webpack-assets-manifest#assets */ assets?: object; + + /** https://github.com/webdeveric/webpack-assets-manifest#output */ output?: string; + + /** https://github.com/webdeveric/webpack-assets-manifest#replacer */ replacer?: null | ReadonlyArray | ((key: string, value: string) => number | string | boolean | null | object | undefined); + + /** https://github.com/webdeveric/webpack-assets-manifest#space */ space?: number; + + /** https://github.com/webdeveric/webpack-assets-manifest#writetodisk */ writeToDisk?: boolean; + + /** https://github.com/webdeveric/webpack-assets-manifest#fileextregex */ fileExtRegex?: RegExp | null | false; + + /** https://github.com/webdeveric/webpack-assets-manifest#sortmanifest */ sortManifest?: boolean | ((a: any, b: any) => number); + + /** https://github.com/webdeveric/webpack-assets-manifest#merge */ merge?: boolean | "customize"; + + /** https://github.com/webdeveric/webpack-assets-manifest#publicpath */ publicPath?: string | boolean | null | (((filename: string, manifest: WebpackAssetsManifest) => string)); + + /** https://github.com/webdeveric/webpack-assets-manifest#apply */ apply?: ((manifest: WebpackAssetsManifest) => void) | null; + + /** https://github.com/webdeveric/webpack-assets-manifest#customize */ customize?: ((entry: Entry, original: AnyObject, manifest: WebpackAssetsManifest, asset: AnyObject) => Entry | false) | null; + + /** https://github.com/webdeveric/webpack-assets-manifest#transform */ transform?: ((assets: AnyObject, manifest: WebpackAssetsManifest) => any) | null; + + /** https://github.com/webdeveric/webpack-assets-manifest#done */ done?: ((manifest: WebpackAssetsManifest, stats: AnyObject) => void) | null; + + /** https://github.com/webdeveric/webpack-assets-manifest#entrypoints */ entrypoints?: boolean; + + /** https://github.com/webdeveric/webpack-assets-manifest#entrypointskey */ entrypointsKey?: string | false; + + /** https://github.com/webdeveric/webpack-assets-manifest#integrity */ integrity?: boolean; + + /** https://github.com/webdeveric/webpack-assets-manifest#integrityhashes */ integrityHashes?: ReadonlyArray; + + /** https://github.com/webdeveric/webpack-assets-manifest#integritypropertyname */ integrityPropertyName?: string; } From 38f534ff7a396d205e2843dff1d9874b92cbfc07 Mon Sep 17 00:00:00 2001 From: Franklin Tse Date: Tue, 28 Aug 2018 13:20:09 +0800 Subject: [PATCH 4/7] Bug fix; Add test cases --- types/webpack-assets-manifest/index.d.ts | 2 +- types/webpack-assets-manifest/tsconfig.json | 1 + types/webpack-assets-manifest/tslint.json | 9 +- .../webpack-assets-manifest-tests.ts | 103 ++++++++++++++++++ 4 files changed, 113 insertions(+), 2 deletions(-) diff --git a/types/webpack-assets-manifest/index.d.ts b/types/webpack-assets-manifest/index.d.ts index 488de0050f..c428d36294 100644 --- a/types/webpack-assets-manifest/index.d.ts +++ b/types/webpack-assets-manifest/index.d.ts @@ -98,7 +98,7 @@ declare namespace WebpackAssetsManifest { fileExtRegex?: RegExp | null | false; /** https://github.com/webdeveric/webpack-assets-manifest#sortmanifest */ - sortManifest?: boolean | ((a: any, b: any) => number); + sortManifest?: boolean | ((this: WebpackAssetsManifest, a: string, b: string) => number); /** https://github.com/webdeveric/webpack-assets-manifest#merge */ merge?: boolean | "customize"; diff --git a/types/webpack-assets-manifest/tsconfig.json b/types/webpack-assets-manifest/tsconfig.json index cbd18f692e..624eb9f943 100644 --- a/types/webpack-assets-manifest/tsconfig.json +++ b/types/webpack-assets-manifest/tsconfig.json @@ -8,6 +8,7 @@ "noImplicitThis": true, "strictNullChecks": true, "strictFunctionTypes": true, + "esModuleInterop": true, "baseUrl": "../", "typeRoots": [ "../" diff --git a/types/webpack-assets-manifest/tslint.json b/types/webpack-assets-manifest/tslint.json index b4b47a0378..369725d5a4 100644 --- a/types/webpack-assets-manifest/tslint.json +++ b/types/webpack-assets-manifest/tslint.json @@ -1,3 +1,10 @@ { - "extends": "dtslint/dt.json" + "extends": "dtslint/dt.json", + "rules": { + "indent": [ + true, + "tabs", + 4 + ] + } } diff --git a/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts b/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts index e69de29bb2..70436291a7 100644 --- a/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts +++ b/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts @@ -0,0 +1,103 @@ +import WebpackAssetsManifest from "webpack-assets-manifest"; + +/** https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/asset-integrity.js */ +new WebpackAssetsManifest({ + output: 'asset-integrity-manifest.json', + integrity: true, + publicPath: true, + customize(entry, original, manifest, asset) { + return { + key: entry.value, + value: asset && asset.integrity, + }; + }, +}); + +/** https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/aws-s3-data-integrity.js */ +new WebpackAssetsManifest({ + output: 'aws-s3-data-integrity-manifest.json', + integrity: true, + integrityHashes: ['md5'], + integrityPropertyName: 'md5', + publicPath: 's3://some-bucket/some-folder/', + customize(entry, original, manifest, asset) { + return { + key: entry.value, + value: asset && asset.md5.substr(4), + }; + }, +}); + +/** https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/custom-cdn.js */ +new WebpackAssetsManifest({ + output: 'custom-cdn-manifest.json', + publicPath(filename, manifest) { + switch (manifest.getExtension(filename).substr(1).toLowerCase()) { + case 'jpg': case 'jpeg': case 'gif': case 'png': case 'svg': + return `https://img.cdn.example.com/${filename}`; + case 'css': + return `https://css.cdn.example.com/${filename}`; + case 'js': + return `https://js.cdn.example.com/${filename}`; + default: + return `https://cdn.example.com/${filename}`; + } + }, +}); + +/** https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/customized.js https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/merged.js */ +new WebpackAssetsManifest({ + output: 'customized-manifest.json', + merge: true, + customize(entry, original, manifest, asset) { + if (manifest.isMerging) {} + + if (entry.key.toLowerCase().endsWith('.map')) { + return false; + } + + return { + key: `src/${entry.key}`, + value: `dist/${entry.value}`, + }; + }, +}); + +/** https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/sorted.js */ +new WebpackAssetsManifest({ + output: 'sorted-manifest.json', + sortManifest(a, b) { + const extA = this.getExtension(a); + const extB = this.getExtension(b); + + if (extA > extB) { + return 1; + } + + if (extA < extB) { + return -1; + } + + return a.localeCompare(b); + }, +}); + +/** https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/transformed.js */ +new WebpackAssetsManifest({ + output: 'transformed-manifest.json', + transform(assets, manifest) { + const { name, version } = require('./package.json'); + + assets.package = { + name, + version, + }; + + const { key, value } = manifest.hooks.customize.call({ + key: 'YourKey', + value: 'YourValue', + }); + + assets[key] = value; + }, +}); From 607ef432d53dace15c2389762c34e6fd31ac4d98 Mon Sep 17 00:00:00 2001 From: Franklin Tse Date: Tue, 28 Aug 2018 13:30:32 +0800 Subject: [PATCH 5/7] Require TypeScript >= 2.7 --- types/webpack-assets-manifest/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/webpack-assets-manifest/index.d.ts b/types/webpack-assets-manifest/index.d.ts index c428d36294..ae4327098e 100644 --- a/types/webpack-assets-manifest/index.d.ts +++ b/types/webpack-assets-manifest/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/webdeveric/webpack-assets-manifest // Definitions by: Franklin Tse // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 2.7 import { Plugin } from "webpack"; import { SyncHook, SyncWaterfallHook } from "tapable"; From 847e5609f86b54d2c52cbf586541c4605fa16ff3 Mon Sep 17 00:00:00 2001 From: Franklin Tse Date: Tue, 28 Aug 2018 14:33:30 +0800 Subject: [PATCH 6/7] Fix jsdoc-format --- types/webpack-assets-manifest/index.d.ts | 2 +- .../webpack-assets-manifest/webpack-assets-manifest-tests.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/types/webpack-assets-manifest/index.d.ts b/types/webpack-assets-manifest/index.d.ts index ae4327098e..3d54da7745 100644 --- a/types/webpack-assets-manifest/index.d.ts +++ b/types/webpack-assets-manifest/index.d.ts @@ -18,7 +18,7 @@ declare class WebpackAssetsManifest extends Plugin { * The `SyncWaterfallHook` class supports 3 type parameters only but this hook actually has 4 parameters. The type of 4th parameter is `AnyObject`. * * Refer to https://github.com/webdeveric/webpack-assets-manifest#hooks for details - * */ + */ customize: SyncWaterfallHook; transform: SyncWaterfallHook; diff --git a/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts b/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts index 70436291a7..551451a989 100644 --- a/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts +++ b/types/webpack-assets-manifest/webpack-assets-manifest-tests.ts @@ -45,7 +45,10 @@ new WebpackAssetsManifest({ }, }); -/** https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/customized.js https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/merged.js */ +/** + * https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/customized.js + * https://github.com/webdeveric/webpack-assets-manifest/blob/master/examples/merged.js + */ new WebpackAssetsManifest({ output: 'customized-manifest.json', merge: true, From 08cfb1646f49f854a2582f244115fada794a6683 Mon Sep 17 00:00:00 2001 From: Franklin Tse Date: Tue, 28 Aug 2018 14:37:22 +0800 Subject: [PATCH 7/7] Fix the remaining jsdoc-format issue --- types/webpack-assets-manifest/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/webpack-assets-manifest/index.d.ts b/types/webpack-assets-manifest/index.d.ts index 3d54da7745..ad6d00fab3 100644 --- a/types/webpack-assets-manifest/index.d.ts +++ b/types/webpack-assets-manifest/index.d.ts @@ -73,7 +73,7 @@ declare class WebpackAssetsManifest extends Plugin { * Get a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler) for the manifest * * @param raw - Use `setRaw` instead of `set` - * */ + */ getProxy(raw?: boolean): ProxyHandler; }