简体   繁体   English

使用RequireJS生成捆绑包时出现问题

[英]Problems generating bundle with RequireJS

I have a Typescript project: 我有一个Typescript项目:

myproject
 |
 +-src (folder)
 |  |
 |  +-main.ts
 |  +-stringHandler.ts
 |  +-disposable.ts
 +-out (folder)
 |  |
 |  +-...
 +-Gruntfile.js

In my Grunt configuration I have a 2-step task which compiles all .ts files in myproject/src/ and generates corresponding .js files into myproject/out/ . 在我的Grunt配置中,我有一个两步任务,该任务将编译myproject/src/所有.ts文件,并将相应的.js文件生成到myproject/out/ So after the first step of the task is complete, I have the following: 因此,任务的第一步完成后,我将得到以下内容:

myproject
 |
 +-out (folder)
    |
    +-main.js
    +-stringHandler.js
    +-disposable.js

Bundling 捆绑

The second step of the task is generating bundle file myproject.js . 该任务的第二步是生成捆绑文件 myproject.js I am using RequireJS for this purpose. 我为此使用RequireJS

I have installed grunt-contrib-requirejs . 我已经安装了grunt-contrib-requirejs The Gruntfile.js file handling the bundling task is as follows (showing only relevant parts in the file): 处理捆绑任务的Gruntfile.js文件如下(仅显示文件中的相关部分):

module.exports = function(grunt) {
  var config = {
    pkg: grunt.file.readJSON('package.json'),
    requirejs: {
      compile: {
        options: {
          baseUrl: "out",
          bundles: {
            'myproject': ['main', 'stringHandler', 'disposable']
          },
          out: 'out/myproject.js'
        }
      }
    }
  };
  grunt.initConfig(config);
  grunt.loadNpmTasks('grunt-contrib-requirejs');
  grunt.registerTask('default', ['compile', 'requirejs']);
};

When Grunt reaches requirejs , after successfully compiling the project, I get the following error: 成功编译项目后,当Grunt达到requirejs ,出现以下错误:

Running "requirejs:compile" (requirejs) task { [Error: Error: Missing either a "name", "include" or "modules" option at Function.build.createConfig (C:\\Users\\myuser\\Documents\\myproject\\node_modules\\grunt-contrib-requirejs\\node_modules\\requirejs\\bin\\r.js:29567:19) ] originalError: [Error: Missing either a "name", "include" or "modules" option] } 运行“ requirejs:compile”(requirejs)任务{[错误:错误:在Function.build.createConfig(C:\\ Users \\ myuser \\ Documents \\ myproject \\ node_modules中缺少“名称”,“包含”或“模块”选项\\ grunt-contrib-requirejs \\ node_modules \\ requirejs \\ bin \\ r.js:29567:19)] originalError:[错误:缺少“名称”,“包含”或“模块”选项]}

I can understand there are missing parameters, but when I use name I get other errors. 我可以理解缺少参数,但是使用name会遇到其他错误。 I guess there must be something wrong at a more generic level. 我想在更一般的层面上一定有问题。 What is the correct configuration format? 正确的配置格式是什么? Thanks 谢谢

This assumes main.ts is your application's entry point and that it contains a require.config section with your application dependencies (libraries and shims). 假设main.ts是应用程序的入口点,并且它包含带有应用程序依赖项(库和require.config节。

First, move the require.config section out of main.ts and into its own file, config.ts . 首先,将require.config部分移出main.ts并移入其自己的文件config.ts Leave the application bootstrap code in main.ts . 将应用程序引导程序代码保留在main.ts

Then determine where you want this optimized application code deployed. 然后确定要在何处部署此优化的应用程序代码。 Let's assume it is to a directory named build , which is parallel to your src and out folders. 假设它位于名为build的目录中,该目录与srcout文件夹平行。

Update you Gruntfile to reflect this configuration: 更新您的Gruntfile以反映此配置:

requirejs: {
    compile: {
        options: {
            baseUrl: "out",
            mainConfigFile: "out/config.js",
            modules: [
                { name: "main" }
            ],
            dir: "build",
            optimize: "none" // skip compression while debugging
        }
    }
}

You can read more about each of these config options at http://requirejs.org/ but here's the basic rundown: 您可以在http://requirejs.org/上了解有关每个配置选项的更多信息,但这是基本的摘要:

  • baseUrl : Where the source JS code lives. baseUrl :源JS代码所在的位置。
  • mainConfigFile : Points to the config object mentioned above. mainConfigFile :指向上述配置对象。 It tells the plugin where the dependencies live. 它告诉插件依赖项在哪里。 This obviates the need to specify and manually update the list of dependencies in two places. 这样就无需在两个地方指定和手动更新依赖项列表。
  • modules : Is an array of application bootstraps. modules :是应用程序引导程序的数组。 In this case a list of one, main.js . 在这种情况下,一个清单main.js
  • dir : Where the optimized application will be generated. dir :将在其中生成优化的应用程序。 Note that your dependencies will also be copied here. 请注意,您的依赖项也将在此处复制。
  • optimize : I left this off so you can easily debug the resulting app under ./build . optimize :我取消了此操作,因此您可以在./build下轻松调试生成的应用程序。 Remove it when you're happy and the plugin will optimize (compress and munge) your build files. 满意时将其删除,该插件将优化(压缩和压缩)您的构建文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM