Permalink
Browse files

feat(task) create grunt userev task

  • Loading branch information...
1 parent 8ef4875 commit d9f515c4b74c980231f006dfdaaf4727ccd42aae @iki iki committed Nov 21, 2013
Showing with 403 additions and 2 deletions.
  1. +3 −0 .gitignore
  2. +20 −0 .jshintrc
  3. +69 −0 Gruntfile.js
  4. +125 −2 README.md
  5. +40 −0 package.json
  6. +109 −0 tasks/userev.js
  7. +37 −0 test/userev_test.js
View
3 .gitignore
@@ -0,0 +1,3 @@
+node_modules
+npm-debug.log
+tmp
View
20 .jshintrc
@@ -0,0 +1,20 @@
+{
+ "strict": true,
+ "camelcase": true,
+ "indent": 2,
+ "undef": true,
+ "quotmark": "single",
+ "maxlen": 100,
+ "trailing": true,
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "sub": true,
+ "boss": true,
+ "eqnull": true,
+ "node": true,
+ "es5": true
+}
View
69 Gruntfile.js
@@ -0,0 +1,69 @@
+/*
+ * grunt-runtask
+ * https://github.com/salsita/grunt-runtask
+ *
+ * Copyright (c) 2013 Salsita Software
+ * Licensed under the MIT license.
+ */
+
+'use strict';
+
+module.exports = function (grunt) {
+ // load all npm grunt tasks
+ require('load-grunt-tasks')(grunt);
+
+ // Project configuration.
+ grunt.initConfig({
+ jshint: {
+ all: [
+ 'Gruntfile.js',
+ 'tasks/*.js',
+ '<%= nodeunit.tests %>'
+ ],
+ options: {
+ jshintrc: '.jshintrc',
+ reporter: require('jshint-stylish')
+ }
+ },
+
+ // Before generating any new files, remove any previously-created files.
+ clean: {
+ tests: ['tmp']
+ },
+
+ // Unit tests.
+ nodeunit: {
+ tests: ['test/*_test.js']
+ },
+
+ // Watch sources and run tests.
+ delta: {
+ js: {
+ files: ['test/**', 'tasks/*.js', 'Gruntfile.js'],
+ tasks: ['jshint', 'test'],
+ },
+ jshintrc: {
+ files: ['.jshintrc'],
+ tasks: ['jshint'],
+ },
+ },
+ });
+
+ // Actually load this plugin's task(s).
+ grunt.loadTasks('tasks');
+
+ // By default, lint and run all tests.
+ grunt.registerTask('default', ['jshint', 'test']);
+
+ // On watch, run both default and delta (original watch).
+ grunt.renameTask('watch', 'delta');
+ grunt.registerTask('watch', ['default', 'delta']);
+
+ // Whenever the "test" task is run, first clean the "tmp" dir, then run this
+ // plugin's task(s), then test the result.
+ grunt.registerTask('test', ['clean', 'userevTest', 'nodeunit']);
+
+ grunt.registerTask('userevTest', [
+ ]);
+
+};
View
127 README.md
@@ -1,2 +1,125 @@
-grunt-userev
-============
+# grunt-userev [![Build Status](https://secure.travis-ci.org/salsita/grunt-userev.png?branch=master)](https://travis-ci.org/salsita/grunt-userev) [![npm version](https://badge.fury.io/js/grunt-userev.png)](https://npmjs.org/package/grunt-userev) [![Dependency Status](https://david-dm.org/salsita/grunt-userev.png)](https://david-dm.org/salsita/grunt-userev) [![](#devDependency Status#https://david-dm.org/salsita/grunt-userev/dev-status.png)](https://david-dm.org/salsita/grunt-userev#info=devDependencies) <a href='https://github.com/salsita'><img align='right' title='Salsita' src='https://www.google.com/a/cpanel/salsitasoft.com/images/logo.gif?alpha=1' _src='https://1.gravatar.com/avatar/d413290a5fe1385efcf5a344d4a0b588?s=50' /></a>
+
+<a href='https://npmjs.org/package/grunt-userev'><img align='right' alt='npm' title='npm info' src='https://nodei.co/npm/grunt-userev.png?compact=true' /></a>
+
+> Update references to assets versioned with [grunt-filerev](https://github.com/yeoman/grunt-filerev). Leaner and faster than [grunt-usemin](https://github.com/yeoman/grunt-usemin), and able to update already versioned references.
+
+
+## Getting Started
+
+This plugin requires [Grunt](http://gruntjs.com).
+
+If you haven't used Grunt before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
+
+```shell
+npm install grunt-userev --save-dev
+```
+
+Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
+
+```js
+grunt.loadNpmTasks('grunt-userev');
+```
+
+
+## The "userev" task
+
+### Overview
+
+In your project's Gruntfile, configure `userev` to update references to assets versioned by [grunt-filerev](https://github.com/yeoman/grunt-filerev). It will automatically use [`grunt.filerev.summary`](https://github.com/yeoman/grunt-filerev#summary).
+
+
+```js
+grunt.initConfig({
+ filerev: {
+ options: {
+ encoding: 'utf8',
+ algorithm: 'md5',
+ length: 8
+ },
+ sourcemaps: { // Rename sourcemaps.
+ src: ['build/css/*.map.css', 'build/js/*.map.js'],
+ },
+ assets: { // Rename minified js/css.
+ src: ['build/css/*.css', 'build/js/*.js', '!build/css/*.map.css', '!build/js/*.map.js'],
+ },
+ },
+
+ userev: {
+ options: {
+ hash: /(\.[a-f0-9]{8})\.[a-z]+$/,
+ },
+ assets: { // Link to sourcemaps in minified js/css.
+ src: ['build/css/*.css', 'build/js/*.js', '!build/css/*.map.css', '!build/js/*.map.js'],
+ options: {
+ patterns: {
+ 'Linking versioned source maps': /sourceMappingURL=([a-z0-9.]*\.map)/,
+ },
+ },
+ },
+ index: { // Link to minified js/css in index html.
+ src: 'build/index.html',
+ options: {
+ patterns: {
+ 'Linking versioned assets': /(css\/[a-z0-9.]*\.css)/,
+ },
+ },
+ },
+ },
+});
+
+grunt.registerTask( 'rev', [
+ 'filerev:sourcemaps', // Rename sourcemaps.
+ 'userev:assets', // Link to sourcemaps in minified js/css.
+ 'filerev:assets', // Rename minified js/css.
+ 'userev:index', // Link to minified js/css in index html.
+]);
+
+```
+
+
+### Options
+
+#### patterns
+Type: `Object {Key: RegExp}`
+Default: none
+
+Reference RegExp patterns are matched in source files. The first matched group is checked if it matches ending of any key in [`grunt.filerev.summary`](https://github.com/yeoman/grunt-filerev#summary). The key names are used to log processing of matched patterns.
+
+#### hash
+Type: `RegExp`
+Default: none
+
+If specified, the RegExp pattern is matched against matched references and the first matched group (the hash in filename) is removed. This allows to update the references in source files multiple times without need to regenerate them.
+
+
+## Roadmap
+
+- [ ] Add changelog
+- [ ] Extend tests
+
+
+## Contributing
+
+Welcome to the project. Choose a way that suits you. You'll need a [GitHub account](https://github.com/signup/free).
+
+### Submit a bug, or feature request
+
+* Search [existing issues](https://github.com/salsita/grunt-userev/issues) to avoid duplicities.
+* [Submit an issue](https://github.com/salsita/grunt-userev/issues/new) with label `bug`, or `enhancement`.
+* For a bug, include any relevant information, e.g. task output, installed OS/Node.js/Grunt/grunt-userev versions, and steps to reproduce.
+
+### Submit a pull request
+
+* [Fork the repository](https://github.com/salsita/grunt-userev/fork) ([help](https://help.github.com/articles/fork-a-repo)) and checkout new branch prefixed with either `feature-`, or `fix-`, or `docs-`, or `chore-`.
+* [Use](https://github.com/salsita/grunt-userev/blob/master/.jshintrc) the [popular JavaScript style convention](http://sideeffect.kr/popularconvention#javascript) and [winning JavaScript style](http://seravo.fi/2013/javascript-the-winning-style) with exception of 100 characters per line. In short, use 2 spaces indent, camelCase names, trailing comma, single quotes, semicolons, sparse spacing, and no trailing whitespace.
+* For a bugfix, write a test that exposes the bug and fails before applying your fix. Submit the test alongside the fix.
+* For a new feature, add tests that cover the feature.
+* Lint and test your code by running `grunt`, or `grunt watch` to watch source files and run tests on any change.
+* Use commit messages suitable for generating [changelog](https://github.com/salsita/grunt-userev/blob/master/CHANGELOG.md) and following [Karma/Angular commit message convention](https://github.com/karma-runner/karma/blob/master/docs/dev/04-git-commit-msg.md) ([docs](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#)). In short, use `<type>(<scope>): <subject>` header in imperative present tense, message body describing motivation/differences, and footer referencing related issues and breaking changes.
+* [Create a pull request](https://github.com/salsita/grunt-userev/compare) ([help](https://help.github.com/articles/creating-a-pull-request)) to [grunt-userev](https://github.com/salsita/grunt-userev) [master](https://github.com/salsita/grunt-userev/branches) branch.
+
+
+## License
+
+Copyright 2013 [Salsita Software](http://salsitasoft.com). Licensed under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
View
40 package.json
@@ -0,0 +1,40 @@
+{
+ "name": "grunt-userev",
+ "version": "0.0.1",
+ "description": "Update references to assets versioned with grunt-filerev. Leaner and faster than grunt-usemin, and able to update already versioned references.",
+ "homepage": "https://github.com/salsita/grunt-userev",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/salsita/grunt-userev.git"
+ },
+ "bugs": "https://github.com/salsita/grunt-userev/issues",
+ "author": {
+ "name": "Salsita Software",
+ "email": "",
+ "url": "http://www.salsitasoft.com/"
+ },
+ "keywords": [
+ "gruntplugin"
+ ],
+ "main": "Gruntfile.js",
+ "engines": {
+ "node": ">= 0.8.0"
+ },
+ "licenses": [
+ {
+ "type": "MIT"
+ }
+ ],
+ "devDependencies": {
+ "jshint-stylish": "~0.1.3",
+ "grunt-contrib-clean": "~0.4.0",
+ "grunt-contrib-jshint": "~0.2.0",
+ "grunt-contrib-nodeunit": "~0.1.2",
+ "grunt-contrib-watch": "~0.5.3",
+ "load-grunt-tasks": "~0.2.0",
+ "grunt": "~0.4.0rc2"
+ },
+ "scripts": {
+ "test": "grunt test"
+ }
+}
View
109 tasks/userev.js
@@ -0,0 +1,109 @@
+/*
+ * grunt-runtask
+ * https://github.com/salsita/grunt-runtask
+ *
+ * Copyright (c) 2013 Salsita Software
+ * Licensed under the MIT license.
+ */
+
+'use strict';
+
+module.exports = function (grunt) {
+
+ /** @
+ * The `userev` task updates references to assets versioned with `filerev`.
+ *
+ * It uses `grunt.filerev.summary`.
+ */
+ function reEscape(s) { // http://stackoverflow.com/a/18620139/899047
+ return s.replace(/[$-\/?[-^{|}]/g, '\\$&');
+ }
+
+ function endsWith(s, suffix) { // http://stackoverflow.com/a/2548133/899047
+ return s.indexOf(suffix, s.length - suffix.length) !== -1;
+ }
+
+ function replaceFirstGroup(s, pattern, replacement) {
+ var match = pattern.exec(s);
+ if (match) {
+ return s.replace(pattern, match[0].replace(match[1] || match[0], replacement));
+ } else {
+ return s;
+ }
+ }
+
+ grunt.task.registerMultiTask('userev', 'Update versioned assets references.', function() {
+ var path = require('path');
+ var sep = '/';
+ var options = this.options();
+ var versioned = grunt.filerev && grunt.filerev.summary;
+
+ if (versioned && path.sep !== sep) {
+ var re = new RegExp(reEscape(path.sep), 'g');
+ for (var assetpath in versioned) {
+ versioned[assetpath.replace(re, sep)] = versioned[assetpath].replace(re, sep);
+ delete versioned[assetpath];
+ }
+ }
+
+ grunt.log.debug(this.nameArgs + ': ' + JSON.stringify(this.files, null, 4) +
+ JSON.stringify(options, null, 4));
+ grunt.log.debug('filerev.summary: ' + JSON.stringify(versioned, null, 4));
+
+ if (versioned) {
+ this.files.forEach(function(file) {
+ file.src.filter(function(filepath) {
+ if (!grunt.file.exists(filepath)) {
+ grunt.log.warn('Source file "' + filepath + '" not found.');
+ return false;
+ } else {
+ return true;
+ }
+ }).forEach(function(filepath) {
+ var content = grunt.file.read(filepath);
+ var updated = false;
+ var replacement, lastLink, baseLink, hashLink;
+
+ for (var label in options.patterns) {
+ var pattern = options.patterns[label];
+ var match = pattern.exec(content);
+ if (match) {
+ grunt.log.debug('Matching ' + [filepath, pattern, JSON.stringify(match)].join(': '));
+ replacement = match[0];
+ lastLink = match[1] || match[0];
+ baseLink = options.hash ? replaceFirstGroup(lastLink, options.hash, '') : lastLink;
+ for (var assetpath in versioned) {
+ if (endsWith(assetpath, baseLink)) {
+ if (!updated) {
+ grunt.log.writeln('Updating ' + filepath.cyan +
+ (file.dest ? ' -> ' + file.dest.cyan : '.'));
+ }
+ hashLink = versioned[assetpath].slice(assetpath.length - baseLink.length);
+ if (lastLink !== hashLink) {
+ grunt.log.writeln('Linking ' + label + ': ' + lastLink +
+ (baseLink !== lastLink ? ' -> ' + baseLink : '') + ' -> ' + hashLink.green);
+ replacement = replacement.replace(lastLink, hashLink);
+ content = content.replace(pattern, replacement);
+ updated = true;
+ } else {
+ grunt.log.writeln('Already linked ' + label + ': ' +
+ baseLink + ' -> ' + hashLink.green);
+ }
+ break;
+ } else {
+ grunt.log.debug('No match: ' + lastLink +
+ (baseLink !== lastLink ? ' -> ' + baseLink : '') + ' <> ' + assetpath);
+ }
+ }
+ } else {
+ grunt.log.debug('Not matching ' + filepath + ': ' + pattern);
+ }
+ }
+ if (updated) {
+ grunt.file.write(file.dest || filepath, content);
+ }
+ });
+ });
+ }
+ });
+};
View
37 test/userev_test.js
@@ -0,0 +1,37 @@
+'use strict';
+
+var grunt = require('grunt');
+
+/*
+ ======== A Handy Little Nodeunit Reference ========
+ https://github.com/caolan/nodeunit
+
+ Test methods:
+ test.expect(numAssertions)
+ test.done()
+ Test assertions:
+ test.ok(value, [message])
+ test.equal(actual, expected, [message])
+ test.notEqual(actual, expected, [message])
+ test.deepEqual(actual, expected, [message])
+ test.notDeepEqual(actual, expected, [message])
+ test.strictEqual(actual, expected, [message])
+ test.notStrictEqual(actual, expected, [message])
+ test.throws(block, [error], [message])
+ test.doesNotThrow(block, [error], [message])
+ test.ifError(value)
+*/
+
+exports.runtaskTest = {
+ setUp: function (done) {
+ // setup here if necessary
+ done();
+ },
+ someTest: function (test) {
+ test.expect(1);
+
+ test.equal(1, 1, 'should ...');
+
+ test.done();
+ },
+};

0 comments on commit d9f515c

Please sign in to comment.