复杂的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"]); };更多推荐
发布评论