This can be achieved by utilizing grunt's rename
function when building the files object dynamically, instead of using another task.
The documentation describes grunts rename
function as follows:
rename
Embeds a customized function, which returns a string containing the new destination and filename. This function is called for each matched src
file (after extension renaming and flattening).
Inside the body of the rename
function is where you add your custom logic to append a timestamp to each filename.
The following Gruntfile.js
configuration shows how to achieve this:
Gruntfile.js
module.exports = function (grunt) {
var path = require('path'); // Load nodes built-in `path` module.
// Obtain local timestamp formatted as: YYYY-MM-DD-HH-MM-SS
var tzOffset = (new Date()).getTimezoneOffset() * 60000;
var timeStamp = (new Date(Date.now() - tzOffset)).toISOString().slice(0, -1)
.replace(/\.[\w\W]+?$/, '') // Delete from dot to end.
.replace(/\:|\s|T/g, '-'); // Replace colons, spaces, and T with hyphen.
grunt.initConfig({
cssmin: {
timestamp: {
files: [{
expand: true,
cwd: 'css/',
src: ['aw2017.css', 'aw2018.css'],
dest: 'dist/',
/**
* Grunt rename function generates new destination filepath,
# adds timestamp, and new min.css extension to the file name.
# (https://gruntjs.com/configuring-tasks#the-rename-property)
#
* @param {String} dest - The path to the desination directory.
* @param {String} src - The path to the source directory.
* @returns {String} New dest path with time-stamped filename.
*/
rename: function(dest, src) {
var fileExt = path.extname(src),
fileName = path.basename(src, fileExt),
dirName = path.dirname(src),
newFileExt = ['.min', fileExt].join(''),
newFileName = [fileName, '-', timeStamp, newFileExt].join(''),
newDestPath = path.join(dest, dirName, newFileName);
return newDestPath;
}
}]
}
}
});
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.registerTask('default', ['cssmin:timestamp']);
};
Additional info:
Firstly, in the Gruntfile.js
above, we load the nodejs built-in path module via the line reading.
var path = require('path');
This module is used later in the rename
function to help create the new time-stamped filename, and ascertain the destination filepath to be return
'ed:
We then create a local timestamp formatted as YYYY-MM-DD-HH-MM-SS
via the lines reading:
var tzOffset = (new Date()).getTimezoneOffset() * 60000;
var timeStamp = (new Date(Date.now() - tzOffset)).toISOString().slice(0, -1)
.replace(/\.[\w\W]+?$/, '') // Delete from dot to end.
.replace(/\:|\s|T/g, '-'); // Replace colons, spaces, and T with hyphen.
Note: We assign the generated timestamp to the timeStamp
variable outside of any grunt task(s) to ensure all resultant filenames get the same timestamp.
The date/time format will be based on your local timezone and not UTC (Coordinated Universal Time).
We then reconfigure your cssmin
task to build the files object dynamically instead of utilizing the compact format. By configuring the task this way we get access to the rename
function.
Further usage and modifications to the current config:
The Gruntfile.js
configuration provided above takes a two source CSS files, named aw2017.css
and aw2018.css
from the following directory structure:
.
└── css
├── aw2017.css
└── aw2018.css
After running the grunt
command via your CLI, it outputs both minified (time-stamped) .css
files to the new dist
directory. Resulting as this:
.
├── css
│ ├── aw2017.css
│ └── aw2018.css
└── dist
├── aw2017-2018-05-09-08-35-57.min.css
└── aw2018-2018-05-09-08-35-57.min.css
However, if you want to also include the source css
folder in the dist
directory like this:
.
├── css
│ ├── aw2017.css
│ └── aw2018.css
└── dist
└── css
├── aw2017-2018-05-09-08-35-57.min.css
└── aw2018-2018-05-09-08-35-57.min.css
then you need to change the values of the cwd
and src
properties in the cssmin
task to this:
// ...
cwd: '.',
src: ['css/aw2017.css', 'css/aw2018.css'],
// ...
Minifying and time-stamping multiple .css
files using a glob pattern
Currently, in your question, you seem to only want to minify two .css
file, namely aw2017.css
and aw2018.css
.
However, if you wanted to minify (and time-stamp) many .css
files found in the css
directory, however many levels deep, you can utilize a globbing pattern. For example, lets say your source css
directory looks like this:
.
└── css
├── a.css
├── b.css
├── foo
│ ├── bar
│ │ └── c.css
│ └── d.css
└── quux
└── e.css
...and if you change the values of the cwd
and src
properties in your cssmin
task to this:
// ...
cwd: '.',
src: ['css/**/*.css'],
// ...
Your resultant output will be something like this:
.
├── css
│ └── ...
└── dist
└── css
├── a-2018-05-09-08-35-57.min.css
├── b-2018-05-09-08-35-57.min.css
├── foo
│ ├── bar
│ │ └── c-2018-05-09-08-35-57.min.css
│ └── d-2018-05-09-08-35-57.min.css
└── quux
└── e-2018-05-09-08-35-57.min.css