node-s3-cdn v0.4.1
node-s3-cdn
A simple Node.js library for versioning and uploading your assets to S3, then pointing to those assets in your views, stylesheets, and scripts.
For the TL;DR version see /test/gulpfile.js
Express, Gulp, Handlebars, and Sass are used throughout, but helpers and examples for other libs are more than welcome.
##Configuration
Instantiate the cdn with your s3 credentials and config options.
Then set your Sass up to compile in Gulp (or whatever):
...or set your Handlebars up to compile in Express (or whatever):
##Using template helpers
If you write Sass like this:
It will come out like this when NODE_ENV === 'production'
:
And like this otherwise:
If you write Handlebars like this:
It will come out like this when NODE_ENV === 'production'
:
And like this otherwise:
##Uploading assets to S3
To generate a new version hash and upload the contents of ./asset_folder/*
to your_s3_bucket/version_hash/*
:
Remember when you set hashFile: __dirname + '/your_asset_version_hash_will_be_stored_here.json'
above? That file will be updated whenever you run cdn.upload
to match the new version hash, which will be the name of the tag on S3 that your assets are now stored under. This file is how your template helpers will know how to write out full cdn urls.
From there you can configure Cloudfront to point to your S3 bucket and serve assets from https://your.cdn.domain.com
.
##Versioning assets
To create a new version of your assets, run:
You can run cdn.upload
as many times as you want on the same version, only changed files will be overwritten.
##Cleaning up previous versions
IMPORTANT CAVEAT: cdn.clean
will delete every other tag in your s3 bucket except the current asset version.
##Let's go over that again because this is very important
WHEN YOU RUN CDN.CLEAN IT WILL DELETE EVERY OTHER TAG IN YOUR S3 BUCKET EXCEPT THE CURRENT ASSET VERSION.
MAKE A SEPARATE S3 BUCKET SO YOU CAN VERSION YOUR CDN ASSETS. SERIOUSLY.
##Contributing This is a very early version, feel free to file issues and pull requests for bug reports / suggestions / tests / etc.