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');
}
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');
}
Share
Improve this question
edited Nov 4, 2013 at 2:01
Mark Parnell
9,2009 gold badges32 silver badges37 bronze badges
asked Mar 6, 2013 at 12:11
Otskimanot SqilalOtskimanot Sqilal
2,4024 gold badges23 silver badges25 bronze badges
7
|
Show 2 more comments
4 Answers
Reset to default 12Adding 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']
}
}
}
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.
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
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');
define
is a requireJs function it seems you miss to load requireJs. – Andreas Köberle Commented Mar 7, 2013 at 20:43requirejs
is not included. Once i load it, got no errors. – Otskimanot Sqilal Commented Mar 9, 2013 at 5:45