potato-assets v1.0.0
potato-assets
sprockets inspired node potatoes
example
var express = require("express"),
potato = require("potato-assets").instance,
pat = require("path"),
app = express();
//add directory to render path
potato.use(path.resolve(__dirname, "assets"));
//change bundle root from /assets to /potatoes
//resulting bundle will be /potatoes/[hash].application.bundle
potato.root = "/potatoes";
//if development
app.configure("development", function() {
//watch for file changes
potato.watch();
//don't minify
potato.minify = false;
});
//runtime render bundles, return middleware
app.use(potato.render());
app.listen(8080);
info
Potato Assets uses a modular building system, it allows you to add new and remove existing handlers at runtime. It works in two sets, bundles and renderers.
For example, I have the file example.js.ls
, the library will look for the handler for the "ls" extension, within the bundle for the "js" extension if "ls" isn't a bundle itself.
existing handlers and requirements
The following modules will be loaded if all requirements are met for that specific handler*
- .js
- no requirements
- .js.ls
- LiveScript (npm)
- .js.coffee, .js.cs
- coffee-script (npm)
- .css
- no requirements
- .css.less
- less (npm)
- .css.sass, .css.scss
- node-sass (npm)
- .jst
- no base handler
- .jst.jade
- jade (npm)
- .jst.ejs
- ejs (npm)
- .jst.handlebars, .jst.hbs
- handlebars (npm)
* example: i have jade installed but not ejs or handlebars, only the jade handler gets loaded, but not the ejs or handlebars handler
custom handlers
Any Potato Assets instance will have three variables,
instance.handlers;
instance.bundles;
instance.mime;
If you want to add a new bundle you need to add variables to all three dictionaries.
instance.handlers.example = function(filename, data, callback) {
return callback(error, data);
};
instance.bundles.example = "application.example";
instance.mime.example = "text/plain";
would create a new bundle called application.example
and is available under the POTATO_ASSETS.example
local
However, if you want to add a new renderer to a preexisting bundle, you would have to add a renderer with the key to the base function, as example.
instance.handlers.jst.jsx = function(filename, data, callback) {
return callback(filename, data);
}
The above code would allow for example.jst.jsx
to be rendered into application.jst
Alternatively you can use the .addHandler
and .removeHandler
functions.
instance.addHandler("js.jsx", function(filename, data, callback) {
return callback(filename, data);
});
instance.addHandler("example", function(filename, data, callback) {
return callback(filename, data);
}, "application.example", "text/plain");
//override js
instance.addHandler("js", function(filename, data, callback) {
return callback(filename, data);
});
//remove cs handler
instance.removeHandler("js.cs");
//remove css base handler
instance.removeHandler("css");
//remove jst entirely
instance.removeHandler("jst", true);
jst
View templates are automatically compiled and bundled into a global dictionary variable called JST under a key relative to the related directory (hopefully)
JST["site/notification"](render arguments)
render locals
Potato Assets adds a variable to res.locals called "POTATO_ASSETS", which is a dictionary relating to POTATO_ASSETSbundle.
<script src="<%= POTATO_ASSETS.js %>"></script>
todo
9 years ago