[英]How to make couple of minified files from different js files using grunt
I am new to grunt and task runners in JS, so this might seem a simple question but I have been unable to find exact working answer. 我对JS的咕gr声和任务赛跑者是陌生的,因此这似乎是一个简单的问题,但我无法找到确切的工作答案。
I have : 我有 :
concat: {
options: {
// define a string to put between each file in the concatenated output
separator: '\n\n'
},
dist: {
// the files to concatenate
src: ['scripts/app.js', 'scripts/constant.js'
],
// the location of the resulting JS file
dest: 'scripts/custom.js'
}
},
This task collects all my custom file together. 此任务将所有我的自定义文件收集在一起。 What I want is to do similar thing for all my vendors file.
我想要对所有供应商文件执行类似的操作。 Finally I should end up with two js only
custom.js
having my concatenated-minified code and vendor.js
having concatenated-minfied libraries. 最后,我应该以两个js结束,只有
custom.js
具有串联的vendor.js
代码, vendor.js
具有串联的最小化库。
How do I write grunt configuration for this. 我该如何为此编写grunt配置。 Do I need to make two different tasks.
我需要做两个不同的任务吗? If I write the above code twice with different input files, it seems to run the last code.
如果我使用不同的输入文件两次编写以上代码,则似乎在运行最后一个代码。
grunt-contrib-concat can be configured to utilize multiple-targets . grunt-contrib-concat可以配置为利用多个目标 。
For further documentation on this subject refer to multi-tasks and Task Configuration and Targets in the grunt
documentation. 有关此主题的更多文档,请参阅
grunt
文档中的多任务以及任务配置和目标 。
For your scenario you need to configure your concat
task similar to this ( Note: the new custom
and vendor
targets): 对于您的方案,您需要配置与此类似的
concat
任务( 注意:新的custom
和vendor
目标):
module.exports = function(grunt) {
grunt.initConfig({
concat: {
options: {
separator: '\n\n'
},
custom: {
src: ['scripts/app.js', 'scripts/constant.js'],
dest: 'scripts/output/custom.js'
},
vendor: {
// Modify the src and dest paths as required...
src: ['scripts/vendor/foo.js', 'scripts/vendor/baz.js'],
dest: 'scripts/output/vendor.js'
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('concatenate', [
'concat:custom', // <-- Targets in a task are called using a colon separator.
'concat:vendor'
]);
};
concat
concat
Using the example gist provided above you can run the concat
task via the CLI by typing the following command: 使用上面提供的示例要点,您可以通过以下命令通过CLI运行
concat
任务:
$ grunt concatenate
If you require different configuration options for both the custom
and vendor
targets you will need to move the options
object inside their respective targets. 如果
custom
目标和vendor
目标都需要不同的配置选项,则需要将options
对象移动到它们各自的目标中。 As explained here . 如这里所解释。
Note: Using the example gist provided the options
specified will apply to both targets. 注意:使用示例要点提供的示例,指定的
options
将适用于两个目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.