简体   繁体   中英

Is it possible to compile nodejs project into single javascript file?

The difficulty I see is with all the require calls, and the dependency tree. Is there a way to iterate through a project, including dependencies where needed, and produce a single, fully contained javascript file?

I am hoping to convert some server side only libraries to client side apps.

Alternatively, is there another method to achieve this...

If there is a file lib/_third_party_main.js in the node source when you compile, it will run that on start. See src/node.js . You might be able to compile your sources with eg UglifyJS or Google Closure .

Edit: Also, you can require any modules you put in lib as if they were native modules. Example:

lib/_third_party_main.js

var foo = require('foo');

foo();

lib/foo.js

module.exports = function() {
  console.log('O hai');
}

Compile and run, and it will print O hai .

Edit: You might be able to use Ender.js , Browserify or a similar browser packaging tool to build a single file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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