Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there tools for compiling CommonJS modules into a single .js file?

Tags:

javascript

Are there any tools that can compile modules written with CommonJS/Node-like modules (require, exports, etc.) into a single .js file to be served to a browser?

like image 760
Nathan Wall Avatar asked Dec 11 '12 01:12

Nathan Wall


2 Answers

Sounds like you're looking for Browserify:

https://github.com/substack/node-browserify

"Make node-style require() work in the browser with a server-side build step, as if by magic!"

like image 89
jAndy Avatar answered Sep 21 '22 10:09

jAndy


Another quite similar utility named CommonJS Compiler and Grunt task respectively you can find at https://github.com/dsheiko/cjsc

It uses Esprima syntaxTree while parsing require() calls in the modules. It enclosures each module in a unique scope and does the caching in the very way nodejs does. It works fine with UMD (universal module definition) modules. What I like most, it adds during compiling very little of code - small require function body plus define-call wrapper per module.

like image 38
Dmitry Sheiko Avatar answered Sep 20 '22 10:09

Dmitry Sheiko