为客户端部署压缩node_modules [英] Compacting node_modules for client-side deployment

查看:579
本文介绍了为客户端部署压缩node_modules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.js在JavaScript中实现一个客户端应用程序。因为我将在许多机器上部署该软件,所以我想尽量减少我分发的软件包的大小。特别是,我想从node_modules中删除任何不必要的文件。



对于初学者来说,这意味着删除和修剪依赖关系树,其中 npm 可以为我做。但我还想删除所有 package.json 文件和(特别是)其他任何不需要部署的文件。在我使用的许多软件包中有大量测试,多个文件版本(缩小,浏览等)等等。我只需要运行应用程序实际使用的JavaScript文件。否则,我将分发几个100Kb的文件,这些文件实际上并未使用。



我知道 node-browserify ,但我的应用程序将运行在CommonJS环境中,而不是浏览器,所以我会就像保持模块分离并加载它们 require



我正在考虑编写一个Grunt插件,使用 required 来遍历依赖关系树,拉取出运行时需要的JavaScript文件并将它们写入树结构,以便使用 require 来加载它们(只需直接加载模块而不需要包上传.json )。但我想确保在我解决这个问题之前,没有人为我做过这件事。 FWIW: grunt-package-minifier 。由于我们正在开发跨浏览器扩展框架,因此我的用例有点不寻常。像 Browserify ,与标准的NodeJS部署不同,我们我们希望尽可能减少我们发行的大小,包括任何CommonJS模块。但与 Browserify 不同,我们支持CommonJS,因此我们可以保留模块结构而不是将所有内容连接成一个大文件。



本质上,我从 node_modules 中提取所有 package.json ,README,测试文件等,但保留基本的JavaScript文件在一个可以被CommonJS模块加载器使用的结构中。


I'm implementing a client-side application in JavaScript using Node.js. Because I will be deploying the software on many machines, I would like to minimize the size of the package I distribute. In particular, I would like to remove any unnecessary files from node_modules.

For starters this means deduping and pruning the dependency tree, which npm can do for me. But I'd also like to remove all the package.json files and (especially) any other files that are not needed for deployment. In many of the packages I am using there are tons of tests, multiple versions of files (minified, browserified, etc.) and the like. I just need the JavaScript files that are actually used by the running app. Otherwise I'll be distributing a few 100Kb of files that aren't actually used.

I know about node-browserify but my app will be running in a CommonJS environment, not a browser, so I'd like to keep the modules separate and load them with require.

I'm thinking about writing a Grunt plugin that walks the dependency tree using required, pulls out the JavaScript files needed at runtime and writes them to a tree structure so they can be loaded using require (just loading the modules directly without needing a package.json). But I'd like to make sure no one has done this for me before I tackle it.

解决方案

FWIW: grunt-package-minifier. My use case is a bit unusual since we are developing a cross-browser extension framework. Like Browserify and unlike standard NodeJS deployments we want to minimize the size of our distribution, including any CommonJS modules. But unlike Browserify we support CommonJS so we can keep the module structure rather than concatenating everything into one big file.

Essentially I strip all the package.json, README, test files, etc. from node_modules but keep the essential JavaScript files in a structure that can be used by a CommonJS module loader.

这篇关于为客户端部署压缩node_modules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆