我正在使用 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/