简体   繁体   English

使用grunt顺序CONCATENATE文件

[英]using grunt to CONCATENATE FILES in sequence

i'm new to grunt and i want to concatenate java script files to one file using grunt and i have 6 js files but they need to be in some sequence to run the code without errors like jquery should loaded in first but the result file which came from grunt not preserve this sequence i tried many things like arrange them in src or make more than one folder but it didn't work 我是grunt的新手,我想使用grunt将java脚本文件连接到一个文件,并且我有6个js文件,但是它们需要按一定顺序运行代码,而不会出现错误,例如应该首先加载jquery,但是结果文件来自咕unt声不保存此序列,我尝试了很多事情,例如将它们排列在src中或制作多个文件夹,但这没有用

note - when i make manual concatenation in one file by copy and paste it works fine so is there any command to make grunt concatenate this files in the secuquence that i wrote them in src as example this is my gruntfile.js too 注意-当我通过复制和粘贴在一个文件中进行手动串联操作时,它可以正常工作,因此是否有任何命令使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']);

};

i need the files to be concatenated in some orders like first add 1.js then add 2.js after it so i wrote the files in sequence but this way didn't work too – 我需要按某些顺序将文件串联起来,例如先添加1.js,然后再添加2.js,所以我按顺序编写了文件,但是这种方式也行不通–

If you want to continue to use grunt-contrib-concat, and manually specify your sources explicitly, like you have it should work. 如果您想继续使用grunt-contrib-concat,并手动明确地指定您的来源,就像您应该使用它一样。 What order are you seeing the modules in? 您看到模块的顺序是什么? Have you removed the uglify option and just used the concat option? 您是否删除了uglify选项并仅使用了concat选项? This grunt config correctly puts the combined scripts in order. 此grunt配置正确地将组合脚本排序。

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']);

}

this produces a result like this- 产生这样的结果-

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

Also, just for styles sake, I don't see a need for a semi-colon separator. 另外,仅出于样式考虑,我认为不需要分号分隔符。 Another piece of un-solicited advice if you really need to specify a dependency order in your JS files, you should move towards using a module loader like RequireJS , Browserify , or ES6 Modules (with a transpiler of some sort) 如果您确实需要在JS文件中指定依赖关系顺序,则另一条未经请求的建议是,您应该转向使用RequireJSBrowserifyES6 Modules等模块加载器(带有某种编译器)

you dont have to write all your JS-files. 您不必编写所有JS文件。 Just use the wildcard. 只需使用通配符即可。

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

Your min task 您的最小任务

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM