【问题标题】:How to add separator to Concat options when using Usemin使用 Usemin 时如何将分隔符添加到 Concat 选项
【发布时间】:2014-07-08 03:35:04
【问题描述】:

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

【问题讨论】:

    标签: gruntjs group-concat grunt-usemin


    【解决方案1】:
     concat: {
      options: {
        process: function (src, filepath) {
          if (filepath.split(/\./).pop() === 'js') {
            return src + ';\n';
          }
          return src;
        }
      }
    }
    

    进程选项说明link

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

    【讨论】:

    • 您能解释一下这里发生了什么并添加指向相关文档的链接吗?
    猜你喜欢
    • 2013-03-22
    • 2013-08-20
    • 2016-01-20
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 2021-10-23
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多