Skip to content

Instantly share code, notes, and snippets.

@MathieuLoutre
Created August 22, 2013 08:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MathieuLoutre/6304468 to your computer and use it in GitHub Desktop.
Save MathieuLoutre/6304468 to your computer and use it in GitHub Desktop.
Compress + AWS S3 upload
grunt.initConfig({
aws: grunt.file.readJSON('aws-keys.json'), // Read the file
compress: {
assets: {
options: {
mode: 'gzip'
},
},
files: [
{expand: true, cwd: "assets/", src: ['**/*.css', '**/*.js'], dest: 'dist/assets/'},
]
},
aws_s3: {
options: {
accessKeyId: "<%= aws.AWSAccessKeyId %>",
secretAccessKey: "<%= aws.AWSSecretKey %>",
region: 'eu-west-1',
},
production: {
options: {
bucket: 'nzk-test-bucket',
},
files: [
{expand: true, cwd: "dist/assets/", src: ['**/*.css', '**/*.js'], params: {ContentEncoding: 'gzip'}},
]
},
},
});
@SimplGy
Copy link

SimplGy commented Sep 2, 2013

This method doesn't set the ContentType. Here's how I did it in production.
Then running grunt aws_s3 will copy the parent files, the static files, and overwrite the static files you specify with gzipped versions and the correct headers.

    aws_s3
      deployParent:
        options: params: CacheControl: 'public,max-age=120000' # 2 minutes (1000 * 60 * 2)
        files: [
          expand: true
          cwd: "<%= grunt.option('targetParent') %>"
          src: '*' # all files, but only those in this immediate directory
          dest: '' # putting a slash here will cause '//path' on Amazon
        ]
      deployStatic: files: [
          expand: true
          cwd: "<%= grunt.option('target') %>"
          src: [ '**', '!**/*.gz' ]
          dest: "<%= grunt.option('targetSubdir') %>"
        ] 
      deployGzipped:
        options: params: ContentEncoding: 'gzip'
        files: [
          { # The Minified CSS file
            expand: true
            cwd: "<%= grunt.option('target') %>"
            src: "<%= cfg.minName %>.css.gz"
            dest: "<%= grunt.option('targetSubdir') %>"
            ext: '.css'
            params: ContentType: 'text/css'
          }
          { # The Javascript package
            expand: true
            cwd: "<%= grunt.option('target') %>"
            src: "core/main.js.gz"
            dest: "<%= grunt.option('targetSubdir') %>"
            ext: '.js'
            params: ContentType: 'application/javascript'
          }
        ]

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