14

I'm trying to optimize RequireJS using GruntJS, using the grunt-contrib-requirejs plugin.

The problem is my code works fine before optimizing it, and then after optimizing it, on the console it says Uncaught ReferenceError: define is not defined.

Here's the Gruntfile.js

module.exports = function (grunt) {
  grunt.loadNpmTasks('grunt-contrib-requirejs');

  grunt.initConfig({
    requirejs: {
        compile : {
            options : {
              name  : 'main',
              baseUrl : ".",
              mainConfigFile : "./main.js",
              out : "./optimized.js",
              preserveLicenseComments: false
           }
        }
}
  })

  grunt.registerTask('default', 'requirejs');

}
Mark Parnell
  • 9,175
  • 9
  • 31
  • 36
Otskimanot Sqilal
  • 2,332
  • 4
  • 21
  • 25
  • How to you use load the compiled file? As `define` is a requireJs function it seems you miss to load requireJs. – Andreas Köberle Mar 07 '13 at 20:43
  • Yep, it was because `requirejs` is not included. Once i load it, got no errors. – Otskimanot Sqilal Mar 09 '13 at 05:45
  • 1
    ok will add this as an answer too. – Andreas Köberle Mar 09 '13 at 09:09
  • 4
    @OtskimanotSqilal how did you include it? Did you add it as a seperate script tag or put the minified script in data-main? – streetlight Aug 05 '13 at 11:42
  • @OtskimanotSqilal & Andreas As the previous commenter asked, how exactly did you do this? Was the reference to the require lib made in the main.js file or did you modify the Gruntfile? I have this exact same issue. – jusopi Nov 28 '13 at 05:07
  • The only way I was able to bypass this was to add another script tag to the HTML with the uglified code. It seemed to deal with "define is undefined" error. I don't like this solution but it works for now. Ideally it should be handled in the data-main js file as a dependency? Not sure how to make that work. – jusopi Nov 28 '13 at 06:14
  • Doesn't doing so add the entire requirejs file to the output vs running it through the optimizer? Perhaps I'm missing something? – hybrid9 Jun 04 '14 at 18:28

4 Answers4

12

Adding the require.js file as an "include" option should work.

requirejs: {
    compile : {
        options : {
            name  : 'main',
            baseUrl : ".",
            mainConfigFile : "./main.js",
            out : "./optimized.js",
            preserveLicenseComments: false,
            include: ['path/to/require.js']
        }
    }
}
Thomas Higginbotham
  • 1,662
  • 20
  • 25
  • 1
    I'm having a similar issue, but wouldn't that just add requirejs to the output? It doesn't seem like it goes through the optimizer. – hybrid9 Jun 04 '14 at 18:26
  • Yes, it does add RequireJS to the output. It's a simple solution to the problem, but using a smaller AMD loader in the minified script (see accepted answer) would be more ideal. – Thomas Higginbotham Jun 07 '14 at 02:49
8

As define is a requireJs function it seems you miss to load requireJs or any other AMD loader. If you dont need to load any other AMD module then your complied once, you can use a light weight loader shim like almond.

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
0

As pointed out before the requirejs-script is missing.

This is the way the official requirejs-page suggests you do it (ripped from my gruntfile):

requirejs: {
  compile: {
    options: {
      baseUrl: "src/js",
      mainConfigFile: 'src/js/require.config.js', 
      paths: {
        requireLib: "vendor/require/require"
      },
      include: "requireLib",
      name: "require.config",
      out: "dist/js/bundle.js"
    }
  }
},

Observe the options paths and include, those are vital for the require to be defined. Just point the requireLib-option to your require.js-file.

See the official answer here: http://requirejs.org/docs/optimization.html#onejs

Per
  • 332
  • 3
  • 18
-1

It seems that the grunt-contrib-requirejs doesn't compile requirejs in by default. You could use concat to re-add requirejs back in.

concat : {
  dist : {
    src : ['./optimized.js', 'path/to/requirejs.js'],
    dest : './optimized.js'
  },
}

grunt.loadNpmTasks('grunt-contrib-concat');
Lucia
  • 13,033
  • 6
  • 44
  • 50