一个用于 babel-minify 的 webpack 插件 - 基于 babel 的 minifier
npm install babel-minify-webpack-plugin --save-dev
// webpack.config.js
const MinifyPlugin = require("babel-minify-webpack-plugin");
module.exports = {
entry: //...,
output: //...,
plugins: [
new MinifyPlugin(minifyOpts, pluginOpts)
]
}
minifyOpts
会被传递给 babel-preset-minify。你可以在 babel 仓库的 packages 目录中找到 所有可用选项。
Default: {}
test
: 匹配特定类型的文件。默认值: /\.js($|\?)/i
include
: include
的文件。 默认值: undefined
exclude
: exclude
的文件。 默认值: undefined
comments
: 保留注释。 默认值: /^\**!|@preserve|@license|@cc_on/
, falsy
值将移除所有注释。可以接受函数,带有测试属性的(正则)的对象和值。sourceMap
: 配置 sourcemap。 默认值: webpackConfig.devtoolparserOpts
: 配置具有特殊解析器选项的 babel。babel
: 可传入一个自定义的 babel-core
以替换默认的。 默认值: require("babel-core")
minifyPreset
: 可传入一个自定义的 babel-minify
预设以替换默认的。 默认值: require("babel-preset-minify")
你也可以在 webpack 中使用 babel-loader,然后再引入 minify
作为一个 preset,并且会比直接使用此插件执行得更快。— 这是因为 babel-minify
处理的文件体积会更小。既然如此,那这个插件存在的意义是什么呢?
mangle: { topLevel: true }
。node_modules
不通过 babel-loader 运行时,babel-minify 的优化便不会应用于被排除的文件,因为它们不需要通过 minifier。 Boopathi Rajaa | Juho Vepsäläinen | Joshua Wiens | Kees Kluskens | Sean Larkin |