javascript grunt requirejs '定义未定义'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15247048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grunt requirejs 'define is undefined'
提问by Otskimanot Sqilal
I'm trying to optimize RequireJS
using GruntJS
, using the grunt-contrib-requirejs
plugin.
我试图优化RequireJS
使用GruntJS
,使用grunt-contrib-requirejs
插件。
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
.
问题是我的代码在优化之前运行良好,然后在优化之后,在控制台上显示Uncaught ReferenceError: define is not defined
。
Here's the Gruntfile.js
这是 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');
}
采纳答案by Andreas K?berle
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.
作为define
requireJs 函数,您似乎错过了加载 requireJs 或任何其他 AMD 加载器。如果你不需要加载任何其他 AMD 模块然后你编译一次,你可以使用像杏仁这样的轻量级加载器垫片。
回答by Thomas Higginbotham
Adding the require.js file as an "include" option should work.
添加 require.js 文件作为“包含”选项应该可以工作。
requirejs: {
compile : {
options : {
name : 'main',
baseUrl : ".",
mainConfigFile : "./main.js",
out : "./optimized.js",
preserveLicenseComments: false,
include: ['path/to/require.js']
}
}
}
回答by Per Thomasson
As pointed out before the requirejs-script is missing.
正如在 requirejs-script 丢失之前指出的那样。
This is the way the official requirejs-pagesuggests you do it (ripped from my gruntfile):
这是官方 requirejs-page建议您这样做的方式(从我的 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 pathsand include, those are vital for the require to be defined. Just point the requireLib-option to your require.js-file.
观察选项路径和include,这些对于定义 require 至关重要。只需将 requireLib 选项指向您的 require.js 文件。
See the official answer here: http://requirejs.org/docs/optimization.html#onejs
在此处查看官方答案:http: //requirejs.org/docs/optimization.html#onejs
回答by Lucia
It seems that the grunt-contrib-requirejs doesn't compile requirejs in by default. You could use concat to re-add requirejs back in.
似乎 grunt-contrib-requirejs 默认不编译 requirejs。您可以使用 concat 重新添加 requirejs。
concat : {
dist : {
src : ['./optimized.js', 'path/to/requirejs.js'],
dest : './optimized.js'
},
}
grunt.loadNpmTasks('grunt-contrib-concat');