4

I'm using RequireJs Optimizer to combine all modules into one file - app.js

On production I have two js files: app.js and require.js

Production HTML has following script tag:

<script data-main="app.js" src="require.js"></script>

This will result in two requests for js files when running.

The question: is it possible to combine everything including all modules and require.js itself into one file, so I have only one request when running?

artin
  • 1,764
  • 1
  • 17
  • 23
  • possible duplicate of [Way to bundle require.js source with main.js](http://stackoverflow.com/questions/19625581/way-to-bundle-require-js-source-with-main-js) – Louis Feb 05 '15 at 11:54

2 Answers2

4

The answer is here http://requirejs.org/docs/optimization.html#onejs

I had to include require.js through 'include' r.js option.

With gulp task and gulp-requirejs it looks like this:

requirejs({
   paths: {
      requireLib: 'lib/require'
   },
   include: ['requireLib'],
   out: 'bundle.js'
 })
.pipe(insert.append('require(["app"]);'))

Notice I had to append require(["app"]) to the end of js-bundle to start main module.

Now I can reference only 'bundle.js' on the page and have one request.

<script type="text/javascript" src="bundle.js"></script>
artin
  • 1,764
  • 1
  • 17
  • 23
2

AlmondJS sounds like a good fit, snippets from their documentation:

A replacement AMD loader for RequireJS. It provides a minimal AMD API footprint that includes loader plugin support. Only useful for built/bundled AMD modules, does not do dynamic loading.

...

By including almond in the built file, there is no need for RequireJS. almond is around 1 kilobyte when minified with Closure Compiler and gzipped.

For example

node r.js -o baseUrl=. name=path/to/almond include=main out=main-built.js wrap=true

Will output something like this:

(function () {
    //almond will be here
    //main and its nested dependencies will be here
}());

Admittedly I've not used it, but I've heard of it.

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
  • 1
    Thanks for the advice, but I've fould out that it is doable with RequireJs Optimizer by just adding require.js lib through 'include' option. I took some time reading AlmondJS docs, and it looks pretty sweet. I've decided to get back to it later. – artin Feb 06 '15 at 08:36