2014-12-13 79 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