Search code examples
javascriptmoduleintegrationloadingcode-organization

Ways to integrate javascript modules into one file for deployment?


On development, I am using JS modules for a better organization of my code. For example, I have module1.js, module2.js and both modules are included in the main.js with LABjs

$LAB
.script("module1.js").wait()
.script("module2.js").wait();

However, I also use Google Closure Compiler for code optimization and it requires a integrated javascript file according to the documentation.

The simplest solution to this problem is to compile your functions together with the portion of your program that calls those functions.

So I want to integrate all the modules into one js file which could then compiled with Google Closure Compiler. LabJS runs on the client side and won't get things done on the server-side. So is there any tools to perform this job? Or is there other library supports server-side module integration?


Solution

  • I found RequireJS has offered an optimization tool which could

    Combines related scripts together into build layers and minifies them via UglifyJS (the default) or Closure Compiler (an option when using Java).

    In the Documentation, for a page with the following main.js which imports one.js, two.js and three.js:

    require(["one", "two", "three"], function (one, two, three) { });

    This optimization tool could compress them into one integrated file is

    node ../../r.js -o name=main out=main-built.js baseUrl=.