如何摆脱grunt-processhtml&#34; <link rel =“....”/>&#34;标签

时间:2015-10-14 10:58:43

标签: gruntjs grunt-usemin grunt-html-build

我试图从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 -->
    ;
  }

但是,grunt-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';

这将允许测试未经编译的(未构建的)和缩小的版本。 Grunt构建对我来说大约需要5分钟,所以我在开发过程中试图避免这种情况。

修改 我可以覆盖css的默认blockReplacement(参见https://github.com/yeoman/grunt-usemin#blockreplacements),但是对于任何后来尝试找出其样式表未正确嵌入的原因的人来说都会很痛苦。

1 个答案:

答案 0 :(得分:0)

我仍然无法为此找到一个可接受的解决方案,但现在有一个有效的解决方案:

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 snippet:

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);

grunt配置文件为 concatjs 添加了一个定义 - 一个只连接缩小文件的任务,不会对它们运行uglifier。 cssQuoted ,它接受块之间的字符串,并用引用的&#34;链接替换它= ...&#34;标签

确保您的grunt-usemin插件版本是最新的 - 我在几个小时内尝试使用旧版本(~0.1.3)的选项。上面的代码适用于3.0.0。