Skip to content

Instantly share code, notes, and snippets.

@alexcrown
Last active August 29, 2015 14:08
Show Gist options
  • Save alexcrown/38ac3baf40dc1cc73885 to your computer and use it in GitHub Desktop.
Save alexcrown/38ac3baf40dc1cc73885 to your computer and use it in GitHub Desktop.
Requirejs + hashres + Grunt
// I found solution for hashres in requireJs module. It based on method onModuleBundleComplete which invoked for each bundle after it created.
// Methods for calc hash used from grunt-task-hashres.
requirejs: {
compile: {
options: {
...
onModuleBundleComplete: function (data) {
var crypto = require('crypto'),
fs = require('fs');
var utils = require("./node_modules/grunt-hashres/tasks/hashresUtils");
var dest = [
'build/**/*.js-temp' // Bundle temporary file. Such files for all bundles exists at this moment
];
var hsh = utils.md5("build/" + data.path).slice(0, 8);
var bundleName = data.name;
var bundleNewName = data.name + "." + hsh + ".cache";
// Substituting references to the given files with the hashed ones.
grunt.file.expand(dest).forEach(function (f) {
var destContents = fs.readFileSync(f, 'utf8');
destContents = destContents.replace(new RegExp(utils.preg_quote(bundleName) + "(\\?[0-9a-z]+)?", "g"), bundleNewName);
fs.writeFileSync(f, destContents, 'utf8');
});
fs.renameSync("build/" + data.path, "build/" + bundleNewName + ".js");
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment