gruntjsgrunt-useminangular-fullstack

cant replace HTML base tag with grunt-usemin's custom blockReplacements


I'm trying to replace the HTML base tag's href attribute value with /mydir/. Have attempted this with grunt-usemin's blockReplacements.

Here is the configuration

// Reads HTML for usemin blocks to enable smart builds that automatically
// concat, minify and revision files. Creates configurations in memory so
// additional tasks can operate on them
useminPrepare: {
  html: ['<%= yeoman.client %>/index.html'],
  options: {
    dest: '<%= yeoman.dist %>/public'
  }
},

// Performs rewrites based on rev and the useminPrepare configuration
usemin: {

  html: ['<%= yeoman.dist %>/public/{,*/}*.html'],
  css: ['<%= yeoman.dist %>/public/{,*/}*.css'],
  js: ['<%= yeoman.dist %>/public/{,*/}*.js'],
  options: {
    assetsDirs: [
      '<%= yeoman.dist %>/public',
      '<%= yeoman.dist %>/public/assets/images'
    ],
    blockReplacements: {
      baseUrl: function (block) {
        grunt.log.debug("******************* blockReplacements *******************");
        return '<base href="/mydir/">';
      }
    },

    // This is so we update image references in our ng-templates
    patterns: {
      js: [
        [/(assets\/images\/.*?\.(?:gif|jpeg|jpg|png|webp|svg))/gm, 'Update the JS to reference our revved images']
      ]
    }

  }
}

My html that is calling usemin:

<!-- build:baseUrl /mydir/ -->
<base href="/">
<!-- endbuild -->

I am running:

$ grunt serve:dist

But this only removed the base tag and does not replace. I have a suspicion I need to configure a baseUrl attribute within the Gruntfile, but I'm not trying to rev/uglify files and believe this attribute would point files that need reving etc.

Also I've tried running debug to fire grunt.log.debug calls the buildReplace function:

$ grunt --debug serve:dist

But there is no console output.

Note: My application is based on angular fullstack generator application. https://github.com/angular-fullstack/generator-angular-fullstack

Any help or suggestions appreciated.


Solution

  • After getting no where, I have not been back to this issue for a while. Having just revisited, it seems upgrading from grunt-usemin": "~2.1.1" to grunt-usemin": "3.0.0" fixed the issue. Still not sure if a specific bug was address to fix this in 3.0.0

    Also Note: baseUrl in the Gruntfile is assigned a function with first parameter being block:

    baseUrl: function (block) {
        grunt.log.debug("******************* blockReplacements *******************");
        return '<base href="/mydir/">';
    }
    

    This parameter named block is not actually used in the function, the href return is a string (I know this is bad and shouldn't be used this way, but just pointing something out here). The call to this function in client/index.html must have a parameter passed to it:

    <!-- build:baseUrl /mydir/ -->
    <base href="/">
    <!-- endbuild -->
    

    If you take away /mydir/ url portion the following would fail to create the base tag:

    <!-- build:baseUrl -->
    <base href="/">
    <!-- endbuild -->
    

    So in effect we could add /some-rubbish-url-portion/ and it would not fail and would not have any effect on the base tag's outcome.