是否有一个工具可以在node_modules中的每个javascript文件中运行,并将其替换为缩小版本?
我意识到这是一个非常简单的任务,可以通过循环遍历所有.js文件并将它们传递给minifier来完成,但我的实现会很慢,并且希望有人可能有一种"更聪明"的方式.
我有一个类似的问题需要解决,与要部署在 Lambda AWS 层框架上的层的维度相关。就我而言,问题与输出层开始增加其维度这一事实有关,并且从 AWS 的限制和性能角度来看都存在问题(尝试考虑加载一层,例如 100MB)与 40MB 之一相比)。
无论如何,考虑到这一概述,我设法执行了三个步骤的过程,以便实现对 node_modules 文件夹的适当压缩,然后实现整体层大小。
使用modcleannode_modules根据预定义和自定义 glob 模式从目录中删除不必要的文件和文件夹。
使用node-prune脚本从目录中删除不必要的文件node_modules,例如markdown、typescript源文件等。
最后我们可以运行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
Run Code Online (Sandbox Code Playgroud)
请注意,第三步需要很长时间才能完成。
我还没有尝试过这个,但看起来这应该可行。你必须安装 webpack
首先你必须安装 webpack
$ npm install -g webpack
Run Code Online (Sandbox Code Playgroud)
然后你必须安装 uglify-js 插件
$ npm install uglifyjs-webpack-plugin
Run Code Online (Sandbox Code Playgroud)
--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,
}),
],
}
};
Run Code Online (Sandbox Code Playgroud)
然后在你的终端运行
$ webpack --config webpack.config.js
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1181 次 |
| 最近记录: |