diff --git a/types/webpack-merge/index.d.ts b/types/webpack-merge/index.d.ts index a939a2ed53..421737ccaf 100644 --- a/types/webpack-merge/index.d.ts +++ b/types/webpack-merge/index.d.ts @@ -1,25 +1,34 @@ -// Type definitions for webpack-merge +// Type definitions for webpack-merge 4.1 // Project: https://github.com/survivejs/webpack-merge -// Definitions by: Simon Hartcher +// Definitions by: Simon Hartcher , Matt Traynham // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/// +import webpack = require('webpack'); -declare module "webpack-merge" { - import { Configuration } from "webpack"; +export = webpackMerge; + +declare const webpackMerge: webpackMerge.WebpackMerge; + +declare namespace webpackMerge { + type CustomizeArrayFunction = (a: any[], b: any[], key: string) => any[] | null | undefined; + type CustomizeObjectFunction = (a: {}, b: {}, key: string) => {} | null | undefined; + type UniqueFunction = (field: string, fields: string[], keyFn: (field: any) => string) => CustomizeArrayFunction; + interface CustomizeOptions { + customizeArray?: CustomizeArrayFunction | UniqueFunction; + customizeObject?: CustomizeObjectFunction; + } + type ConfigurationMergeFunction = (...configs: webpack.Configuration[]) => webpack.Configuration; + type ConfigurationMergeConfigFunction = (customizeOptions: CustomizeOptions) => ConfigurationMergeFunction; + type MergeFunction = ConfigurationMergeFunction | ConfigurationMergeConfigFunction; + type MergeStrategy = 'prepend' | 'append' | 'replace'; interface WebpackMerge { - /** - * Merge multiple webpack configurations into one. - */ - (...configs: Configuration[]): Configuration; - - /** - * Merge multiple webpack configurations into one, with smart merging of loaders. - */ - smart(...configs: Configuration[]): Configuration; + (...configs: webpack.Configuration[]): webpack.Configuration; + (customizeOptions: CustomizeOptions): ConfigurationMergeFunction; + unique: UniqueFunction; + smart: ConfigurationMergeFunction; + multiple: ConfigurationMergeFunction; + strategy(options: {[field: string]: MergeStrategy}): ConfigurationMergeFunction; + smartStrategy(options: {[key: string]: MergeStrategy}): ConfigurationMergeFunction; } - - const merge: WebpackMerge; - export = merge; } diff --git a/types/webpack-merge/tsconfig.json b/types/webpack-merge/tsconfig.json index f2368dc89d..78589b444b 100644 --- a/types/webpack-merge/tsconfig.json +++ b/types/webpack-merge/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -19,4 +19,4 @@ "index.d.ts", "webpack-merge-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/webpack-merge/tslint.json b/types/webpack-merge/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/webpack-merge/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/webpack-merge/v0/index.d.ts b/types/webpack-merge/v0/index.d.ts new file mode 100644 index 0000000000..a939a2ed53 --- /dev/null +++ b/types/webpack-merge/v0/index.d.ts @@ -0,0 +1,25 @@ +// Type definitions for webpack-merge +// Project: https://github.com/survivejs/webpack-merge +// Definitions by: Simon Hartcher +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module "webpack-merge" { + import { Configuration } from "webpack"; + + interface WebpackMerge { + /** + * Merge multiple webpack configurations into one. + */ + (...configs: Configuration[]): Configuration; + + /** + * Merge multiple webpack configurations into one, with smart merging of loaders. + */ + smart(...configs: Configuration[]): Configuration; + } + + const merge: WebpackMerge; + export = merge; +} diff --git a/types/webpack-merge/v0/tsconfig.json b/types/webpack-merge/v0/tsconfig.json new file mode 100644 index 0000000000..aedaa9a44e --- /dev/null +++ b/types/webpack-merge/v0/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "webpack-merge": ["webpack-merge/v0"], + "webpack-merge/*": ["webpack-merge/v0/*"] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "webpack-merge-tests.ts" + ] +} \ No newline at end of file diff --git a/types/webpack-merge/v0/webpack-merge-tests.ts b/types/webpack-merge/v0/webpack-merge-tests.ts new file mode 100644 index 0000000000..4c9c4adca4 --- /dev/null +++ b/types/webpack-merge/v0/webpack-merge-tests.ts @@ -0,0 +1,12 @@ +import merge = require("webpack-merge"); +import { Configuration } from "webpack"; + +const a: Configuration = { + entry: "test.js" +} +const b: Configuration = { + devtool: "source-map" +} + +const c = merge(a, b); +const d = merge.smart(a, b); diff --git a/types/webpack-merge/webpack-merge-tests.ts b/types/webpack-merge/webpack-merge-tests.ts index 4c9c4adca4..d7180a6950 100644 --- a/types/webpack-merge/webpack-merge-tests.ts +++ b/types/webpack-merge/webpack-merge-tests.ts @@ -1,12 +1,61 @@ -import merge = require("webpack-merge"); -import { Configuration } from "webpack"; +import _ = require('lodash'); +import { Configuration, HotModuleReplacementPlugin, Plugin } from 'webpack'; +import webpackMerge = require('webpack-merge'); const a: Configuration = { - entry: "test.js" -} + entry: 'test.js' +}; const b: Configuration = { - devtool: "source-map" -} + devtool: 'source-map' +}; -const c = merge(a, b); -const d = merge.smart(a, b); +const c: Configuration = webpackMerge(a, b); +const d: Configuration = webpackMerge.smart(a, b); +const e: Configuration = webpackMerge.multiple(a, b); +const f: Configuration = webpackMerge( + { + customizeArray(x: any[], y: any[], key: string): any[] | undefined { + if (key === 'extensions') { + return _.uniq([...x, ...y]); + } + // Fall back to default merging + return undefined; + }, + customizeObject(x: {}, y: {}, key: string): {} | undefined { + if (key === 'module') { + // Custom merging + return _.merge({}, x, y); + } + + // Fall back to default merging + return undefined; + } + } +)(a, b); +const g: Configuration = webpackMerge({ + customizeArray: webpackMerge.unique( + 'plugins', + ['HotModuleReplacementPlugin'], + (plugin: Plugin) => plugin.constructor && plugin.constructor.name + ) +})({ + plugins: [ + new HotModuleReplacementPlugin() + ] +}, { + plugins: [ + new HotModuleReplacementPlugin() + ] +}); +const h: Configuration = webpackMerge.strategy( + { + entry: 'prepend', // or 'replace', defaults to 'append' + 'module.loaders': 'prepend' + } +)(a, b); +const i: Configuration = webpackMerge.smartStrategy( + { + entry: 'prepend', // or 'replace' + 'module.loaders': 'prepend' + } +)(a, b);