Compress dead_code选项似乎无法删除未使用的导入:
https://www.npmjs.com/package/uglify-es#compress-options
const UglifyJS = require('uglify-es')
const treeshake = (code) => {
var options = {
compress: {
dead_code: true,
},
output: {
beautify: false,
}
};
const minified = UglifyJS.minify(code, options)
console.log(minified)
}
var string = "import {celebrate} from 'celebrate'; import path from 'path'; function add(first, second) { return first + second; }";
treeshake(string)
以上输出:
{ code:
'import{celebrate}from"celebrate";import path from"path";function add(r,t){return r+t}' }
但是您可以看到导入语句仍然存在。