gruntjsgrunt-usemingrunt-html-build

How to get rid of grunt-processhtml "<link rel=....>" tags


I'm trying to dynamically load some css files from a Javascript.

Snippet:

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

However, the grunt-build task replaces all the text between the build comments with something like:

<link rel="stylesheet" href="styles/e59b065a.main.css">

thus removing the string quotes and rendering the code invalid.

How I would like to to run:

<!-- build:css({.tmp,app}) styles/main.css -->
css = 'styles/css/main.css';
<!-- endbuild -->

should result in:

css = 'styles/e59b065a.main.css';

That would allow testing both the unminified (unbuilt) and the minified versions. Grunt build takes around 5 minutes for me so I'm trying to avoid that while developing.

Edit: I can probably override the default blockReplacement for css (see https://github.com/yeoman/grunt-usemin#blockreplacements ) but it will make it a pain for anyone coming afterwards to try and figure out why their stylesheet is not embedded properly.


Solution

  • I still was not able to find an acceptable solution for this, but here's one that works, for now:

    Gruntfile.js snippet:

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

    The grunt config file adds a definition for concatjs - a tasks which only concatenates minified files, does not run the uglifier on them. cssQuoted which takes the string between the blocks and replaces it with a quoted "link rel=..." tag.

    Make sure your grunt-usemin plugin version is up-to-date - I lost several hours trying out options with an old version (~0.1.3). The code above works with 3.0.0.