javascript 如何将多文件 node.js 应用程序转换为单个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5655975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert a multi-file node.js app to a single file?
提问by ryeguy
If I have a node.js application that is filled with many require
statements, how can I compile this into a single .js
file? I'd have to manually resolve the require
statements and ensure that the classes are loaded in the correct order. Is there some tool that does this?
如果我有一个充满许多require
语句的 node.js 应用程序,我如何将它编译成一个.js
文件?我必须手动解析require
语句并确保以正确的顺序加载类。有没有什么工具可以做到这一点?
Let me clarify.
让我澄清一下。
The code that is being run on node.js is not node specific. The onlything I'm doing that doesn't have a direct browser equivalent is using require
, which is why I'm asking. It is not using any of the node libraries.
在 node.js 上运行的代码不是特定于节点的。我正在做的唯一一件事没有直接的浏览器等效项是 using require
,这就是我问的原因。它没有使用任何节点库。
回答by vvo
You can use webpack with target: 'node', it will inline all required modules and export everything as a single, standalone, one file, nodejs module
您可以将 webpack 与 target: 'node' 一起使用,它将内联所有必需的模块并将所有内容导出为一个单独的、独立的、一个文件的 nodejs 模块
回答by Raynos
If you want to send common code to the browser I would personally recommend something like brequireor requireJSwhich can "compile" your nodeJS source into asynchronously loading code whilst maintaining the order.
如果您想将公共代码发送到浏览器,我个人会推荐像brequire或requireJS这样的东西,它们可以将您的 nodeJS 源“编译”为异步加载代码,同时保持顺序。
For an actual compiler into a single file you might get away with one for requireJS but I would not trust it with large projects with high complexity and edge-cases.
对于将实际编译器转换为单个文件的情况,您可能会为 requireJS 使用一个文件,但对于具有高复杂性和边缘情况的大型项目,我不相信它。
It shouldn't be too hard to write a file like package.json that npm uses to statein which order the files should occur in your packaging. This way it's your responsibility to make sure everything is compacted in the correct order, you can then write a simplistic node application to reads your package.json file and uses file IO to create your compiled script.
编写像 package.json 这样的文件应该不会太难,npm 用它来说明文件在你的包中应该以什么顺序出现。通过这种方式,您有责任确保以正确的顺序压缩所有内容,然后您可以编写一个简单的节点应用程序来读取您的 package.json 文件并使用文件 IO 来创建您的编译脚本。
Automatically generating the order in which files should be packaged requires building up a dependency tree and doing lots of file parsing. It should be possible but it will probably crash on circular dependencies. I don't know of any libraries out there to do this for you.
自动生成文件的打包顺序需要构建依赖树并进行大量文件解析。这应该是可能的,但它可能会因循环依赖而崩溃。我不知道有任何图书馆可以为你做这件事。
回答by Dave Dopson
Do NOTuse requireJS if you value your sanity. I've seen it used in a largish project and it was an absolute disaster ... maybe the worst technical choice made at that company. RequireJS is designed to run in-browser and to asynchronously and recursively load JS dependencies. That is a TERRIBLE idea. Browsers suck at loading lots and lots of little files over the network; everysingledocon web performance will tell you this. So you'll very very quickly end up needing a solution to smash your JS files together ... at which point, what's the point of having an in-browser dependency resolution mechanism? And even though your production site will be smashed into a single JS file, with requireJS, your code must constantly assume that any dependency might or might not be loaded yet; in a complex project, this leads to thousands of async load barriers wrapping every interaction point between modules. At my last company, we had some places where the closure stack was 12+ levels deep. All that "if loaded yet" logic makes your code more complex and harder to work with. It also bloats the code increasing the number of bytes sent to the client. Plus, the client has to load the requireJS library itself, which burns another 14.4k. The size alone should tell you something about the level of feature creep in the requireJS project. For comparison, the entire underscore.jstoolkit is only 4k.
千万不要,如果你珍惜你的理智使用requireJS。我见过它在一个大型项目中使用,这绝对是一场灾难......也许是那家公司做出的最糟糕的技术选择。RequireJS 旨在在浏览器中运行并异步和递归地加载 JS 依赖项。这是一个可怕的想法。浏览器很难通过网络加载大量的小文件;每一个文档网络性能会告诉你这一点。所以你很快就会需要一个解决方案来将你的 JS 文件粉碎在一起......在这一点上,拥有浏览器内的依赖解析机制有什么意义?即使您的生产站点将被粉碎为单个 JS 文件,但使用 requireJS,您的代码必须不断假设任何依赖项可能尚未加载;在一个复杂的项目中,这会导致数以千计的异步负载屏障包裹着模块之间的每个交互点。在我上一家公司,我们有一些地方的闭包堆栈深度超过 12 层。所有“如果尚未加载”的逻辑都会使您的代码更加复杂且难以使用。它还会使代码膨胀,从而增加发送到客户端的字节数。另外,客户端必须自己加载 requireJS 库,再烧掉 14.4k。仅大小就可以告诉您有关 requireJS 项目中功能蠕变程度的一些信息。为了比较,整个underscore.js工具包只有 4k。
What you want is a compile-time step for smashing JS together, not a heavyweight framework that will run in the browser....
你想要的是一个将 JS 粉碎在一起的编译时步骤,而不是一个将在浏览器中运行的重量级框架......
You should check out https://github.com/substack/node-browserify
您应该查看https://github.com/substack/node-browserify
Browserify does exactly what you are asking for .... combines multiple NPM modules into a single JS file for distribution to the browser. The consolidated code is functionally identical to the original code, and the overhead is low (approx 4k + 140 bytes per additional file, including the "require('file')" line). If you are picky, you can cut out most of that 4k, which provides wrappers to emulate common node.js globals in the browser (eg "process.nextTick()").
Browserify 完全符合您的要求……将多个 NPM 模块组合到一个 JS 文件中,以便分发到浏览器。合并后的代码在功能上与原始代码相同,并且开销很低(每个附加文件大约 4k + 140 字节,包括“require('file')”行)。如果你很挑剔,你可以去掉大部分 4k,它提供了包装器来模拟浏览器中常见的 node.js 全局变量(例如“process.nextTick()”)。