如何使用uglify和grunt索引所有的导入,并生成一个单一文件

4

我刚开始使用Grunt,希望它可以将所有文件组合并压缩。但是我的问题是,它只是组合和压缩,却没有删除导入语句。(我正在使用uglify和concat)

我想要的是:

// File.js
import something from './something.js';
something.someFunction("hello world");

并且

// something.js
export default {
   someFunction: function(msg){console.log(msg)}
}

to

// all.js
var something = {
  someFunction: function(msg){
     console.log(msg)
  }
}
something.someFunction("hello world");

压缩不是问题。

1个回答

0

如果你想将源代码合并成一个文件,可以使用rollup.js来帮助你。

  1. 下载Node.js以获取npm
  2. 更新npm:npm install -g npm
  3. npm install -g rollup
    • 检查:rollup -v

然后,运行下面的命令将按预期工作。

rollup --format es --input file.js -o all.js


你也可以通过 rollup.config.js 生成。

rollup -c

// rollup.config.js
const AUTHOR = ""
const OutputFileName = `bundle` // in your case is `all`.js
const banner = `// Copyright (c) ..., all right reserved.`
const footer = `// powered by ${AUTHOR}`

export default {
  input: './index.js', // in your case is `File.js`
  output: [
    {
      file: `./${OutputFileName}.js`,
      format: 'es', // amd, umd, iife, cjs, ...
      //  Option
      // banner,
      // footer
    },
    { // You can generate different formats at once.
      file: `./${OutputFileName}_cjs.js`,
      format: 'cjs',
      banner,
      footer,
    },
  ]
}

压缩代码

  • 获取uglifyjs: npm install uglify-js -g
  • uglifyjs all.js -m -c -o all.min.js

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接