4

I have the following directory structure (relevant bits only):

app
 - build
   - script.js
 - js
   - lib
     - require.js
     - jquery-1.10.2.js
   - app.js
 - index.html
Gruntfile.js

Gruntfile.js contains the following:

module.exports = function (grunt) {

    var gruntConfig = {
        pkg: grunt.file.readJSON('package.json'),
        requirejs: {
            compile: {
                options: {
                    name: 'app',
                    baseUrl: 'app/assets/js',
                    out: 'app/assets/build/script.js',
                    include:  ['lib/require'],

                    uglify: {
                        // beautify: true,
                        defines: {
                            DEBUG: ['name', 'false']
                        }
                    },

                    paths: {
                        jquery: "lib/jquery-1.10.2"
                    }
                }
            }
        },
    };

    grunt.initConfig(gruntConfig);

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

};

app.js contains the following:

require(['jquery'], function ($) {
    // Do stuff
});

How do I set it up so that it copies all the JavaScript needed into build/script.js, not just app.js and require.js when I tell it to (erroring when I try to use jQuery)? I also want to be able to add modules without adding them to my Gruntfile, just by adding them to script.js.

4

2 回答 2

1

您可以定义一个 mainConfigFile 并在配置文件中包含指向您的 lib 代码的路径,如下所示

gruntfile 的内容:

requirejs: {
  compile: {
    options: {
      baseUrl: "app/js/",
      mainConfigFile: app/config.js",
      out: "build/script.js",
      name: "config"
    }
  }
}

配置文件内容:

require.config({
  paths: {
    lib: "<path to>/js/lib",
    jquery: "<path to>/js/lib/jquery-1.10.2.min",
    .
    and particular file required
    .
  }

Requirejs 会将所有路径文件的内容添加到优化后的代码中。

于 2014-01-03T08:26:49.657 回答
0

尝试将此添加到您的选项对象:

findNestedDependencies: true

这样 grunt-contrib-requirejs(实际上只是 requirejs)将找到您的配置文件中列出的任何依赖项。

希望对你有效。

于 2015-04-06T19:37:51.617 回答