gruntjs - 使用 Usemin 时如何在 Concat 选项中添加分隔符

标签 gruntjs group-concat grunt-usemin

我正在使用 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: '})();'
    }
}

最佳答案

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

进程选项说明link

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

关于gruntjs - 使用 Usemin 时如何在 Concat 选项中添加分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750215/

相关文章:

node.js - Node-sass 错误(尝试重新安装)

webpack - 生成工具和任务运行器之间的区别

javascript - Grunt、Compass、Foundation(foundationpress wordpress 主题)

mysql - 使用 JOIN 和 GROUP_CONCAT 进行更新

使用两个连接的列将 MySQL 组连接转换为 Postgres

node.js - grunt-usemin 阻止与 html 文件无关的路径?

node.js - 咕噜-鲍尔-安装 : exclude certain components

javascript - 如何在一个 grunt.registerTask 中调用多个 grunt.run.task?

php - MySQL子查询拉取多个结果

javascript - jit-咕噜声 : Plugin for the "clean" task not found?