使用教程(一)--- entry,devtool,output,resolve
使用教程(二)--- module.loaders
Webpack版本 3.10.0
plugins: [ new webpack.DefinePlugin({ 'process.env': require('../config/dev.env') }), new webpack.HotModuleReplacementPlugin(), new HtmlWebpackPlugin({ filename: 'index.html', template: 'index.html', inject: true }), ]
每一个插件实例是一个具备 apply 属性的 js 对象 (Function.prototype.apply
)。css
const webpack = require('webpack'); const configuration = require('./webpack.config.js'); let compiler = webpack(configuration); function HelloWorldPlugin (options) { // do something... } // this 指向了 compiler。 HelloWorldPlugin.prototype.apply = function (compiler) { // compiler.plugin(),可看做绑定事件,打包时会触发事件。 compiler.plugin('run', function() { console.log('hello world!') }); };
DefinePlugin:它的做用是定义全局常量,是常量。即在模块用它定义的全局常量,那么你就不能改变它的值啦。用法例子:html
//webpack.config.js Plugins: [ new webpack.DefinePlugin({ '_ABC_': false }) ] // 在某个要打包的js模块里 if (_ABC_){ console.log('_ABC_是 true,才看得见这输出'); }
配置属性 | 数据类型 | 做用 |
---|---|---|
multiStep | Boolean | 若是 true 插件将分两步创建。首先编译常更新的模块,而后编译剩余的普通资源。 |
fullBuildTimeout | Number | multiStep 为 true,启用两步之间的延迟。 |
requestTimeout | Number | 下载某些东西的超时时间 |
官方讲,这些 options 可能会被弃用
,就 new webpack.HotModuleReplacementPlugin()
就能够了。jquery
Plugins: [ $: 'jquery' ] // 不用导入 jquery ,均可用 $。 // 能够理解为在全局就 import $ from 'jquery' 或 const $ = require('jquery')
// 安装 npm install --save-dev html-webpack-plugin // 导入 const HtmlWebpackPlugin = require('html-webpack-plugin'); // 使用 new HtmlWebpackPlugin({ filename: 'index.html', template: 'index.html', inject: true ... // 其余配置,可看下面。 })
inject:'true' | 'head' | 'body' | 'false' 。webpack
minify: {...} | 'false' 。
{...}:经过 html-minifier 的选项对象,来减少打包后文件的大小。在生产环境通常用到。看看里面通常用到什么吧。git
removeAttributeQuotes:若是 true ,则尽量地去掉 html 里标签元素属性的引号,有些特定属性则不会。( 请注意,HTML规范建议始终使用引号。谨慎使用这选项
)github
<p id="abc" title="hello world"></P> 转换为: <p id=abc title="hello world"></p> // 可见有些特定的属性是不会去掉引号的。
chunksSortMode:'none' | 'auto' | 'dependency' |'manual' | {function} 。
做用:对注入 html 中的 js 模块进行排序。默认:'auto' (自动排序)web
plugins 就讲到这里了,以上都是一些常见经常使用的插件,喜欢的朋友能够点波赞,谢谢啦。npm