9

我的Gruntfile.js文件:

module.exports = function (grunt) {
    grunt.initConfig({
        pkg : grunt.file.readJSON('package.json'),
        requirejs : {
            compile: {
                options: {
                    baseUrl: "public_html/js",
                    mainConfigFile: "public_html/js/config.js",
                    out: "public_html/app.min.js"
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-requirejs');

    grunt.registerTask('default', ['requirejs']);
};

我的config.js文件:

'use strict';

require.config({
    deps: ['main'],
    paths: {
        jquery: 'vendor/jquery',
        jquery_tokeninput: 'vendor/jquery.tokeninput',
        underscore: 'vendor/underscore',
        backbone: 'vendor/backbone'
    },
    shim: {
        jquery: [],
        jquery_tokeninput: {
            deps: ['jquery']
        },
        backbone: {
            deps: ['vendor/underscore', 'vendor/jquery', 'vendor/jquery.tokeninput'],
            exports: 'Backbone'
        },
        underscore: {
            exports: '_'
        }
    }
});

require(['views/app'], function(AppView) {
  new AppView;
});

当我运行grunt requirejs它时出现以下错误:

Running "requirejs:compile" (requirejs) task
[Error: Error: Missing either a "name", "include" or "modules" option at function.build.createConfig (D:\project\node_modules\grunt-contrib-requirejs\node_modules\requirejs\bin\r.js:24829:19)]

第一次使用 gruntjs 和 requirejs 所以不知道为什么我会收到错误。

4

1 回答 1

10

更新了 grunt.js 文件以使用名称:

module.exports = function (grunt) {
    grunt.initConfig({
        pkg : grunt.file.readJSON('package.json'),
        requirejs : {
            compile: {
                options: {
                    name: "views/app",
                    baseUrl: "public_html/js",
                    mainConfigFile: "public_html/js/config.js",
                    out: "public_html/app.min.js"
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-requirejs');

    grunt.registerTask('default', ['requirejs']);
};

并从以下内容中删除config.js

require(['views/app'], function(AppView) {
  new AppView;
});
于 2013-07-26T21:15:13.057 回答