对于webpack的配置以及经常使用模块你们应该都比较熟悉,本文将说一说webpack的一些经常使用插件,以及用法。
1.内置插件css
名称 | 参数 | 说明 | 用法 |
---|---|---|---|
DefinePlugin | Object | 编译时配置的全局常量,开发模式和发布模式的构建容许不一样的行为很是有用 | DefinePlugin |
HotModuleReplacementPlugin | - | 热更新模块 | |
NoEmitOnErrorsPlugin | - | 打包出错时不把错误输出到文件 | |
NamedModulesPlugin | - | 显示模块相对路径 | |
ProvidePlugin | - | 自动加载模块 | ProvidePlugin |
PrefetchPlugin | context: 目录的绝对路径,request: 模块路径 | 预加载模块请求 |
2.其余插件html
名称 | 参数 | 说明 | 用法 |
---|---|---|---|
CopyWebpackPlugin | Array | 拷贝文件夹或文件到指定目录 | CopyWebpackPlugin |
HtmlWebpackPlugin | - | 在编译目录下生成html,并将打包后的js文件插入script标签中 | HtmlWebpackPlugin |
ExtractTextPlugin | - | 把打包文件中的文本提取到一个文件 | ExtractTextPlugin |
OptimizeCSSPlugin | - | 优化压缩css文件 | OptimizeCSSPlugin |
UglifyJsPlugin | - | 压缩JavaScript文件 | UglifyJsPlugin |
WebpackDevServer | 提供了一个简单的 web server,而且具备 live reloading(实时从新加载) 功能 | WebpackDevServer | |
WebpackHotMiddleware | 把 webpack 处理过的文件发送到一个 server | webpackHotMiddleware |
new webpack.DefinePlugin({ PRODUCTION: JSON.stringify(true), VERSION: JSON.stringify('5fa3b9'), BROWSER_SUPPORTS_HTML5: true, TWO: '1+1', 'typeof window': JSON.stringify('object'), 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) });
console.log('Running App version ' + VERSION); if(!BROWSER_SUPPORTS_HTML5) require('html5shiv');
自动加载模块,而没必要处处 import 或 require 。
任什么时候候,当 identifier 被看成未赋值的变量时,module 就会自动被加载,而且 identifier 会被这个 module 导出的内容所赋值。(或者被模块的 property 导出的内容所赋值,以支持命名导出(named export))。html5
new webpack.ProvidePlugin({ identifier: 'module1', // ... }); new webpack.ProvidePlugin({ identifier: ['module1', 'property1'], // ... });
对于 ES2015 模块的 default export,你必须指定模块的 default 属性。webpack
拷贝文件夹或文件到指定目录web
const CopyPlugin = require('copy-webpack-plugin'); module.exports = { plugins: [ new CopyPlugin([ { from: 'source', to: 'dest' }, { from: 'other', to: 'public', ignore: ['*.js'], flatten: false, //仅复制文件。用于文件夹和文件同名时 }, ]), ], };https://webpack.docschina.org/plugins/copy-webpack-plugin/#ignore
查看更多express
该插件将为你生成一个HTML文件,其中包括使用script标签中引入webpack打包js。
若是你有多个webpack入口点,他们都会在生成的HTML文件中的script标签内。
若是你有任何CSS assets在webpack的输出中(例如,利用MiniCssExtractPlugin提取 CSS),那么这些将被包含在HTML head中的<link>标签内。app
new HtmlWebpackPlugin({ title: 'Webpack App' //用于生成的HTML文档的标题 默认为Webpack App filename: 'index.html', //将HTML写入的文件 默认index.html template: 'index.html', //webpack模板的相对或绝对路径。默认src/index.ejs //template: path.resolve(__dirname, '../index.ejs'), inject: true, //true || 'head' || 'body' || false 打包后的js引入位置 body/head favicon: String, meta: Object, base: Object|String|false, showErrors: Boolean, //将它错误信息写入页面 }),
把打包文件中的文本提取到一个文件一般用于提取csswebpack-dev-server
//webpack4 module: { rules: [ { test: /.css$/, use: ExtractTextPlugin.extract({ fallback: "style-loader", use: "css-loader", publicPath: "/dist" }) } ] } plugins: [ new ExtractTextPlugin({ filename: "bundle.css", disable: false, allChunks: true }) ]
new OptimizeCssAssetsPlugin({ assetNameRegExp: /\.optimize\.css$/g, cssProcessor: require('cssnano'), cssProcessorPluginOptions: { preset: ['default', { discardComments: { removeAll: true } }], }, canPrint: true })
new UglifyJsPlugin({ uglifyOptions: { compress: { warnings: false } }, sourceMap: true, parallel: true }),
提供了一个简单的 web server,而且具备 live reloading(实时从新加载) 功能ide
module.exports = { devServer: { contentBase: './dist' } };
查看详细配置函数
const express = require('express'); const webpack = require('webpack'); const webpackDevMiddleware = require('webpack-dev-middleware'); const app = express(); const config = require('./webpack.config.js'); const compiler = webpack(config); // 告诉 express 使用 webpack-dev-middleware, // 以及将 webpack.config.js 配置文件做为基础配置 app.use(webpackDevMiddleware(compiler, { publicPath: config.output.publicPath })); // 将文件 serve 到 port 3000。 app.listen(3000, function () { console.log('Example app listening on port 3000!\n'); });
也能够配合devServer
const WebpackDevServer = require('webpack-dev-server') const compiler = webpack(webpack.conf) hotMiddleware = webpackHotMiddleware(compiler, { log: false, //path heartbeat: 2500 }) const server = new WebpackDevServer( compiler, { contentBase: path.join(__dirname, '../'), quiet: true, before (app, ctx) { app.use(hotMiddleware) ctx.middleware.waitUntilValid(() => { }) } } ) server.listen(3000)