Skip to content

New features added. Updated Node compatibility. #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 75 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

Object.assign = require('object-assign')

module.exports = function(grunt) {

var _fileGlobSync;
Expand Down Expand Up @@ -49,6 +51,16 @@ module.exports = function(grunt) {
'tmp/js/js_bundle_b.js': ['test/fake/file3.js', 'test/fake/file4.js']
}
},
options_tag_date_rename: {
options: {
tag : 'date',
renameToVersion: true
},
files: {
'tmp/js/js_bundle_a.js': ['test/fake/file1.js', 'test/fake/file2.js'],
'tmp/js/js_bundle_b.js': ['test/fake/file3.js', 'test/fake/file4.js']
}
},
options_dateFormat: {
options: {
tag : 'date',
Expand Down Expand Up @@ -145,19 +157,74 @@ module.exports = function(grunt) {
versionsMapTrimPath : 'tmp/options_output_trim/'
}
},
options_versionsMapTemplate: {
options_versionsMapTemplateFail: {
files: [{
expand : true,
cwd : "test/fixtures/images/",
src : ['**/*.png', '**/*.jpg', '**/*.gif'],
dest : "tmp/options_versionsMapTemplate/"
}],
options: {
versionsMapFile : 'tmp/options_versionsMapTemplateFail.php',
versionsMapTemplate : 'test/fixtures/templates/php.tpl.missing',
versionsMapTrimPath : 'tmp/options_versionsMapTemplate/'
}
},
options_versionsMapTemplateFile: {
files: [{
expand : true,
cwd : "test/fixtures/images/",
src : ['**/*.png', '**/*.jpg', '**/*.gif'],
dest : "tmp/options_versionsMapTemplate/"
}],
options: {
versionsMapFile : 'tmp/options_versionsMapTemplate.php',
versionsMapFile : 'tmp/options_versionsMapTemplateFile.php',
versionsMapTemplate : 'test/fixtures/templates/php.tpl',
versionsMapTrimPath : 'tmp/options_versionsMapTemplate/'
}
},
options_versionsMapTemplateString: {
files: [{
expand : true,
cwd : "test/fixtures/images/",
src : ['**/*.png', '**/*.jpg', '**/*.gif'],
dest : "tmp/options_versionsMapTemplate/"
}],
options: {
delimiters : 'altDelimiters',
versionsMapFile : 'tmp/options_versionsMapTemplateString.php',
versionsMapTemplate : encodeURI('<?php\r\n\r\nclass MyDict\r\n{\r\n public static $myDict = array(\r\n<% _.forEach(files, function(file) { %>\r\n \"<%= file.originalPath %>\" => \"<%= file.versionedPath %>\",\r\n<% }); %>\r\n );\r\n}\r\n'),
versionsMapTrimPath : 'tmp/options_versionsMapTemplate/'
}
},
options_versionsMapTemplateFunction: {
files: [{
expand : true,
cwd : "test/fixtures/images/",
src : ['**/*.png', '**/*.jpg', '**/*.gif'],
dest : "tmp/options_versionsMapTemplate/"
}],
options: {
delimiters : 'altDelimiters',
versionsMapFile : 'tmp/options_versionsMapTemplateFunction.php',
versionsMapTemplate : function () { return encodeURI('<?php\r\n\r\nclass MyDict\r\n{\r\n public static $myDict = array(\r\n<% _.forEach(files, function(file) { %>\r\n \"<%= file.originalPath %>\" => \"<%= file.versionedPath %>\",\r\n<% }); %>\r\n );\r\n}\r\n'); },
versionsMapTrimPath : 'tmp/options_versionsMapTemplate/'
}
},
options_versionsMapDataFile: {
files: [{
expand : true,
cwd : "test/fixtures/images/",
src : ['**/*.png', '**/*.jpg', '**/*.gif'],
dest : "tmp/options_versionsMapDataFile/"
}],
options: {
delimiters : 'altDelimiters',
versionsMapDataFile : 'tmp/options_versionsMapDataFile.json',
versionsMapFilesAutoDelete : true,
versionsMapTrimPath : 'tmp/options_versionsMapDataFile/'
}
},
files_compact_format: {
src: [
'test/fixtures/js/file1.js',
Expand Down Expand Up @@ -427,6 +494,7 @@ module.exports = function(grunt) {
'clean',
'startMocking',
'assets_versioning:options_tag_date',
'assets_versioning:options_tag_date_rename',
'assets_versioning:options_dateFormat',
'assets_versioning:options_timezoneOffset',
'stopMocking',
Expand All @@ -439,7 +507,11 @@ module.exports = function(grunt) {
'assets_versioning:options_post_internal',
'assets_versioning:options_versionsMapFile',
'assets_versioning:output_versionsMapTrimPath',
'assets_versioning:options_versionsMapTemplate',
'assets_versioning:options_versionsMapTemplateFail',
'assets_versioning:options_versionsMapTemplateFile',
'assets_versioning:options_versionsMapTemplateString',
'assets_versioning:options_versionsMapTemplateFunction',
'assets_versioning:options_versionsMapDataFile',
'assets_versioning:files_compact_format',
'assets_versioning:files_object_format',
'assets_versioning:files_array_format',
Expand Down
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# grunt-assets-versioning [![Build Status](https://travis-ci.org/theasta/grunt-assets-versioning.svg?branch=master)](https://travis-ci.org/theasta/grunt-assets-versioning) [![NPM version](https://badge.fury.io/js/grunt-assets-versioning.svg)](http://badge.fury.io/js/grunt-assets-versioning)
[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/theasta/grunt-assets-versioning?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
# grunt-assets-versioning
[![Build Status](https://travis-ci.org/theasta/grunt-assets-versioning.svg?branch=master)](https://travis-ci.org/theasta/grunt-assets-versioning) [![GitHub version](https://badge.fury.io/gh/theasta%2Fgrunt-assets-versioning.svg)](https://badge.fury.io/gh/theasta%2Fgrunt-assets-versioning) [![NPM version](https://badge.fury.io/js/grunt-assets-versioning.svg)](http://badge.fury.io/js/grunt-assets-versioning) [![dependencies](https://david-dm.org/theasta/grunt-assets-versioning.svg)](https://david-dm.org/theasta/grunt-assets-versioning) [![devDependencies](https://david-dm.org/theasta/grunt-assets-versioning/dev-status.svg)](https://david-dm.org/theasta/grunt-assets-versioning?type=dev) [![peerDependencies](https://david-dm.org/theasta/grunt-assets-versioning/peer-status.svg)](https://david-dm.org/theasta/grunt-assets-versioning?type=peer) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/theasta/grunt-assets-versioning?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

> Versioning static assets with Grunt

Expand Down Expand Up @@ -55,6 +55,12 @@ Default value: `8`

If you choose to version your files using a hash, hashLength let you set how long the hash is going to be.

#### options.renameToVersion
Type: `Boolean`
Default value: `false`

Should the hash replace the file name before the final extension, or (default) should it be just be appended before the final extension?

#### options.dateFormat
Type: `String`
Default value: `YYYYMMDDHHmmss`
Expand Down Expand Up @@ -99,17 +105,17 @@ By default you can retrieve the map of versions by accessing this configuration
The versionsMapFile gives you the possibility to also output that map to a file.

#### options.versionsMapTemplate
Type: `String`
Type: `String` or `Function: String`
Default value: `null`

Path to a lodash template file that is going to be used to generate the versions map file (options.versionsMapFile)
Path to a lodash template file or URL-encoded string, optionally returned by a function, that is going to be used to generate the versions map file (options.versionsMapFile).

By default, when no template is indicated, the task will output a json file.
By default, when no valid template is indicated, the task will output a json file.

The lo-dash template may reuse the keys from the version maps (version, originalPath, versionedPath).
Here's an example of a lo-dash template to generate a php dictionary.

```php
```
<?php

class MyDict
Expand All @@ -121,13 +127,26 @@ class MyDict
);
```

#### options.versionsMapDataFile
Type: `String`

Specifying a file here will store the internal version data in this file for use on subsequent runs.

#### options.versionsMapFilesAutoDelete
Type: `Boolean`
Default value: `false`

When generating a new hashed file, delete the old one.

_NOTE: This depends on `options.versionsMapDataFile` being set._

#### options.versionsMapTrimPath
Type: `String`
This gives you the possibility to trim the path output in the version map.

For example, if you set options.versionsMapTrimPath to be 'super/long/path/to/', instead of getting this map:

```
```js
[
{
version: '3d04f375',
Expand All @@ -145,7 +164,7 @@ For example, if you set options.versionsMapTrimPath to be 'super/long/path/to/',
you will get this one:


```
```js
[
{
version: '3d04f375',
Expand Down Expand Up @@ -176,7 +195,7 @@ Combined with the skipExisting option, it allows to speed up the deployment proc

If ever you're trying to version a task that doesn't expose all its source files but only an entry point (less, requirejs), you should set the post options to true.

```
```js
less: {
production: {
files: {
Expand Down Expand Up @@ -238,6 +257,7 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.

## Release History

* 2017-09-07 v1.1.0 Add support for templates from strings, persisting version data between runs, deleting old hashed files, and renaming target files instead of append hash. Also include file name in hash.
* 2015-01-29 v1.0.4 Fix MomentJS warning
* 2015-01-28 v1.0.3 Bug fixing.
* 2014-11-22 v1.0.2 Add a post mode which generates the version hash based on the destination files and not the source files.
Expand Down
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-assets-versioning",
"description": "Versioning static assets with Grunt",
"version": "1.0.6",
"version": "1.1.3",
"homepage": "https://github.com/theasta/grunt-assets-versioning",
"author": {
"name": "Alexandrine Boissière",
Expand All @@ -22,19 +22,21 @@
],
"main": "Gruntfile.js",
"engines": {
"node": ">= 0.10.0"
"node": ">= 0.12.0"
},
"scripts": {
"test": "grunt test"
},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-clean": "~0.4.1",
"grunt-contrib-concat": "~0.2.0",
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt": "^0.4.5",
"grunt-cli": "^1.2.0",
"grunt-contrib-clean": "~1",
"grunt-contrib-concat": "~1",
"grunt-contrib-jshint": "~1",
"grunt-contrib-nodeunit": "~1",
"grunt-jsdoc": "^0.6.1",
"mock-fs": "^2.3.1"
"mock-fs": "~4",
"nodeunit": "^0.11.0"
},
"peerDependencies": {
"grunt": ">=0.4.0"
Expand All @@ -43,8 +45,9 @@
"gruntplugin"
],
"dependencies": {
"lodash": "^2.4.1",
"moment": "^2.9.0",
"lodash": "^4",
"moment": "^2.18.1",
"object-assign": "^4.1.1",
"slash": "^1.0.0"
}
}
10 changes: 5 additions & 5 deletions tasks/assets_versioning.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var path = require('path');
var versionerFactory = require('./versioners/versionerFactory');
var _ = require('lodash');
var taggers = require('./taggers');

module.exports = function(grunt) {

Expand All @@ -24,11 +25,10 @@ module.exports = function(grunt) {
encoding: 'utf8',
dateFormat: 'YYYYMMDDHHmmss',
timezoneOffset: 0,
versionize: function(destPath, version) {
versionize: function(destPath, version, onlyVersion) {
return path.dirname(destPath) +
path.sep +
path.basename(destPath, path.extname(destPath)) +
'.'+
(onlyVersion ? '' : path.basename(destPath, path.extname(destPath)) + '.') +
version +
path.extname(destPath);
},
Expand All @@ -41,8 +41,8 @@ module.exports = function(grunt) {
runTask: true
});

if (!_.contains(['hash', 'date'], options.tag)) {
grunt.fail.warn('Invalid argument : options.tag should be equal to date or hash', 1);
if (!_.includes(Object.keys(taggers), options.tag)) {
grunt.fail.warn('Invalid argument : options.tag should be one of: ' + Object.keys(taggers).join(', '), 1);
}

if (options.post && options.skipExisting) {
Expand Down
20 changes: 12 additions & 8 deletions tasks/taggers/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ var fs = require('fs');
* @returns {string}
*/
module.exports = function (src, options) {
var hash = '';
'use strict';
var hash = crypto.createHash('md5');

src.forEach(function(f){
hash += crypto.createHash('md5').update(fs.readFileSync(f, options.encoding)).digest('hex');
var digest = null;

src.forEach(function (f) {
var input = fs.readFileSync(f, options.encoding);
hash.update(input, 'binary');
hash.update(f, 'binary');
});
if (src.length > 1){
hash = crypto.createHash('md5').update(hash).digest('hex');
}

var hashLength = parseInt(options.hashLength, 10);
digest = hash.digest('hex');

// can't use typeof since typeof NaN == 'number'
if (Object.prototype.toString.call(hashLength) === '[object Number]') {
hash = hash.substr(0, options.hashLength);
digest = digest.substr(0, hashLength);
}

return hash;
return digest;
};
Loading