复杂的gruntjs任务(Complex gruntjs tasks)

编程入门 行业动态 更新时间:2024-10-24 18:27:31
复杂的gruntjs任务(Complex gruntjs tasks)

我有以下情况:

连接一些文件 uglify一些文件(基于已连接的文件) 将文件连接到另一个文件(上面已经证实)

我怎么写gruntfile.js?

我尝试过这样的东西,但它不起作用。

谢谢

module.exports = function(grunt) { grunt.initConfig({ pkg:grunt.file.readJSON('package.json'), concat: { target: { files: [{ "dest/js/admin.main.js": ["js/spa.js", "js/spa.library.js"], "dest/js/jquery-1.min.js": ["js/jquery.min.js", "js/jquery-ui.min.js"] }]} }, uglify: { target: { files: [{ "dest/js/admin.main.min.js": ["dest/js/admin.main.js"], "dest/js/jquery-2.min.js": ["js/jquery.loadingoverlay.js"] }] } }, concat: { target: { files: [{ "dest/js/jq.min.js": ["dest/js/jquery-1.min.js", "dest/js/jquery-2.min.js"] }] } } }); grunt.loadNpmTasks("grunt-contrib-concat"); grunt.loadNpmTasks("grunt-contrib-uglify"); grunt.registerTask("default", ["concat", "uglify"]); };

I have the following scenario:

concat some files uglify some files (based on the concatinated ones) concat a files to another file (that was uglified above)

How would I write gruntfile.js?

I tried something as this but it didn't work.

Thanks

module.exports = function(grunt) { grunt.initConfig({ pkg:grunt.file.readJSON('package.json'), concat: { target: { files: [{ "dest/js/admin.main.js": ["js/spa.js", "js/spa.library.js"], "dest/js/jquery-1.min.js": ["js/jquery.min.js", "js/jquery-ui.min.js"] }]} }, uglify: { target: { files: [{ "dest/js/admin.main.min.js": ["dest/js/admin.main.js"], "dest/js/jquery-2.min.js": ["js/jquery.loadingoverlay.js"] }] } }, concat: { target: { files: [{ "dest/js/jq.min.js": ["dest/js/jquery-1.min.js", "dest/js/jquery-2.min.js"] }] } } }); grunt.loadNpmTasks("grunt-contrib-concat"); grunt.loadNpmTasks("grunt-contrib-uglify"); grunt.registerTask("default", ["concat", "uglify"]); };

最满意答案

在grunt中,您只能为任务定义一次配置,但您可以定义多个目标(具有不同的名称)。 因此,在您的情况下,您需要在'concat'任务下定义两个目标,然后按顺序调用它们:

module.exports = function(grunt) { grunt.initConfig({ pkg:grunt.file.readJSON('package.json'), concat: { step1: { files: [{ "dest/js/admin.main.js": ["js/spa.js", "js/spa.library.js"], "dest/js/jquery-1.min.js": ["js/jquery.min.js", "js/jquery-ui.min.js"] }] }, step2: { files: [{ "dest/js/jq.min.js": ["dest/js/jquery-1.min.js", "dest/js/jquery-2.min.js"] }] } }, uglify: { target: { files: [{ "dest/js/admin.main.min.js": ["dest/js/admin.main.js"], "dest/js/jquery-2.min.js": ["js/jquery.loadingoverlay.js"] }] } }, }); grunt.loadNpmTasks("grunt-contrib-concat"); grunt.loadNpmTasks("grunt-contrib-uglify"); grunt.registerTask("default", ["concat:step1", "uglify", "concat:step2"]); };

In grunt you can only define the config for a task once, but you can define multiple targets (with different names). So in your case you need to define two targets under the 'concat' task and then call them in sequence:

module.exports = function(grunt) { grunt.initConfig({ pkg:grunt.file.readJSON('package.json'), concat: { step1: { files: [{ "dest/js/admin.main.js": ["js/spa.js", "js/spa.library.js"], "dest/js/jquery-1.min.js": ["js/jquery.min.js", "js/jquery-ui.min.js"] }] }, step2: { files: [{ "dest/js/jq.min.js": ["dest/js/jquery-1.min.js", "dest/js/jquery-2.min.js"] }] } }, uglify: { target: { files: [{ "dest/js/admin.main.min.js": ["dest/js/admin.main.js"], "dest/js/jquery-2.min.js": ["js/jquery.loadingoverlay.js"] }] } }, }); grunt.loadNpmTasks("grunt-contrib-concat"); grunt.loadNpmTasks("grunt-contrib-uglify"); grunt.registerTask("default", ["concat:step1", "uglify", "concat:step2"]); };

更多推荐

本文发布于:2023-04-29 11:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336290.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:gruntjs   Complex   tasks

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!