自动缩小所有node_modules

时间:2017-12-01 20:25:30

标签: node.js minify

是否有一个工具可以在node_modules中的每个javascript文件中运行,并将其替换为缩小版本?

我意识到这是一个非常简单的任务,可以通过循环遍历所有.js文件并将它们传递给minifier来完成,但是我的实现会很慢并且希望有人可能采用“更聪明”的方式来完成它。

2 个答案:

答案 0 :(得分:0)

我还没有尝试过,但是似乎应该可以。您将必须安装webpack

首先,您必须安装webpack

$ npm install -g webpack

然后,您将必须安装uglify-js插件

$ npm install uglifyjs-webpack-plugin

-webpack.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  entry: './app.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        test: /\.js(\?.*)?$/i,
      }),
    ],
  }
};

然后在您的终端中运行

$ webpack --config webpack.config.js

答案 1 :(得分:0)

我有一个类似的问题要解决,与要在Lambda AWS Layer框架上部署的层的尺寸有关。 在我的案例中,问题与输出层开始增加其尺寸有关,并且从AWS的局限性和从性能的角度来看都存在问题(尝试考虑加载100MB的层)相比40MB之一)。

无论如何,鉴于概述,我设法执行了三个步骤,以便对node_modules文件夹进行压缩,然后对整个图层大小进行适当压缩。

  1. 使用modclean根据预定义和自定义的glob模式从node_modules目录中删除不必要的文件和文件夹。

  2. 使用node-prune脚本来修剪node_modules目录中不必要的文件,例如markdown,打字稿源文件等。

  3. 最后,我们可以运行minify-all一个函数,以最小化嵌套文件夹中的所有javascript和css文件。

以下用于实现适当压缩的bash脚本文件(在我的情况下,我有249MB node_modules文件夹,在脚本变为120MB之后):

npm install -g modclean
npm install -g minify-all
npm install -g node-prune

# The following will install the dependencies into the node_modules folder,
# starting from a package.json file.
npm install

# The following line will remove all the unnecessary files and folders
# with caution to all the dependencies.
modclean -n default:safe,default:caution -r

# The following will prune unnecessary files.
node-prune

# The following with minify all the javascript and css files.
minify-all

请注意,第三步需要很多时间才能完成。

相关问题