如何摆脱 grunt-processhtml“<link rel=....>”标签
How to get rid of grunt-processhtml "<link rel=....>" tags
我正在尝试从 Javascript.
中动态加载一些 css 文件
片段:
if (theme === 'testtheme' || theme === 'testtheme/') {
css =
<!-- build:css({.tmp,app}) styles/main_testtheme.css -->
'<link rel="stylesheet" href="styles/css/main_testtheme.css" type="text/css" />'
<!-- endbuild -->
;
} else {
css =
<!-- build:css({.tmp,app}) styles/main.css -->
'<link rel="stylesheet" href="styles/css/main.css" type="text/css" />'
<!-- endbuild -->
;
}
但是,g运行t-build 任务将构建注释之间的所有文本替换为类似以下内容:
<link rel="stylesheet" href="styles/e59b065a.main.css">
从而删除字符串引号并使代码无效。
我想运行:
<!-- build:css({.tmp,app}) styles/main.css -->
css = 'styles/css/main.css';
<!-- endbuild -->
应该导致:
css = 'styles/e59b065a.main.css';
这将允许测试未缩小(未构建)和缩小版本。 G运行t build 对我来说大约需要 5 分钟,所以我在开发时尽量避免这种情况。
编辑:
我可能可以覆盖 css 的默认 blockReplacement(请参阅 https://github.com/yeoman/grunt-usemin#blockreplacements ),但这会让后来的任何人都感到痛苦,因为他们试图弄清楚为什么他们的样式表没有正确嵌入。
我仍然无法为此找到可接受的解决方案,但目前可以使用以下解决方案:
Gruntfile.js 片段:
useminPrepare: {
html: ['<%= yeoman.app %>/index.html', '<%= yeoman.app %>/includes.html'],
options: {
dest: '<%= yeoman.dist %>',
flow: {
// i'm using this config for all targets, not only 'html'
steps: {
// Here you define your flow for your custom block
cssQuoted: ['concat', 'cssmin'],
// use the option below where you have minified files that you just want to concatenate
concatjs: ['concat'],
// Note that you NEED to redefine flow for default blocks...
// These below is default flow.
js: ['concat', 'uglifyjs'],
css: ['concat', 'cssmin']
},
// also you MUST define 'post' field to something not null
post: {}
}
}
},
usemin: {
//css_name: ['<%= yeoman.dist %>/{,*/}*.html'],
html: ['<%= yeoman.dist %>/{,*/}*.html'],
css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
options: {
//patterns: {
// html: [
// [ /cssHrefString\s+=\s+['"]([^"']+)["']/gm, 'Update the HTML to reference our revved/min CSS and add quotes' ]
// ]
//},
blockReplacements: {
cssQuoted: function(block){
return '\'<link rel="stylesheet" href="' + block.dest + '">\'';
},
concatjs: function (block) {
return '<script src="' + block.dest + '"></script>';
}
}
}
},
script.js 片段:
var theme = getUrlParameter('theme');
var cssHrefString;
// we need fully qualified css tags below otherwise grunt build will not be able to pick them up
if (theme === 'testtheme' || theme === 'testtheme/') {
cssHrefString =
<!-- build:cssQuoted({.tmp,app}) styles/main_testtheme.css -->
'<link rel="stylesheet" href="styles/css/main_testtheme.css">'
<!-- endbuild -->
;
} else {
cssHrefString =
<!-- build:cssQuoted({.tmp,app}) styles/main.css -->
'<link rel="stylesheet" href="styles/css/main.css">'
<!-- endbuild -->
;
}
$('head').append(cssHrefString);
g运行t 配置文件添加了 concatjs 的定义 - 一个只连接缩小文件的任务,不 运行 它们的丑化器. cssQuoted 获取块之间的字符串并将其替换为带引号的 "link rel=..." 标记。
确保你的 g运行t-usemin 插件版本是最新的 - 我花了几个小时尝试旧版本的选项 (~0.1 .3).上面的代码适用于 3.0.0.
我正在尝试从 Javascript.
中动态加载一些 css 文件片段:
if (theme === 'testtheme' || theme === 'testtheme/') {
css =
<!-- build:css({.tmp,app}) styles/main_testtheme.css -->
'<link rel="stylesheet" href="styles/css/main_testtheme.css" type="text/css" />'
<!-- endbuild -->
;
} else {
css =
<!-- build:css({.tmp,app}) styles/main.css -->
'<link rel="stylesheet" href="styles/css/main.css" type="text/css" />'
<!-- endbuild -->
;
}
但是,g运行t-build 任务将构建注释之间的所有文本替换为类似以下内容:
<link rel="stylesheet" href="styles/e59b065a.main.css">
从而删除字符串引号并使代码无效。
我想运行:
<!-- build:css({.tmp,app}) styles/main.css -->
css = 'styles/css/main.css';
<!-- endbuild -->
应该导致:
css = 'styles/e59b065a.main.css';
这将允许测试未缩小(未构建)和缩小版本。 G运行t build 对我来说大约需要 5 分钟,所以我在开发时尽量避免这种情况。
编辑: 我可能可以覆盖 css 的默认 blockReplacement(请参阅 https://github.com/yeoman/grunt-usemin#blockreplacements ),但这会让后来的任何人都感到痛苦,因为他们试图弄清楚为什么他们的样式表没有正确嵌入。
我仍然无法为此找到可接受的解决方案,但目前可以使用以下解决方案:
Gruntfile.js 片段:
useminPrepare: {
html: ['<%= yeoman.app %>/index.html', '<%= yeoman.app %>/includes.html'],
options: {
dest: '<%= yeoman.dist %>',
flow: {
// i'm using this config for all targets, not only 'html'
steps: {
// Here you define your flow for your custom block
cssQuoted: ['concat', 'cssmin'],
// use the option below where you have minified files that you just want to concatenate
concatjs: ['concat'],
// Note that you NEED to redefine flow for default blocks...
// These below is default flow.
js: ['concat', 'uglifyjs'],
css: ['concat', 'cssmin']
},
// also you MUST define 'post' field to something not null
post: {}
}
}
},
usemin: {
//css_name: ['<%= yeoman.dist %>/{,*/}*.html'],
html: ['<%= yeoman.dist %>/{,*/}*.html'],
css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
options: {
//patterns: {
// html: [
// [ /cssHrefString\s+=\s+['"]([^"']+)["']/gm, 'Update the HTML to reference our revved/min CSS and add quotes' ]
// ]
//},
blockReplacements: {
cssQuoted: function(block){
return '\'<link rel="stylesheet" href="' + block.dest + '">\'';
},
concatjs: function (block) {
return '<script src="' + block.dest + '"></script>';
}
}
}
},
script.js 片段:
var theme = getUrlParameter('theme');
var cssHrefString;
// we need fully qualified css tags below otherwise grunt build will not be able to pick them up
if (theme === 'testtheme' || theme === 'testtheme/') {
cssHrefString =
<!-- build:cssQuoted({.tmp,app}) styles/main_testtheme.css -->
'<link rel="stylesheet" href="styles/css/main_testtheme.css">'
<!-- endbuild -->
;
} else {
cssHrefString =
<!-- build:cssQuoted({.tmp,app}) styles/main.css -->
'<link rel="stylesheet" href="styles/css/main.css">'
<!-- endbuild -->
;
}
$('head').append(cssHrefString);
g运行t 配置文件添加了 concatjs 的定义 - 一个只连接缩小文件的任务,不 运行 它们的丑化器. cssQuoted 获取块之间的字符串并将其替换为带引号的 "link rel=..." 标记。
确保你的 g运行t-usemin 插件版本是最新的 - 我花了几个小时尝试旧版本的选项 (~0.1 .3).上面的代码适用于 3.0.0.