【问题标题】:How to get rid of grunt-processhtml "<link rel=....>" tags如何摆脱 grunt-processhtml “<link rel=....>” 标签
【发布时间】:2015-10-14 10:58:43
【问题描述】:

我正在尝试从 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 任务将构建 cmets 之间的所有文本替换为以下内容:

<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 build 对我来说大约需要 5 分钟,所以我在开发时尽量避免这种情况。

编辑: 我可能可以覆盖 css 的默认 blockReplacement(请参阅 https://github.com/yeoman/grunt-usemin#blockreplacements ),但这会让任何后来尝试弄清楚为什么他们的样式表没有正确嵌入的人感到痛苦。

【问题讨论】:

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


    【解决方案1】:

    我仍然无法为此找到可接受的解决方案,但目前有一个可行的解决方案:

    Gruntfile.js sn-p:

    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 sn-p:

    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 获取块之间的字符串并将其替换为带引号的“link rel=...”标签。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多