webpack 插件[plugins]
loader 被用于转换某些类型的模块,而插件则可以用于执行范围更广的任务。插件的范围包括,从打包优化和压缩,一直到重新定义环境中的变量。插件接口功能极其强大,可以用来处理各种各样的任务。
想要使用一个插件,你只需要 require() 它,然后把它添加到 plugins 数组中。多数插件可以通过选项(option)自定义。你也可以在一个配置文件中因为不同目的而多次使用同一个插件,这时需要通过使用 new 操作符来创建它的一个实例。
清理dist文件夹
- 使用
npm
下载安装clean-webpack-plugin
插件
npm install clean-webpack-plugin --save-dev
- 在webpack.config.js 中配置插件
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const path = require('path');
module.exports = {
entry: './path/to/my/entry/file.js',
output: {
/**
* With zero configuration,
* clean-webpack-plugin will remove files inside the directory below
* clean-webpack-plugin将删除下面目录中的文件
*/
path: path.resolve(process.cwd(), 'dist'),
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
},
],
},
plugins: [
new CleanWebpackPlugin(),
],
};
clean-webpack-plugin 插件包:https://www.npmjs.com/package/clean-webpack-plugin
打包 .html 文件
- 安装下载
html-webpack-plugin
插件
npm install --save-dev html-webpack-plugin
- 在webpack.config.js 中配置插件
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const path = require('path');
module.exports = {
entry: './path/to/my/entry/file.js',
output: {
/**
* With zero configuration,
* clean-webpack-plugin will remove files inside the directory below
* clean-webpack-plugin将删除下面目录中的文件
*/
path: path.resolve(process.cwd(), 'dist'),
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
},
],
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'My App', // 标题
filename: 'index.html', // 输出文件名
}),
],
};
这是一个webpack插件,它简化了HTML文件的创建,以服务于你的webpack包。这对于在文件名中包含散列的webpack包特别有用,因为文件名会改变每次编译。您可以让插件为您生成一个HTML文件,或者使用lodash模板提供您自己的模板,或者使用您自己的加载器。
如果你想要了解 HtmlWebpackPlugin 插件提供的全部的功能和选项,你就应该阅读 HtmlWebpackPlugin 仓库中的源码:https://github.com/jantimon/html-webpack-plugin。
*使用自定义html模板
如果默认生成的HTML不能满足您的需要,您可以提供自己的模板。最简单的方法是使用模板选项并传递一个定制的HTML文件。html-webpack-plugin会自动将所有必需的CSS, JS, manifest和favicon文件注入到标记中。
这里记录了其他模板加载器的详细信息。
使用template属性设置模板路径:template 属性介绍
webpack.config.js
plugins: [
new HtmlWebpackPlugin({
title: 'Custom template',
// Load a custom template (lodash by default)
template: 'index.html'
})
]
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
</body>
</html>
*生成多个HTML文件
要生成多个HTML文件,请在插件数组中多次声明插件
{
entry: 'index.js',
output: {
path: __dirname + '/dist',
filename: 'index_bundle.js'
},
plugins: [
new HtmlWebpackPlugin(), // 生成默认的 index.html
new HtmlWebpackPlugin({ // 依据路径下的test.html文件再生成一个 test.html
filename: 'test.html',
template: 'src/assets/test.html'
})
]
}
打包静态资源
- 安装下载
copy-webpack-plugin
插件
npm install --save-dev copy-webpack-plugin
- 在webpack.config.js 中配置插件
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
module.exports = {
entry: './path/to/my/entry/file.js',
output: {
/**
* With zero configuration,
* clean-webpack-plugin will remove files inside the directory below
* clean-webpack-plugin将删除下面目录中的文件
*/
path: path.resolve(process.cwd(), 'dist'),
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
},
],
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'My App', // 标题
filename: 'index.html', // 输出文件名
}),
new CopyWebpackPlugin({
patterns: [
{ from: "source", to: "dest" }, // 'from' 我们сopy文件所在的Glob或路径
{ from: "other", to: "public" }, // 'to' 打包输出路径
],
}),
],
};
copy-webpack-plugin 插件详细介绍:https://www.npmjs.com/package/copy-webpack-plugin#globoptions
*编写自定义插件
使用 webpack API 中的 compiler 钩子,可以在特定的时期进行操作
详细案例参考链接:webpack 编写自定义插件
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
const RemoveCommentsPlugin = require('./remove-comments-plugin') // 自定义插件
module.exports = {
entry: './path/to/my/entry/file.js',
output: {
/**
* With zero configuration,
* clean-webpack-plugin will remove files inside the directory below
* clean-webpack-plugin将删除下面目录中的文件
*/
path: path.resolve(process.cwd(), 'dist'),
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
},
],
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'My App', // 标题
filename: 'index.html', // 输出文件名
}),
new CopyWebpackPlugin({
patterns: [
{ from: "source", to: "dest" }, // 'from' 我们сopy文件所在的Glob或路径
{ from: "other", to: "public" }, // 'to' 打包输出路径
],
}),
new RemoveCommentsPlugin() // 自定义插件
],
};