feat: add Watching, MultiCompiler, and MultiWatching types to webpack function return type

- add Compiler, Watching classes
 - add Plugin abstract class
 - change plugins to extend Plugin

BREAKING CHANGE:
 - change Plugin apply param type from Webpack to Compiler
 - remove plugin static types
 - remove Webpack and Optimize interfaces
This commit is contained in:
Benjamin Lim
2017-02-08 03:06:23 +08:00
parent b2f1625784
commit ad66bed9da
14 changed files with 504 additions and 626 deletions
@@ -1,34 +1,24 @@
import HtmlWebpackPlugin = require("html-webpack-plugin");
import { Configuration } from "webpack";
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
const a: Configuration = {
plugins: [
new HtmlWebpackPlugin()
]
};
new HtmlWebpackPlugin();
const b: Configuration = {
plugins: [
new HtmlWebpackPlugin({
title: "test"
})
]
};
const optionsArray: HtmlWebpackPlugin.Options[] = [
{
title: 'test',
},
{
minify: {
caseSensitive: true,
},
},
{
chunksSortMode: function compare(a, b) {
return 1;
},
},
{
arbitrary: 'data',
},
];
const minify: HtmlWebpackPlugin.MinifyConfig = {
caseSensitive: true
};
new HtmlWebpackPlugin({
minify
});
new HtmlWebpackPlugin({
chunksSortMode: function compare(a, b) {
return 1;
}
});
new HtmlWebpackPlugin({
arbitrary: "data"
});
const plugins: HtmlWebpackPlugin[] = optionsArray.map(options => new HtmlWebpackPlugin(options));
+72 -92
View File
@@ -3,106 +3,86 @@
// Definitions by: Simon Hartcher <https://github.com/deevus>, Benjamin Lim <https://github.com/bumbleblym>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { Plugin, Webpack } from "webpack";
import { Options } from "html-minifier";
import { Plugin } from 'webpack';
import { Options as HtmlMinifierOptions } from 'html-minifier';
export = HtmlWebpackPlugin;
declare class HtmlWebpackPlugin implements Plugin {
constructor(options?: HtmlWebpackPlugin.Config);
apply(thisArg: Webpack, ...args: any[]): void;
declare class HtmlWebpackPlugin extends Plugin {
constructor(options?: HtmlWebpackPlugin.Options);
}
declare namespace HtmlWebpackPlugin {
export type MinifyConfig = Options;
type MinifyOptions = HtmlMinifierOptions;
/**
* It is assumed that each [chunk] contains at least the properties "id"
* (containing the chunk id) and "parents" (array containing the ids of the
* parent chunks).
*/
export interface Chunk { // TODO: Import from webpack?
id: string;
parents: string[];
[propName: string]: any; // TODO: Narrow type
}
/**
* It is assumed that each [chunk] contains at least the properties "id"
* (containing the chunk id) and "parents" (array containing the ids of the
* parent chunks).
*
* @todo define in webpack
*/
interface Chunk {
id: string;
parents: string[];
[propName: string]: any;
}
export type ChunkComparator = (a: Chunk, b: Chunk) => number;
type ChunkComparator = (a: Chunk, b: Chunk) => number;
export interface Config {
/**
* The title to use for the generated HTML document.
*/
title?: string;
interface Options {
/** `true | false` if `true` (default) try to emit the file only if it was changed. */
cache?: boolean;
/**
* Allows to control how chunks should be sorted before they are included to the html.
* Allowed values: `'none' | 'auto' | 'dependency' | {function}` - default: `'auto'`
*/
chunksSortMode?: 'none' | 'auto' | 'dependency' | ChunkComparator;
/** Allows you to add only some chunks (e.g. only the unit-test chunk) */
chunks?: string[];
/** Allows you to skip some chunks (e.g. don't add the unit-test chunk) */
excludeChunks?: string[];
/** Adds the given favicon path to the output html. */
favicon?: string;
/**
* The file to write the HTML to.
* Defaults to index.html. You can specify a subdirectory here too (eg: `assets/admin.html`).
*/
filename?: string;
/**
* `true | false` if `true` then append a unique webpack compilation hash to all included scripts and css files.
* This is useful for cache busting.
*/
hash?: boolean;
/**
* `true | 'head' | 'body' | false`
* Inject all assets into the given template or templateContent.
* When passing true or 'body' all javascript resources will be placed at the bottom of the body element.
* 'head' will place the scripts in the head element.
*/
inject?: 'body' | 'head' | boolean;
/**
* `{...} | false` Pass a html-minifier options object to minify the output.
* https://github.com/kangax/html-minifier#options-quick-reference
*/
minify?: false | MinifyOptions;
/** `true | false` if `true` (default) errors details will be written into the html page. */
showErrors?: boolean;
/** Webpack require path to the template. Please see the docs for details. */
template?: string;
/** The title to use for the generated HTML document. */
title?: string;
/** `true | false` If `true` render the link tags as self-closing, XHTML compliant. Default is `false` */
xhtml?: boolean;
/**
* In addition to the options actually used by this plugin, you can use this hash to pass arbitrary data through
* to your template.
*/
[option: string]: any;
}
/**
* The file to write the HTML to. Defaults to index.html. You can specify a subdirectory here too (eg: `assets/admin.html`).
*/
filename?: string;
/**
* Webpack require path to the template. Please see the docs for details.
*/
template?: string;
/**
* `true | 'head' | 'body' | false`
*
* Inject all assets into the given template or templateContent - When passing true or 'body' all javascript resources will be placed at the bottom of the body element. 'head' will place the scripts in the head element.
*/
inject?: boolean | "head" | "body";
/**
* Adds the given favicon path to the output html.
*/
favicon?: string;
/**
* `{...} | false` Pass a html-minifier options object to minify the output.
*
* https://github.com/kangax/html-minifier#options-quick-reference
*/
minify?: MinifyConfig | false;
/**
* `true | false` if `true` then append a unique webpack compilation hash to all included scripts and css files. This is useful for cache busting.
*/
hash?: boolean;
/**
* `true | false` if `true` (default) try to emit the file only if it was changed.
*/
cache?: boolean;
/**
* `true | false` if `true` (default) errors details will be written into the html page.
*/
showErrors?: boolean;
/**
* Allows you to add only some chunks (e.g. only the unit-test chunk)
*/
chunks?: string[];
/**
* Allows to control how chunks should be sorted before they are included to the html. Allowed values: `'none' | 'auto' | 'dependency' | {function}` - default: `'auto'`
*/
chunksSortMode?: "none" | "auto" | "dependency" | ChunkComparator;
/**
* Allows you to skip some chunks (e.g. don't add the unit-test chunk)
*/
excludeChunks?: string[];
/**
* `true | false` If `true` render the link tags as self-closing, XHTML compliant. Default is `false`
*/
xhtml?: boolean;
/**
* In addition to the options actually used by this plugin, you can use
* this hash to pass arbitrary data through to your template.
*/
[option: string]: any;
}
/** @deprecated use MinifyOptions */
type MinifyConfig = MinifyOptions;
/** @deprecated use Options */
type Config = Options;
}