使用uglify-es删除未使用和无效的代码

时间:2019-05-12 15:05:38

标签: uglifyjs

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}' }

但是您可以看到导入语句仍然存在。

0 个答案:

没有答案