简体   繁体   English

grunt-contrib-concat:指定要串联的最后一个文件?

[英]grunt-contrib-concat: specify last file to be concatenated?

I'm using grunt-contrib-concat to concatenate all my custom JS files together, and wrap them with an IIFE. 我正在使用grunt-contrib-concat将所有自定义JS文件连接在一起,并用IIFE包裹它们。 I'm running into some load order issues ( namely, my init file is executing before some modules, causing undefined errors ). 我遇到了一些加载顺序问题(即,我的初始化文件在某些​​模块之前执行,导致未定义的错误)。 I want to specify init.js should be LAST in the concat order, but I don't want to specify the order for all the other JS files as well, just that this specific one goes last. 我想指定init.js在concat顺序中应该为LAST,但是我也不想为所有其他JS文件指定顺序,只是这个特定的最后一个。

Here is the current config of my concat: 这是我的concat的当前配置:

/**
 * Set project info
 */
project: {
  src: 'src',
  app: 'app',
  assets: '<%= project.app %>/assets',
  css: [
    '<%= project.src %>/scss/style.scss'
  ],
  js: [
    '<%= project.src %>/js/*.js'
  ]
},

concat: {
  dev: {
    files: {
      '<%= project.assets %>/js/scripts.min.js': '<%= project.js %>'
    }
  },
  options: {
    stripBanners: true,
    nonull: true,
    banner: ';(function($, window, document, undefined){ \n "use strict";',
    footer: '}(jQuery, window, document));'
  }
},

How do I specify a last file to be concatenated without adding an extra grunt module to my project ? 如何在不向我的项目中添加额外的grunt模块的情况下指定要串联的最后一个文件?

You can do it by concatenating all files but init.js , and then concatenating the result with init.js , using the array syntax that maintains order: 您可以通过连接所有的文件,但这样做init.js ,然后连接,结果init.js ,使用维持秩序的数组语法:

concat: {
    dev1: {
        dest: '<tmp>/concat1.js',
        src: ['<all your js>', '!init.js']
    },
    devFinal: {
        options: {
            stripBanners: true,
            nonull: true,
            banner: ';(function($, window, document, undefined){ \n "use strict";',
            footer: '}(jQuery, window, document));'
        },
        files: {
            dest: 'scripts.min.js',
            src: ['<tmp>/concat1.js', 'init.js']
        },
    },
}

And then call both targets in succession: 然后依次调用两个目标:

grunt.registerTask('myConcat', ['concat:dev1', 'concat:devFinal']);

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

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