From 1025e20d0328087a9c6802d70bf9814d328312a5 Mon Sep 17 00:00:00 2001 From: tkqubo Date: Tue, 22 Sep 2015 20:02:06 +0900 Subject: [PATCH] Add plugins --- webpack/webpack-tests.ts | 143 +++++++++++++++++++++++- webpack/webpack.d.ts | 230 ++++++++++++++++++++++++++++++++++----- 2 files changed, 343 insertions(+), 30 deletions(-) diff --git a/webpack/webpack-tests.ts b/webpack/webpack-tests.ts index a99905088..27a4a5385 100644 --- a/webpack/webpack-tests.ts +++ b/webpack/webpack-tests.ts @@ -1,7 +1,6 @@ /// -import webpack from 'webpack'; -//import webpack = require('webpack'); +import * as webpack from 'webpack'; var configuration: webpack.Configuration; var loader: webpack.Loader; @@ -247,3 +246,143 @@ configuration = { ] }; +// +// https://webpack.github.io/docs/list-of-plugins.html +// + +var plugin: webpack.Plugin; +var resourceRegExp: any; +var newResource: any; +var contextRegExp: any; +var newContentResource: any; +var newContentRecursive: any; +var newContentRegExp: any; +var requestRegExp: any; +var options: any; +var definitions: any; +var paths: any; +var preferEntry = true; +var context: any; +var request: any; +var types: any; +var banner: any; +var plugins: webpack.Plugin[]; + +plugin = new webpack.NormalModuleReplacementPlugin(resourceRegExp, newResource); +plugin = new webpack.ContextReplacementPlugin( + resourceRegExp, + newContentResource, + newContentRecursive, + newContentRegExp); +plugin = new webpack.ContextReplacementPlugin( + resourceRegExp, + newContentResource, + newContentRecursive); +plugin = new webpack.ContextReplacementPlugin( + resourceRegExp, + newContentResource); +plugin = new webpack.ContextReplacementPlugin(resourceRegExp); +plugin = new webpack.IgnorePlugin(requestRegExp); +plugin = new webpack.IgnorePlugin(requestRegExp, contextRegExp); + +plugin = new webpack.PrefetchPlugin(context, request); +plugin = new webpack.PrefetchPlugin(request); +plugin = new webpack.ResolverPlugin(plugins, types); +plugin = new webpack.ResolverPlugin(plugins); +plugin = new webpack.ResolverPlugin([ + new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"]) +], ["normal", "loader"]); +plugin = new webpack.ResolverPlugin([ + new webpack.ResolverPlugin.FileAppendPlugin(['/dist/compiled-moduled.js']) +]); +plugin = new webpack.BannerPlugin(banner, options); +plugin = new webpack.optimize.DedupePlugin(); +plugin = new webpack.optimize.LimitChunkCountPlugin(options); +plugin = new webpack.optimize.MinChunkSizePlugin(options); +plugin = new webpack.optimize.OccurenceOrderPlugin(preferEntry); +plugin = new webpack.optimize.UglifyJsPlugin(options); +plugin = new webpack.optimize.UglifyJsPlugin(); +plugin = new webpack.optimize.UglifyJsPlugin({ + compress: { + warnings: false + } +}); +plugin = new webpack.optimize.UglifyJsPlugin({ + mangle: { + except: ['$super', '$', 'exports', 'require'] + } +}); +plugin = new webpack.optimize.CommonsChunkPlugin(options); +plugin = new CommonsChunkPlugin({ + name: "commons", + // (the commons chunk name) + + filename: "commons.js", + // (the filename of the commons chunk) + + // minChunks: 3, + // (Modules must be shared between 3 entries) + + // chunks: ["pageA", "pageB"], + // (Only use these entries) +}); +plugin = new CommonsChunkPlugin({ + // names: ["app", "subPageA"] + // (choose the chunks, or omit for all chunks) + + children: true, + // (select all children of chosen chunks) + + // minChunks: 3, + // (3 children must share the module before it's moved) +}); +plugin = new CommonsChunkPlugin({ + // names: ["app", "subPageA"] + // (choose the chunks, or omit for all chunks) + + children: true, + // (use all children of the chunk) + + async: true, + // (create an async commons chunk) + + // minChunks: 3, + // (3 children must share the module before it's separated) +}); +plugin = new webpack.optimize.AggressiveMergingPlugin(options); +plugin = new webpack.dependencies.LabeledModulesPlugin(); +plugin = new webpack.DefinePlugin(definitions); +plugin = new webpack.DefinePlugin({ + VERSION: JSON.stringify("5fa3b9"), + BROWSER_SUPPORTS_HTML5: true, + TWO: "1+1", + "typeof window": JSON.stringify("object") +}); +plugin = new webpack.ProvidePlugin(definitions); +plugin = new webpack.ProvidePlugin({ + $: "jquery" +}); +plugin = new webpack.SourceMapDevToolPlugin({ + //// asset matching + //test: string | RegExp | Array, + //include: string | RegExp | Array, + //exclude: string | RegExp | Array, + // + //// file and reference + //filename: string, + //append: bool | string, + // + //// sources naming + //moduleFilenameTemplate: string, + //fallbackModuleFilenameTemplate: string, + // + //// quality/performance + //module: bool, + //columns: bool, + //lineToLine: bool | object +}); +plugin = new webpack.HotModuleReplacementPlugin(); +plugin = new webpack.ExtendedAPIPlugin(); +plugin = new webpack.NoErrorsPlugin(); +plugin = new webpack.WatchIgnorePlugin(paths); + diff --git a/webpack/webpack.d.ts b/webpack/webpack.d.ts index dc25e56f5..f2049bbc9 100644 --- a/webpack/webpack.d.ts +++ b/webpack/webpack.d.ts @@ -1,4 +1,4 @@ -// Type definitions for webpack +// Type definitions for webpack 1.12.2 // Project: https://github.com/webpack/webpack // Definitions by: Qubo // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -36,52 +36,226 @@ declare module "webpack" { } } - interface Plugin { - } - - interface ResolverPlugin extends Plugin { - } - - interface ResolverPluginStatic { - new(plugins: Plugin[], files: string[]): ResolverPlugin; - DirectoryDescriptionFilePlugin: DirectoryDescriptionFilePluginStatic; - } - - interface DirectoryDescriptionFilePlugin extends Plugin { - } - - interface DirectoryDescriptionFilePluginStatic { - new(file: string, files: string[]): DirectoryDescriptionFilePlugin; - } + interface Plugin { } interface Webpack { - ResolverPlugin: ResolverPluginStatic; + /** + * optimize namespace + */ optimize: Optimize; + /** + * dependencies namespace + */ + dependencies: Dependencies; + /** + * Replace resources that matches resourceRegExp with newResource. + * If newResource is relative, it is resolve relative to the previous resource. + * If newResource is a function, it is expected to overwrite the ‘request’ attribute of the supplied object. + */ + NormalModuleReplacementPlugin: NormalModuleReplacementPluginStatic; + /** + * Replaces the default resource, recursive flag or regExp generated by parsing with newContentResource, + * newContentRecursive resp. newContextRegExp if the resource (directory) matches resourceRegExp. + * If newContentResource is relative, it is resolve relative to the previous resource. + * If newContentResource is a function, it is expected to overwrite the ‘request’ attribute of the supplied object. + */ + ContextReplacementPlugin: ContextReplacementPluginStatic; + /** + * Don’t generate modules for requests matching the provided RegExp. + */ + IgnorePlugin: IgnorePluginStatic; + /** + * A request for a normal module, which is resolved and built even before a require to it occurs. + * This can boost performance. Try to profile the build first to determine clever prefetching points. + */ + PrefetchPlugin: PrefetchPluginStatic; + /** + * Apply a plugin (or array of plugins) to one or more resolvers (as specified in types). + */ + ResolverPlugin: ResolverPluginStatic; + /** + * Adds a banner to the top of each generated chunk. + */ + BannerPlugin: BannerPluginStatic; + /** + * Define free variables. Useful for having development builds with debug logging or adding global constants. + */ + DefinePlugin: DefinePluginStatic; + /** + * Automatically loaded modules. + * Module (value) is loaded when the identifier (key) is used as free variable in a module. + * The identifier is filled with the exports of the loaded module. + */ + ProvidePlugin: ProvidePluginStatic; + /** + * Adds SourceMaps for assets. + */ + SourceMapDevToolPlugin: SourceMapDevToolPluginStatic; + /** + * Enables Hot Module Replacement. (This requires records data if not in dev-server mode, recordsPath) + * Generates Hot Update Chunks of each chunk in the records. + * It also enables the API and makes __webpack_hash__ available in the bundle. + */ + HotModuleReplacementPlugin: HotModuleReplacementPluginStatic; + /** + * Adds useful free vars to the bundle. + */ + ExtendedAPIPlugin: ExtendedAPIPluginStatic; + /** + * When there are errors while compiling this plugin skips the emitting phase (and recording phase), + * so there are no assets emitted that include errors. The emitted flag in the stats is false for all assets. + */ + NoErrorsPlugin: NoErrorsPluginStatic; + /** + * Does not watch specified files matching provided paths or RegExps. + */ + WatchIgnorePlugin: WatchIgnorePluginStatic; } interface Optimize { - CommonsChunkPlugin: CommonsChunkPluginStatic; + /** + * Search for equal or similar files and deduplicate them in the output. + * This comes with some overhead for the entry chunk, but can reduce file size effectively. + * This is experimental and may crash, because of some missing implementations. (Report an issue) + */ + DedupePlugin: optimize.DedupePluginStatic; + /** + * Limit the chunk count to a defined value. Chunks are merged until it fits. + */ + LimitChunkCountPlugin: optimize.LimitChunkCountPluginStatic; + /** + * Merge small chunks that are lower than this min size (in chars). Size is approximated. + */ + MinChunkSizePlugin: optimize.MinChunkSizePluginStatic; + /** + * Assign the module and chunk ids by occurrence count. Ids that are used often get lower (shorter) ids. + * This make ids predictable, reduces to total file size and is recommended. + */ + OccurenceOrderPlugin: optimize.OccurenceOrderPluginStatic; + /** + * Minimize all JavaScript output of chunks. Loaders are switched into minimizing mode. + * You can pass an object containing UglifyJs options. + */ + UglifyJsPlugin: optimize.UglifyJsPluginStatic; + CommonsChunkPlugin: optimize.CommonsChunkPluginStatic; + /** + * A plugin for a more aggressive chunk merging strategy. + * Even similar chunks are merged if the total size is reduced enough. + * As an option modules that are not common in these chunks can be moved up the chunk tree to the parents. + */ + AggressiveMergingPlugin: optimize.AggressiveMergingPluginStatic; } - interface CommonsChunkPlugin { + interface Dependencies { + /** + * Support Labeled Modules. + */ + LabeledModulesPlugin: dependencies.LabeledModulesPluginStatic; } - interface CommonsChunkPluginStatic { - new(chunkName: string, filenames?: string|string[]): CommonsChunkPlugin; + interface DirectoryDescriptionFilePluginStatic { + new(file: string, files: string[]): Plugin; } - interface UglifyJsPlugin { - + interface NormalModuleReplacementPluginStatic { + new(resourceRegExp: any, newResource: any): Plugin; } - interface DedupePlugin { + interface ContextReplacementPluginStatic { + new(resourceRegExp: any, newContentResource?: any, newContentRecursive?: any, newContentRegExp?: any): Plugin + } + interface IgnorePluginStatic { + new(requestRegExp: any, contextRegExp?: any): Plugin; + } + + interface PrefetchPluginStatic { + new(context: any, request: any): Plugin; + new(request: any): Plugin; + } + + interface ResolverPluginStatic { + new(plugins: Plugin[], files?: string[]): Plugin; + DirectoryDescriptionFilePlugin: DirectoryDescriptionFilePluginStatic; + /** + * This plugin will append a path to the module directory to find a match, + * which can be useful if you have a module which has an incorrect “main” entry in its package.json/bower.json etc (e.g. "main": "Gruntfile.js"). + * You can use this plugin as a special case to load the correct file for this module. Example: + */ + FileAppendPlugin: FileAppendPluginStatic; + } + + interface FileAppendPluginStatic { + new(files: string[]): Plugin; + } + + interface BannerPluginStatic { + new(banner: any, options: any): Plugin; + } + + interface DefinePluginStatic { + new(definitions: any): Plugin; + } + + interface ProvidePluginStatic { + new(definitions: any): Plugin; + } + + interface SourceMapDevToolPluginStatic { + new(options: any): Plugin; + } + + interface HotModuleReplacementPluginStatic { + new(): Plugin; + } + + interface ExtendedAPIPluginStatic { + new(): Plugin; + } + + interface NoErrorsPluginStatic { + new(): Plugin; + } + + interface WatchIgnorePluginStatic { + new(paths: RegExp[]): Plugin; + } + + namespace optimize { + interface DedupePluginStatic { + new(): Plugin; + } + interface LimitChunkCountPluginStatic { + new(options: any): Plugin + } + interface MinChunkSizePluginStatic { + new(options: any): Plugin; + } + interface OccurenceOrderPluginStatic { + new(preferEntry: boolean): Plugin; + } + interface UglifyJsPluginStatic { + new(options?: any): Plugin; + } + interface CommonsChunkPluginStatic { + new(chunkName: string, filenames?: string|string[]): Plugin; + new(options?: any): Plugin; + } + interface AggressiveMergingPluginStatic { + new(options: any): Plugin; + } + } + + namespace dependencies { + interface LabeledModulesPluginStatic { + new(): Plugin; + } } } var webpack: webpack.Webpack; - export default webpack; - //export = webpack; + //export default webpack; + export = webpack; }