12

I'm trying to build my require.js modules to one javascript file for production.

The command I'm running is...

r.js -o name=main out=main.min.js mainConfigFile=main.js

This compiles but the compiled main.min.js file is not compiled correctly and still includes the "define" statement blocks. and the browser obviously returns

Uncaught ReferenceError: define is not defined 

My main.js file looks like:

require.config({
    paths: {
        jquery: 'libs/jquery/jquery',
    },
    shim: {
        bootstrap: {
            deps: ['jquery'],
            exports: 'jquery'
        }
    }
});
require(['app', 'jquery'], function (app, $) {
    'use strict';
    // use app here
    console.log(app);
    console.log('Running jQuery %s', $().jquery);
});

Please let me know what I'm overlooking here. Thanks!

markstewie
  • 9,237
  • 10
  • 50
  • 72
  • 1
    May have worked it out... I've included require.js in the build. eg r.js -o name=main out=main.min.js mainConfigFile=main.js include=libs/requirejs/require.js is that right though? – markstewie Mar 07 '13 at 04:04
  • 1
    You're correct that RequireJS must also be included in the minified file. Also, if you're concerned with reducing the size of your minified js file, try using Almond. It's a tiny alternative loader by the same author as RequireJS that's designed for use in minified production builds. – cspotcode Mar 07 '13 at 04:38

2 Answers2

21

You're correct, you need to include requireJS in your build. Take a look at http://requirejs.org/docs/optimization.html#onejs. You'll find an example for the command line there. If you're using a build profile it will look something like this -

({
baseUrl: "../Scripts",
paths: {
    requireLib: 'libs/require'
},
name: "main",
out: "main-built.js",
include: ["requireLib"]
})
JackMorrissey
  • 2,567
  • 2
  • 21
  • 18
0

Quick fix: use r.js -o build.js instead of node r.js -o build.js


I had the problem when I was trying to call r.js through node:

node r.js -o build.js

Calling r.js directly fixed the problem:

r.js -o build.js

Note: r.js was installed globally with npm install -g requirejs

arthur.sw
  • 11,052
  • 9
  • 47
  • 104