在线文档教程

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 - 禁用插件

选项名插件默认值
booleanstransform-minify-booleanstrue
builtInsminify-builtinstrue
consecutiveAddstransform-inline-consecutive-addstrue
deadcodeminify-dead-code-eliminationtrue
evaluateminify-constant-foldingtrue
flipComparisonsminify-flip-comparisonstrue
guardsminify-guarded-expressionstrue
infinityminify-infinitytrue
mangleminify-mangle-namestrue
memberExpressionstransform-member-expression-literalstrue
mergeVarstransform-merge-sibling-variablestrue
numericLiteralsminify-numeric-literalstrue
propertyLiteralstransform-property-literalstrue
regexpConstructorstransform-regexp-constructorstrue
removeConsoletransform-remove-consolefalse
removeDebuggertransform-remove-debuggerfalse
removeUndefinedtransform-remove-undefinedtrue
replaceminify-replacetrue
simplifyminify-simplifytrue
simplifyComparisonstransform-simplify-comparison-operatorstrue
typeConstructorsminify-type-constructorstrue
undefinedToVoidtransform-undefined-to-voidtrue

相同选项传递给多个插件

  • 当多个插件需要相同选项时,可以简单的声明在相同位置。这些选项会被传递到两个或更多的插件中。

选项名插件
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 } }]] }