9

我正在使用 Grunt-usemin。但是连接的 JS 没有被 ';' 正确分隔。如何告诉 usemin 只为 JS 文件而不是 CSS 文件添加分隔符?

目前,我的 usemin 任务如下所示:

    useminPrepare: {
        options: {
            dest: '<%= config.dist %>'
        },
        html: '<%= config.app %>/index.html'
    },

    // Performs rewrites based on rev and the useminPrepare configuration
    usemin: {
        options: {
            assetsDirs: ['<%= config.dist %>', '<%= config.dist %>/images']
        },
        concat: {
            separator: ';'
        },
        html: ['<%= config.dist %>/{,*/}*.html'],
        css: ['<%= config.dist %>/styles/{,*/}*.css']
    },

另一个用例是将每个连接的模块包装在 IIFE 中,这需要此配置,但应仅应用于 *.js 文件:

concat: {
    options: {
        banner: ';(function () {',
        separator: '})(); (function () {',
        footer: '})();'
    }
}
4

1 回答 1

4
 concat: {
  options: {
    process: function (src, filepath) {
      if (filepath.split(/\./).pop() === 'js') {
        return src + ';\n';
      }
      return src;
    }
  }
}

工艺选项链接说明

该函数将文件路径拆分为带有 . (点)作为分隔符。Array 中的 pop() 方法返回数组的最后一项,它应该是扩展名。

于 2015-08-18T06:24:42.233 回答