#Radius
Radius is an asset (css, js, whatever-you-need) management tool for your project.
It helps you better organize files you serve to browser, as well as process them with any processor you need, even with your own one.
For now it works with express.js but you can simply adjust it to your project and technology.
##Features:
- creating named 'packages' from your assets (e.g. jquery-ui, my-tools, etc.)
- merging, processing, compressing files for production environment
- generating separate files for development process
- watching for changes and rebuilding suitable packages
- switching between merged, compressed files and separate files with query parameter (in express.js)
- support for jade and stylus (more in progress)
##How it works:
- Create config file with packages (or simply files) you want to serve to your application
- Specify output directory in config
- Specify preprocessors (optional) and postprocessors (optional)
- Run radius
- Inject created packages to your project.
##How to do that:
###Installation
npm install radius
###Creating config file
module.exports = {
assetsDir: 'assets',
outputDir: 'output',
manifest: 'output/manifest.json',
preprocessors: {},
postprocessors: {},
packages: [
{
name: 'core',
final: false,
files: [
'file1.js',
'file2.js'
]
},
{
name: 'gallery',
final: true,
require: ['templates'],
files: [
'gallery.js',
'gallery.css'
]
},
{
name: 'templates',
final: false,
files: [
'template.jade',
'styles.styl'
]
}
]
};
###Preprocessors
Preprocessors are functions that will receive raw content of your files, and will generate from this content final file content (e.g. generate css files from stylus files).
For now jade and stylus are supported.
Definition looks like this:
var preprocessors = require('radius').preprocessors;
var stylus = preprocessors.stylus;
var jade = preprocessors.jade;
module.exports = {
...
preprocessors: {
styl: stylus,
jade: {
fn: jade,
options: {
pretty: true
}
}
},
...
};
For now you can use only one preprocessor per file extension.
###Postprocessors
Postprocessors are functions that will receive content of your merged packages, and will generate from this content final package content (e.g. uglify/compress merged package)
Definition looks like this:
var postprocessors = require('radius').postprocessors;
var cleanCss = postprocessors['clean-css'];
var uglifyJs = postprocessors['uglify-js'];
module.exports = {
...
postprocessors: {
css: [cleanCss],
js: [{
fn: uglifyJs,
options: {
unsafe: true
}
}]
},
...
};
###Run Radius
./node_modules/.bin/radius --config path/to/config/file.js
This will create your packages, preprocess and postprocess them, save them to output directories and create manifest.json file.
Now you have your packages ready to use. You just have to use them in your application
###Using packages with express.js
module.exports = {
assetsDir: 'assets',
outputDir: 'output',
manifest: 'output/manifest.json',
packages: [
{
name: 'gallery',
final: true,
files: [
'gallery.css',
'jquery.js',
'gallery.js'
]
}
]
};
var express = require('express');
var radiusForExpress = require('radius').adapters.express;
var app = express();
app.use(radiusForExpress('output/mainfest.json'));
app.get('/', function (req, res) {
var body = '<html><head>' + res.locals.assets.gallery.css + '</head><body>' + res.locals.assets.gallery.js + '</body></html>';
res.setHeader('Content-Type', 'text/html');
res.setHeader('Content-Length', body.length);
res.end(body);
});
app.listen(3000);
console.log('Listening on port 3000');
TBC...