3996

grunt-usemin not replacing reference block with revved file line

Question:

I've been having an issue with grunt-usemin where it doesn't replace the non-revved reference block with the single revved line. The two files in the reference block get <strong>concatenated and uglified just fine</strong>; the single file metadata.min.js also <strong>gets versioned just fine</strong>; but, the reference to <strong>the revved file doesn't get inserted in to index.html</strong>. Just the non-revved line.

I'm using:

<ul><li>grunt-usemin 2.6.0</li> <li>grunt-filerev 2.1.1</li> <li>the Zend Framework for templating (hence the bizarre template paths)</li> </ul>

Here's the index.html reference block <strong>before</strong> running grunt build:

<!-- build:js js/dest/metadata.min.js --> <script src="js/metadata/MetadataController.js"></script> <script src="js/metadata/MetadataService.js"></script> <!-- endbuild -->

Here's the relevant Grunt config:

useminPrepare: { html: '../cdm_common/cdm/layouts/scripts/index.html', options: { dest: 'dist', root: '.' } }, filerev: { options: { encoding: 'utf8', algorithm: 'md5', length: 8 }, js: { src: ['dist/js/dest/*.js'], dest: 'js/dest/rev/test' } }, usemin: { html: '../cdm_common/cdm/layouts/scripts/index.html', options: { assetsDirs: ['js/dest/rev/test'] } }, grunt.registerTask('build' ['useminPrepare','concat:generated','uglify:generated','filerev','usemin']);

Here's the index.html <strong>after</strong> running grunt build:

<script src="js/dest/metadata.min.js"></script>

Any reason why the revved line shouldn't look like this?

<script src="js/dest/metadata.min.a5851d60.js"></script>

Is this a bug with grunt-usemin? Is a config off somewhere? Though not really answered, this is similar to: <a href="https://stackoverflow.com/questions/23507802/usemin-not-replacing-reference-blocks-in-html/" rel="nofollow">Usemin not replacing reference blocks in HTML</a>

Been beating my head against the desk for awhile. Any insight is greatly appreciated.

Answer1:

Try running grunt --debug with the following usemin configuration for some more information:

usemin: { html: '../cdm_common/cdm/layouts/scripts/index.html', options: { assetsDirs: ['js/dest/rev/test'], blockReplacements: { js: function (block) { grunt.log.debug(JSON.stringify(block.dest)); grunt.log.debug(JSON.stringify(grunt.filerev.summary)); return '<script src="'+block.dest+'"></script>'; } } } }

This will echo the current block its generating and an object with the files modified by filerev.

In my case I had an extra "public/" folder so my string would not match the key in the object, and therefor usemin was unable to find the new location made by filerev:

[D] "js/build/vendors.js" [D] "public\\js\\build\\vendors.js": "public\\js\\build\\vendors.4e02ac3d2e56a0666608.js", "public\\js\\build\\main.js": "public\\js\\build\\main.acd1b38e56d54a170d6d.js"}

Eventually I fixed this with this custom blockReplacements function, (i.e. replacing public/ and the obnoxious Windows path):

js: function (block) { var arr = {}; for (var key in grunt.filerev.summary) { arr[key.replace(/\\/g, "/").replace(/\/\//g, "/").replace("public/", "")] = grunt.filerev.summary[key].replace(/\\/g, "/"); } var path = (arr[block.dest] !== undefined) ? arr[block.dest] : block.dest; return '<script src="{{ asset(\''+Twig.basePath + path +'\') }}"></script>'; },

Answer2:

This occurred to me as well and the issue was caused by not having the correct assetDirs in the usemin block. You will want to make sure your assetDirs array contains the parent folder of your revved file.

<a href="https://github.com/yeoman/grunt-usemin#assetsdirs" rel="nofollow">assetDirs documentation</a>

This is the list of directories where we should start to look for revved version of the assets referenced in the currently looked at file.

usemin: { html: 'build/index.html', options: { assetsDirs: ['foo/bar', 'bar'] } }

Suppose in index.html you have a reference to /images/foo.png, usemin will search for the revved version of /images/foo.png, say /images/foo.12345678.png in any directories in assetsDirs options.

In others words, given the configuration above, usemin will search for the existence of one of these files:

foo/bar/images/foo.12345678.png

bar/images/foo.12345678.png

@Billy Blaze should be able to replace his custom blockReplacements function by updating the assertDirs in his usemin block to be assetsDirs: ['js/dest/rev/test', 'public']

Answer3:

In addition to what Micah said about getting the correct assetsDirs path, you can set the DEBUG variable to actually see what paths are being searched for your files.

If your build task is "build", then you would enter this:

DEBUG=usemin:*,revvedfinder grunt build

That will help to track down exactly what path(s) you need in assetsDirs.

Recommend

  • Bump a specific version number on SVN using Grunt
  • pysvn get latest revision number for specific directory (not head)
  • How to create a project sheet via the python SDK
  • Grunt Shell output to other task
  • How to set the root of git repository to vi/vim find path?
  • The time complexity for a code segment
  • Querying for User Story revisions in Rally
  • Native Browser Automation using Appium 1.2.0.1 on Windows 7 Android real device: Could not find a co
  • MySQL: list of sum for several conditions in one SQL statement
  • How to squash two commits which are actually before a merge commit?
  • Finding a file by its corresponding blob's hash in a git repository?
  • VBA-Json Parse Nested Json
  • NLTK Classifier giving only negative as answer in Sentiment Analysis
  • C#, HTTPClient - IfMatch format requirements?
  • How to Undo Subversion commit
  • fatal error C1083: Cannot open include file: 'Carbon/ Carbon.h': No such file or directory
  • Reset to the very first commit in Git?
  • something very wrong with SESSIONS
  • EIPHTTPProtocolExceptionPeer exception using PutBlock with array of bytes all set to zero
  • Xamarin PCLCrypto SHA256 give different hash
  • How to extract text from a PDF and decode characters?
  • Regex for nested values
  • How gzip file gets stored in HDFS
  • How can I include If-None-Match header in HttpRequestMessage
  • JBoss External Properties Files in Classpath
  • htaccess add www if not subdomain, if subdomain remove www
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • Time complexity of a program which involves multiple variables
  • one Local Olampyad Questions on Informatic in 2011
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • Change div Background jquery
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • How can i traverse a binary tree from right to left in java?