最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - grunt requirejs 'define is undefined' - Stack Overflow

programmeradmin0浏览0评论

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
  • How to you use load the compiled file? As define is a requireJs function it seems you miss to load requireJs. – Andreas Köberle Commented Mar 7, 2013 at 20:43
  • Yep, it was because requirejs is not included. Once i load it, got no errors. – Otskimanot Sqilal Commented Mar 9, 2013 at 5:45
  • 1 ok will add this as an answer too. – Andreas Köberle Commented Mar 9, 2013 at 9: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 Commented Aug 5, 2013 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 Commented Nov 28, 2013 at 5:07
 |  Show 2 more comments

4 Answers 4

Reset to default 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']
        }
    }
}

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');
发布评论

评论列表(0)

  1. 暂无评论