minify
babel-preset-minify
安装
npm install --save-dev babel-preset-minify
使用
通过 .babelrc (推荐)
.babelrc
{
"presets": ["minify"]
}
或者添加选项 -
{
"presets": [["minify", {
"mangle": {
"exclude": ["MyCustomError"]
},
"unsafe": {
"typeConstructors": false
},
"keepFnName": true
}]]
}
通过 CLI
babel script.js --presets minify
通过 Node API
require("babel-core").transform("code", {
presets: ["minify"]
}
选项
包含以下两种选择:
- 1-1 插件映射
1-1 插件映射
false
- 禁用插件
选项名 | 插件 | 默认值 |
---|---|---|
booleans | transform-minify-booleans | true |
builtIns | minify-builtins | true |
consecutiveAdds | transform-inline-consecutive-adds | true |
deadcode | minify-dead-code-elimination | true |
evaluate | minify-constant-folding | true |
flipComparisons | minify-flip-comparisons | true |
guards | minify-guarded-expressions | true |
infinity | minify-infinity | true |
mangle | minify-mangle-names | true |
memberExpressions | transform-member-expression-literals | true |
mergeVars | transform-merge-sibling-variables | true |
numericLiterals | minify-numeric-literals | true |
propertyLiterals | transform-property-literals | true |
regexpConstructors | transform-regexp-constructors | true |
removeConsole | transform-remove-console | false |
removeDebugger | transform-remove-debugger | false |
removeUndefined | transform-remove-undefined | true |
replace | minify-replace | true |
simplify | minify-simplify | true |
simplifyComparisons | transform-simplify-comparison-operators | true |
typeConstructors | minify-type-constructors | true |
undefinedToVoid | transform-undefined-to-void | true |
相同选项传递给多个插件
- 当多个插件需要相同选项时,可以简单的声明在相同位置。这些选项会被传递到两个或更多的插件中。
选项名 | 插件 |
---|---|
keepFnName | 通过 mangle & deadcode |
keepClassName | 通过 mangle & deadcode |
tdz | 通过 builtIns, evaluate, deadcode, removeUndefined |
例如
{
"presets": [["minify", {
"evaluate": false,
"mangle": true
}]]
}
{
"presets": [["minify", {
"mangle": {
"exclude": ["ParserError", "NetworkError"]
}
}]]
}
{
"presets": [["minify", {
"keepFnName": true
}]]
}
// 等同于
{
"presets": [["minify", {
"mangle": {
"keepFnName": true
},
"deadcode": {
"keepFnName": true
}
}]]
}