Skip to content

Instantly share code, notes, and snippets.

Created March 14, 2015 19:45
Show Gist options
  • Save anonymous/0242c780b2b92970cad5 to your computer and use it in GitHub Desktop.
Save anonymous/0242c780b2b92970cad5 to your computer and use it in GitHub Desktop.
Grunt workflow to gzip and upload CSS and JS to S3

Dependencies

grunt.loadNpmTasks('grunt-aws');
grunt.loadNpmTasks('grunt-contrib-compress');

Commands

grunt build
grunt compress #gzip assets
grunt s3 #upload to s3
<?php
/* remember, we can load JSON in PHP too */
$vf = fopen(dirname(__FILE__) . '/version.json',"r");
$v = json_decode(fread($vf,filesize(dirname(__FILE__) . '/version.json')));
fclose($vf);
$cssVer = $v->cssVer;
$jsVer = $v->jsVer;
?>
/* manually compress front end assets with gzip for s3 */
initConfig.compress = {
css: {
options: {
mode: 'gzip'
},
src: ['<%= dirs.theme %><%= dirs.assets %><%= dirs.css %>main.min.css'],
dest: '<%= dirs.theme %><%= dirs.assets %><%= dirs.css %>main.min.' + (grunt.option('cssVer')) + '.cssgz'
},
js: {
options: {
mode: 'gzip'
},
src: ['<%= dirs.theme %><%= dirs.assets %><%= dirs.js %>main-min.js'],
dest: '<%= dirs.theme %><%= dirs.assets %><%= dirs.js %>main-min.' + (grunt.option('jsVer')) + '.jsgz'
}
};
if(doS3) {
/* upload our gzipped CSS and JS zip files to S3 with appropriate headers */
initConfig.aws = grunt.file.readJSON('aws-secret.json');
initConfig.s3 = {
options: {
accessKeyId: "<%= aws.accessKeyId %>",
secretAccessKey: "<%= aws.secretAccessKey %>",
bucket: "markuptips"
},
cssgz: {
cwd:'<%= dirs.theme %><%= dirs.assets %><%= dirs.css %>',
src:'main.min.' + (grunt.option('cssVer')) + '.cssgz',
dest:'<%= dirs.assets %><%= dirs.css %>',
options:{
gzip:false, // did it ourselves
headers:{
ContentType:'text/css',
CacheControl:31556926,
Expires: new Date(new Date().setYear(new Date().getFullYear() + 1)),
ContentEncoding:'gzip'
}
}
},
jsgz: {
cwd:'<%= dirs.theme %><%= dirs.assets %><%= dirs.js %>',
src:'main-min.' + (grunt.option('jsVer')) + '.jsgz',
dest:'<%= dirs.assets %><%= dirs.js %>',
options:{
gzip:false, // did it ourselves
headers:{
ContentType:'application/javascript',
CacheControl:31556926,
Expires: new Date(new Date().setYear(new Date().getFullYear() + 1)),
ContentEncoding:'gzip'
}
}
}
}
}
@jpdevries
Copy link

Example version.json

{
  "cssVer": "0.0.9",
  "jsVer": "0.0.15"
}

Example aws-secret.json

{
"accessKeyId":"*",
"secretAccessKey":"*"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment