[英]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
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
的目录中,该目录与src
和out
文件夹平行。
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. 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.