javascriptrequirejsrequirejs-optimizer

Can I combine RequireJs modules AND require.js itself into one js-bundle


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?


Solution

  • 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>