I'm trying to set up a grunt task that outputs a minified css file and changes the file name with a timestamp.
My Gruntfile looks like this:
module.exports = function (grunt) {
//project configurations
grunt.initConfig({
cssmin: {
target: {
src: ["css/aw2018.css", ],
dest: "dist/aw2018.min.css"
}
}
replace: {
foo: {
options: {
variables: {
'timestamp': '<%= new Date().getTime() %>'
},
force: true
},
files: [{
expand: true,
cwd: 'css/',
src: ['*.css/*.js'],
dest: 'dist/',
ext: '.<%= new Date().getTime() %>.js'
}]
}
}
});
//load cssmin plugin
grunt.loadNpmTasks('grunt-contrib-cssmin');
//create default task
grunt.registerTask("default", ["cssmin"]);
grunt.registerTask('default', 'replace');
};
But I get an error of Loading "Gruntfile.js" tasks...ERROR
SyntaxError: Unexpected identifier Warning: Task "default" not found. Use --force to continue.
EDIT:
This is what I'm ultimately trying to achieve:
- Minify a css file
- Add a timestamp to the end of the file name.
I would like to have it work for any css file in a folder but keep them separate. For instance, lets say I have aw2018.css and aw2017.css. I would like both of them to run through the task and then be output to their own individual minified css file with the timestamp of YYYY-MM-DD-HH-MM-SS at the end of the filename.
This can be achieved by utilizing grunt's
renamefunction when building the files object dynamically, instead of using another task.The documentation describes grunts
renamefunction as follows:Inside the body of the
renamefunction is where you add your custom logic to append a timestamp to each filename.The following
Gruntfile.jsconfiguration shows how to achieve this:Gruntfile.js
Additional info:
Firstly, in the
Gruntfile.jsabove, we load the nodejs built-in path module via the line reading.This module is used later in the
renamefunction to help create the new time-stamped filename, and ascertain the destination filepath to bereturn'ed:We then create a local timestamp formatted as
YYYY-MM-DD-HH-MM-SSvia the lines reading:Note: We assign the generated timestamp to the
timeStampvariable 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
cssmintask to build the files object dynamically instead of utilizing the compact format. By configuring the task this way we get access to therenamefunction.Further usage and modifications to the current config:
The
Gruntfile.jsconfiguration provided above takes a two source CSS files, namedaw2017.cssandaw2018.cssfrom the following directory structure:After running the
gruntcommand via your CLI, it outputs both minified (time-stamped).cssfiles to the newdistdirectory. Resulting as this:However, if you want to also include the source
cssfolder in thedistdirectory like this:then you need to change the values of the
cwdandsrcproperties in thecssmintask to this:Minifying and time-stamping multiple
.cssfiles using a glob patternCurrently, in your question, you seem to only want to minify two
.cssfile, namelyaw2017.cssandaw2018.css.However, if you wanted to minify (and time-stamp) many
.cssfiles found in thecssdirectory, however many levels deep, you can utilize a globbing pattern. For example, lets say your sourcecssdirectory looks like this:...and if you change the values of the
cwdandsrcproperties in yourcssmintask to this:Your resultant output will be something like this: