Browse files

added support for asset hosts

  • Loading branch information...
1 parent ab3b6ed commit 9a38b76aa8462f056aae1947cb44c44e48ac6f1c @clarkdave committed May 3, 2013
Showing with 44 additions and 9 deletions.
  1. +2 −1 .gitignore
  2. +8 −1 README.md
  3. +9 −1 examples/express/README.md
  4. +4 −0 examples/express/app.js
  5. +21 −6 lib/connect-mincer.js
View
3 .gitignore
@@ -1,2 +1,3 @@
.DS_Store
-node_modules
+node_modules
+.tern-port
View
9 README.md
@@ -73,6 +73,10 @@ Mincer and this middleware are unopinionated about where your keep your assets.
- A list of directories where your assets are located.
- **mountPoint** *(optional)*
- This is what the js, css and asset_path helpers use to create the URL for each asset. Defaults to `/assets`.
+- **assetHost** *(optional)*
+ - If specified, the view helpers will generate urls of the form `assetHost + mountPoint + asset`. E.g. `//j2938fj.cloudfront.net/assets/layout.css`
+ - You should specify the protocol, i.e. `http://`, `https://` or `//`
+ - This can be used to serve assets from a CDN like Cloudfront
- **precompileList** *(optional)*
- A list of assets to precompile in development. Defaults to `['*', '*/**']`.
- If you have a lot of individual assets which are bundled together, you may want to make this list more specific, otherwise precompiles will be slow and you may get errors (for example, if using Bootstrap, you'll get compile errors with certain .less files that are intended to be @imported only).
@@ -221,12 +225,15 @@ will first be processed by EJS (resolving things like `<%= version() %>`) and th
All feedback or contributions are welcome!
+# Changelog
+
+- *2013-05-03*: added `assetHost` option for serving assets from a specific host
+
# TODO
- add tests
- add built-in helpers like Rails & Sprockets, like:
- asset-path, asset-data-uri, image
-- allow use of a remote domain (e.g. Amazon S3) in production for helper outputs
# Licence
View
10 examples/express/README.md
@@ -62,4 +62,12 @@ In a production environment, you'd probably want to disable the connectMincer.cr
}
}
-This is a basic example but should give you a good idea. If nginx is compiled with the `HttpGzipStaticModule` it will serve the `.gz` files from the compiled asset directory.
+This is a basic example but should give you a good idea. If nginx is compiled with the `HttpGzipStaticModule` it will serve the `.gz` files from the compiled asset directory.
+
+If you want to serve assets from a different domain (like a CDN), you should pass the `assetHost` option:
+
+``` javascript
+assetHost: '//assets.example.com'
+```
+
+Now the view helpers will output URLs like: `//assets.example.com/assets/account/home.css`
View
4 examples/express/app.js
@@ -11,6 +11,10 @@ var app = express();
var mincer = new ConnectMincer({
root: __dirname,
production: env === 'production' || env === 'staging',
+ // uncomment to have view helpers generate urls of the form: //assets.example.com/assets/...
+ // assetHost: '//assets.example.com',
+ // you'll probably want to get this from a environment-specific config, e.g:
+ // assetHost: config.get('asset_host')
mountPoint: '/assets',
manifestFile: __dirname + '/public/assets/manifest.json',
paths: [
View
27 lib/connect-mincer.js
@@ -11,6 +11,7 @@ var ConnectMincer = (function() {
this.production = opts.production === true ? true : false;
this.options = opts;
+ this._setAssetHost(opts.assetHost);
this._setMountPoint(opts.mountPoint);
this._createEnvironment(opts.root, opts.paths);
this._setPrecompileList(opts.precompileList);
@@ -73,6 +74,24 @@ var ConnectMincer = (function() {
};
/**
+ * Set the asset host if the provided assetHost is not null or an empty string.
+ *
+ * @param {[type]} assetHost [description]
+ */
+ ConnectMincer.prototype._setAssetHost = function(assetHost) {
+
+ this.assetHost = null;
+
+ if (assetHost && assetHost.length > 0) {
+ this.assetHost = assetHost;
+
+ // if (assetHost.substr(0, 2) !== '//' && assetHost.substr(0, 7) !== 'http://' && assetHost.substr(0, 8) !== 'https://') {
+ // throw new Error("Asset host must start with '//', 'http://' or 'https://");
+ // }
+ }
+ };
+
+ /**
* Create a new Mincer Environment with the provided root and paths. If the root does
* not exist an error will be thrown. In production, this will create a cached version
* of the environment.
@@ -161,7 +180,7 @@ var ConnectMincer = (function() {
};
ConnectMincer.prototype._toAssetUrl = function(source) {
- return this.mountPoint + '/' + source;
+ return (this.assetHost ? this.assetHost : '') + this.mountPoint + '/' + source;
};
/**
@@ -304,13 +323,9 @@ var ConnectMincer = (function() {
if (!self.production) {
// if we're not in production, we should make sure all assets are precompiled
- // on a request as they won't exist otherwise. Rather than specify a list, we're
- // just precompiling all the things, though we could have the app give us a list
- // of specific things to precompile if this is too slow or whatever
+ // on a request as they won't exist otherwise
self.environment.precompile(self.precompileList, function(err) {
if (err) {
- // throw new Error("Something went wrong during asset precompile: " + err);
- //throw new Error("Something went wrong during asset precompile: " + err);
return next(err);
}
next();

0 comments on commit 9a38b76

Please sign in to comment.