Otskimanot Sqilal
Otskimanot Sqilal

Reputation: 2402

grunt requirejs 'define is undefined'

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');

}

Upvotes: 14

Views: 16871

Answers (4)

Per
Per

Reputation: 391

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

Upvotes: 0

Thomas Higginbotham
Thomas Higginbotham

Reputation: 1792

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']
        }
    }
}

Upvotes: 12

Lucia
Lucia

Reputation: 13597

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');

Upvotes: -1

Andreas Köberle
Andreas Köberle

Reputation: 111092

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.

Upvotes: 8

Related Questions