简体   繁体   English

将NPM包连接成一个JS文件

[英]Concatenate NPM package into one JS file

I am trying to get Swig (the template language) working on Parse Cloud Code with Express. 我试图让Swig (模板语言)使用Express 解析Parse Cloud Code Parse Cloud Code is a Node/Express host that doesn't allow NPM. Parse Cloud Code是一个不允许NPM的Node / Express主机。 Ridiculous, I know. 可笑,我知道。 I can still load external files into code with requires statements however, so I think that there's hope I can get this working. 我仍然可以将外部文件加载到带有require语句的代码中,所以我认为我希望我可以使用它。

So my question is how do I get the whole entire Swig package into a single JS file that I can include from my Parse Express app like so: 所以我的问题是如何将整个Swig包整合到一个JS文件中,我可以从我的Parse Express应用程序中包含这样的文件:

var swig = require("./cloud/swig.js");

Worth noting that Parse breaks normal require statements so that the NPM package as-is doesn't work without modifying each and every single file in the node_modules folder to have cloud in its path (which is why my above path has cloud in it). 值得注意的是,Parse打破了正常的需要语句,因此如果不修改node_modules文件夹中的每个单独文件以在其路径中包含cloud ,则NPM包不能正常工作(这就是我上面的路径中有cloud的原因)。 Parse also chokes while uploading lots of small files. 在上传大量小文件时,Parse也会窒息。 Concatenation is a need on this platform. 在这个平台上需要连接。

I have tried playing with browserify for hours, but no combination of anything I do makes exposes the Swig object when I load the browserified file with the require statement. 我已经尝试使用browserify几个小时,但是当我使用require语句加载浏览器化文件时,没有任何组合使得暴露Swig对象。 I think it may be the right option since the Browserified file includes all the files from Swig, but it doesn't expose them externally. 我认为它可能是正确的选项,因为Browserified文件包含Swig中的所有文件,但它不会在外部公开它们。

My question is either can this be done in browserify, and if so, how? 我的问题是,这可以在browserify中完成,如果是,如何? Or is there another way to concatenate a NPM repo down to one file so it can be more easily included from this platform? 或者是否有另一种方法将NPM存储库连接到一个文件,以便可以更容易地从此平台中包含它?

Thanks so much. 非常感谢。

Browserify is not the right tool for the job. Browserify不适合这项工作。

As the name implies, browserify is intended to be used to generate files you want to execute in the browser. 顾名思义,browserify旨在用于生成要在浏览器中执行的文件。 It walks the require calls from an entrypoint (ie some JS file you pass to browserify) and bundles them in an object that maps their names to functions wrapping the modules. 它从入口点(即传递给browserify的某些JS文件)遍历require调用,并将它们捆绑在一个对象中,该对象将其名称映射到包装模块的函数。 It does not expect a require function to already exist and doesn't make any use of it. 它不期望已经存在require函数并且不会使用它。 It substitutes its own implementation of require that only does one thing: look up names from the bundle, execute the matching function and return its exports . 它替换了自己的require实现只做了一件事:从bundle中查找名称,执行匹配函数并返回其exports

You could theoretically require a browserify bundle, but it would just return an empty object (although it might mess with globals). 理论上你可以require一个browserify包,但它只会返回一个空对象(尽管它可能会混淆全局变量)。 And in all likelihood it might break because the bundled modules think they are being executed in a browser. 而且很可能它会破坏,因为捆绑的模块认为它们是在浏览器中执行的。 This won't do any good. 这没有任何好处。

The only sane option if you want to stick with the host, is to copy over the node_modules folder from your local project folder. 如果您想坚持使用主机,唯一合理的选择是从本地项目文件夹复制node_modules文件夹。 This may not work if your computer and the server are not 100% compatible (eg 32-bit vs 64-bit, Debian vs RedHat, OSX/Windows vs Linux) but this mostly depends on your exact dependencies (basically anything that is built with node-gyp can be a problem). 如果您的计算机和服务器不是100%兼容(例如32位与64位,Debian与RedHat,OSX / Windows与Linux),这可能不起作用,但这主要取决于您的确切依赖性(基本上是任何构建的node-gyp可能是个问题)。

Node.js uses the node_modules folder when looking up dependencies in require calls automagically. 在自动查找require调用中的依赖项时,Node.js使用node_modules文件夹。 If you can somehow get a node_modules folder with the right contents on the server, require("foo") will work as long as node_modules contains a module foo . 如果你能以某种方式获得一个在服务器上具有正确内容的node_modules文件夹,只要node_modules包含一个模块foorequire("foo")就会起作用。

Ultimately, you are trying to use npm modules in Parse Cloud code and currently it's not possible: 最终,您尝试在Parse Cloud代码中使用npm模块,目前无法实现:

https://parse.com/questions/using-npm-modules-in-cloud-code https://parse.com/questions/using-npm-modules-in-cloud-code

But if you are only trying to use Swig, then as a work-around, you can consider using underscore template instead. 但是,如果您只是尝试使用Swig,那么作为解决方法,您可以考虑使用下划线模板 Parse already includes underscore: Parse已包含下划线:

https://parse.com/docs/cloud_modules_guide#underscore https://parse.com/docs/cloud_modules_guide#underscore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM