不使用Grunt.initConfig()注册Grunt任务
Register Grunt tasks without grunt.initConfig()
我希望我的gruntfile.js具有自然的结构,以便微任务一个接一个地执行。假设我有以下结构:
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: {
movedTyping: 'options...'
},
copy: {
typing: 'options...',
lessVariables: 'options...',
html: 'options...'
},
less: {
compile: 'options...'
},
typescript: {
compile: 'options...'
}
});
grunt.registerTask('build', [
// TYPESCRIPT
'typescript:compileSingle',
'copy:typing',
'clean:movedTyping',
// LESS
'less:compile',
'copy:lessVariables',
// HTML
'copy:html'
]);
但我想实现另一种结构:
grunt.registerTask('build', function () {
// TYPESCRIPT
grunt.task.run('typescript', 'options...');
grunt.task.run('copy', 'options...');
grunt.task.run('clean', 'options...');
// LESS
grunt.task.run('less', 'options...');
grunt.task.run('copy', 'options...');
// HTML
grunt.task.run('copy', 'options...');
});
如何?
您可以使用设置对象的属性。(点)符号。因此也可以设置嵌套结构数据。我还没有遇到更干净的方法,我很高兴看到更好的方法。
grunt.registerTask('build', function () {
// TYPESCRIPT
grunt.config.set('typescript.compile','<options>');
grunt.task.run('typescript');
......................
});
为了实现这一点,我创建了NPM模块创建了grunt任务。现在我的咕哝文件看起来是这样的:
// Gruntfile.js
module.exports = function (grunt) {
require('create-grunt-tasks')(grunt, function (create) {
create.task('build')
// Compile TypeScript and move typing
.sub('typescript', {
src: 'src/index.ts',
dest: 'build/index.js',
options: { module: 'amd', target: 'es5', declaration: true }
})
.sub('copy', {
expand: true, flatten: true,
src: 'build/index.d.ts',
dest: 'build/typing/index.d.ts'
})
.sub('clean', ['build/index.d.ts'])
// Copy HTML
.sub('copy', {
expand: true, flatten: true,
src: 'src/index.html',
dest: 'build/index.html'
});
});
}
相关文章:
- Grunt匹配正则表达式
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 如何将package.json数组传递给grunt.js
- Grunt-connect在根目录之外提供静态文件
- 点击facebook像素跟踪注册(JS/JQUERY代码)
- 如果助手不在,如何从Grunt中的代码中调用任务
- Grunt构建导致Angular应用程序在dist上崩溃
- 当组件准备就绪时,如何在Polymer中动态注册新属性
- 注册OpenLayers事件时,即使使用匿名函数或绑定,JavaScript关闭也会触发循环内的所有内容
- grunt上的压缩文件夹不起作用
- 你能把grunt.js中的linter改成jslint吗
- 如何在单击“提交”按钮时为“新建”窗口编写JavaScript,用“确定”和“取消”显示注册信息
- Angularamd在typescript控制器注册中
- 注册组件的非角度和角度版本
- 如何从已注册的事件发送 JavaScript 对象
- Grunt-将多个文件最小化/处理为一个文件
- 不使用Grunt.initConfig()注册Grunt任务
- 注册代码位于外部 JavaScript 文件中的 Grunt 任务
- 如何创建和组织配置和注册grunt任务
- 我在哪里注册我的JS,以便它是由grunt添加