如何uglify文件并保存到另一个位置(vue.js)

时间:2018-04-12 00:06:57

标签: node.js webpack vue.js vuejs2 webpack-2

我不确定如何uglify我的server.js文件并将其保存到dist文件夹下的server文件夹中。现在我只使用CopyWebpackPlugin

new CopyWebpackPlugin([
  {
    from: path.resolve(__dirname, '../static'),
    to: config.build.assetsSubDirectory,
    ignore: ['.*']
  },
  {
    from: path.resolve(__dirname, '../src/server'),
    to: config.build.assetsServerDirectory,
    ignore: ['*.sql']
  }
]),

这很有效,但只是简单的复制和粘贴。

2 个答案:

答案 0 :(得分:4)

您可以使用uglify-es + copy-webpack-plugin's transform()

  • 安装包:

    npm install uglify-es --save-dev
    
  • 将其添加到您的来源:

    const UglifyJS = require("uglify-es");                  // add the require
    
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../static'),
        to: config.build.assetsSubDirectory,
        ignore: ['.*']
      },
      {
        from: path.resolve(__dirname, '../src/server'),
        to: config.build.assetsServerDirectory,
        transform: function (content, path) {               // add transform()
          return UglifyJS.minify(content.toString()).code;  // use uglify
        },                                                  // (no args = mangle+compress)
        ignore: ['*.sql']
      }
    ]),
    

注意:UglifyJS.minify(content.toString()).codeUglifyJS.minify(content.toString('utf8')).code相同。 There are options in case of different encodings

答案 1 :(得分:0)

有一个图书馆已经准备好了。您可以使用 uglifyjs-webpack-plugin

然后将捆绑的文件输出到所需的目录

如果您不喜欢使用该插件。您可以使用Webpack的默认uglifier。

const webpack = require("webpack");

    module.exports = {
      entry: {
        "bundle": "./server/server.js",
      }
      output: {
        path: "./dist",
        filename: "[name].js"
      },
      plugins: [
        new webpack.optimize.UglifyJsPlugin({
          include: /\.js$/,
          minimize: true
        })
      ]
    };