Combining NPM package into a single JS file

I am trying to get Swig (template language) working on Code Cloud Code using Express. Parse Cloud Code is a Node / Express host that does not allow NPM. Funny I know. However, I can load external files into the code with the required statements, so I think I hope I can do this.

So my question is: how to get the entire Swig package into one JS file, which can be included from my Parse Express application, for example:

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

It is worth noting that Parse violates the normal requirements, so the NPM as-is package does not work without changing each individual file in the node_modules folder to have cloud in its path (which is why my path above has cloud in it). Parse also suffocates when downloading a large number of small files. Concatenation is a must on this platform.

I tried playing with the browser for several hours, but no combination of anything I do provides a Swig object when I load the browser file with the request. I think this may be the right option, since the Browserified file includes all the files from Swig, but it does not expose them from the outside.

My question is, can this be done in a browser, and if so, how? Or is there another way to combine the NPM repository into one file so that it can be easily included from this platform?

Thank you very much.

+6
source share
2 answers

Browserify is not suitable for work.

As the name implies, the browser needs to be used to generate the files that you want to execute in the browser. It makes the required calls from the entry point (i.e., some JS file that you pass to the browser) and binds them in an object that maps their names to functions that wrap the modules. He does not expect that the require function already exists and does not use it. It replaces its own implementation of require , which only does one thing: look at the names from the package, execute the corresponding function, and return its exports .

You could theoretically require talk about the package, but it will just return an empty object (although it can ruin global variables). And, in all likelihood, this can break down because the related modules believe that they are executed in the browser. It will not do any good.

The only normal option, if you want to stick with the host, is to copy the node_modules folder from your local project folder. This may not work if your computer and server are not 100% compatible (e.g. 32-bit with 64-bit, Debian vs RedHat, OSX / Windows and Linux), but it mostly depends on your exact dependencies (mostly everything built with node-gyp can be a problem).

Node.js uses the node_modules folder when automatically looking for dependencies in require . If you can somehow get the node_modules folder with the correct contents on the server, require("foo") will work as long as node_modules contains the foo module.

0
source

Ultimately, you are trying to use npm modules in Parse Cloud code, and this is currently not possible:

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

But if you are only trying to use Swig, while a workaround, you can use the underline pattern instead. The parameter already contains an underscore:

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

0
source

All Articles