2014-12-13 46 views
0

我是grunt 的新手,我想使用grunt将java脚本文件连接到一个文件 并且我有6个js文件,但它们需要以某种顺序运行代码像jQuery的错误应加载第一 但来自咕噜结果文件不保留该序列 我试了很多事情,如安排他们在SRC或使多个文件夹,但没有奏效使用grunt按顺序排列文件

注 - 当我通过复制和粘贴在一个文件中手动连接它工作正常 所以是否有任何命令让grunt连接这个文件在我写在src中的这个文件中作为例子 这是我gruntfile.js太

module.exports = function(grunt) { 

    // 1. All configuration goes here 
    grunt.initConfig({ 
    pkg: grunt.file.readJSON('package.json'), 




    // 1 - this is contecnate js files call them in one file only 
    concat: { 
     options: { 
     separator: ';', 
     }, 
     dist: { 
     src: ['src/public/js/jquery.min.js','src/public/js/bootstrap.js','public/js/modernizr.custom.77555.js', 
      'public/js/jquery.magnific-popup.js', 
    'public/js/jquery.mixitup.min.js','public/js/popup-box.js' ], 
     dest: 'dist/1newbuilt.js', 
     }, 
    }, 



    uglify: { 
     build: { 
     src: 'dist/1newbuilt.js', 
     dest: 'dist/1newbuilt.min.js' 
     } 
    } 


    }); 

    // end 1 - this is contecnate js files call them in one file only 







    // 3. Where we tell Grunt we plan to use this plug-in. 
    grunt.loadNpmTasks('grunt-contrib-concat'); 
    grunt.loadNpmTasks('grunt-contrib-uglify'); 

    // 4. Where we tell Grunt what to do when we type "grunt" into the terminal. 
    grunt.registerTask('default', ['concat', 'uglify']); 

}; 

我需要的文件在一些订单要连接像第一添加1.js再加入2.js后 所以我写序列中的文件,但这样没也没用 -

回答

1

如果你想继续使用咕噜-的contrib-CONCAT,并手动明确指定你的源代码,就像你有它应该工作。你看到这些模块的顺序是什么?你是否删除了uglify选项,并使用concat选项?这个咕噜配置正确地放置组合的脚本。

module.exports = function(grunt) { 
// 1. All configuration goes here 
    grunt.initConfig({ 




    // 1 - this is contecnate js files call them in one file only 
    concat: { 
     options: { 
     separator: ';', 
     }, 
     dist: { 
     src: ['a.js','b.js'], 
     dest: 'built.js', 
     }, 
    } 
    }); 

    // end 1 - this is contecnate js files call them in one file only 
// 3. Where we tell Grunt we plan to use this plug-in. 
    grunt.loadNpmTasks('grunt-contrib-concat'); 


    // 4. Where we tell Grunt what to do when we type "grunt" into the terminal. 
    grunt.registerTask('default', ['concat']); 

} 

这产生的结果像这个 -

(function(){ 
    console.log("module a"); 
})(); 
;(function(){ 
    console.log("module b"); 
})(); 

而且,只是风格的缘故,我没有看到需要一个分号分隔符。联合国人征求意见,如果你真的需要在您的JS文件到指定的依赖顺序另一块,你应该对使用的模块管理器,例如RequireJSBrowserify,或ES6 Modules(有某种形式的transpiler)

+0

感谢您的评论和代码段 审查后,我发现有一个错误,当我调用两个文件中的Java脚本文件开始于src/pu ..这是不必要的,之后写入正确的src所以我没有发现结果文件没有完成@thebringking – ramyMorad 2014-12-13 02:53:13

0

你不必写所有的JS文件。只需使用通配符。

concat: { 
     js: { 
     src: 'src/public/js/*.js', 
     dest: 'dest/js/concat.js' 
     }, 

你分钟的任务

min: { 
     js: { 
     src: 'dest/js/concat.js', 
     dest: 'dest/js/concat.min.js' 
     } 
    }, 
+0

我需要的移动文件被连接成一些命令,比如首先添加1.js,然后在它后面添加2.js,所以我按顺序编写它们,但这种方式对我来说也不起作用 – ramyMorad 2014-12-13 02:03:32

+0

grunt.loadNpmTasks('grunt-contrib-concat');这会做你的订单。只需使用此扩展名。 – AngularLover 2014-12-13 02:09:54