Browse files

first release

  • Loading branch information...
1 parent 40778b6 commit 6f75cce8631266cdf3ea6e3f2b34b7ad69bab7d3 @masylum committed Aug 14, 2011
Showing with 234 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. 0 README
  3. +77 −0 Readme.md
  4. +1 −0 index.js
  5. +139 −0 lib/asereje.js
  6. +16 −0 package.json
View
1 .gitignore
@@ -0,0 +1 @@
+node_modules
View
0 README
No changes.
View
77 Readme.md
@@ -0,0 +1,77 @@
+ .aMMMb .dMMMb dMMMMMP dMMMMb dMMMMMP dMMMMMP dMMMMMP
+ dMP"dMP dMP" VP dMP dMP.dMP dMP dMP dMP
+ dMMMMMP VMMMb dMMMP dMMMMK" dMMMP dMP dMMMP
+ dMP dMP dP .dMP dMP dMP"AMF dMP dK .dMP dMP
+ dMP dMP VMMMP" dMMMMMP dMP dMP dMMMMMP VMMMP" dMMMMMP
+
+
+"Aserejé ja de jé de jebe, tu de jebere sebiunouba, majabi an de bugui an de buididip"
+
+## Description
+
+Asereje is a library that builds your assets on demand.
+
+## Installation
+
+``` bash
+npm install asereje
+```
+
+## Usage
+
+First you have to configure asereje:
+
+``` javascript
+var asereje = require('asereje');
+
+asereje.config({
+ active: process.env.NODE_ENV === 'production'; // enable it just for production
+, js_globals: ['lib/jquery', 'global', 'underscore']; // js files that will be present always
+, css_globals: ['reset', 'global']; // css files that will be present always
+, js_path: _dirname + '/public/javascripts' // javascript folder path
+, css_path: _dirname + '/public/css' // css folder path
+});
+```
+
+Then you can use `css` or `js` function to get the file names.
+
+``` javascript
+res.render('users/index', {
+ css: asereje.css(['users', users/index]) // => ['reset', 'global', 'users', 'users/index']
+});
+
+... it builds the files and the next requests fill be:
+
+res.render('users/index', {
+ css: asereje.css(['users', users/index]) // => ['dist/f330099956c144c090b06f6d4bae8770', 'dist/caa6925553b03e049eeda6f70da9dc1a']
+});
+```
+
+``` jade
+html
+ head
+ -if(css)
+ -each file in css
+ link(rel= 'stylesheet', href= '/css/' + file + '.css' )
+ ...
+```
+
+## Explanation
+
+Asereje does 3 things:
+
+ * Bundles your files together and build 2 files. The first is for the files that are common to all the pages, such a reset.css or jquery.js.
+ This files will be cached once and not requested anymore. The second file is specific to each page.
+ * Applies minification. `Ugilify-js` for javascripts and `sqwish` for css.
+ * Renames the files with the md5 of the content. This way we can set the expires header to max.
+
+## Configure your webserver to take advantage
+
+Built files are stored under `js_path|css_path/dist`. Add expires headers to all this files so the users will have them cached forever.
+
+```
+// nginx conf
+location ~* ^/(css|javascripts)/dist/.+\.(css|js)$ {
+ expires max;
+}
+```
View
1 index.js
@@ -0,0 +1 @@
+module.exports.model = require('./lib/asereje');
View
139 lib/asereje.js
@@ -0,0 +1,139 @@
+var _cache = {}
+ , ASEREJE = {}
+ , _settings = { active: true
+ , js_globals: []
+ , css_globals: [] }
+ , uglify = require('uglify-js')
+ , sqwish = require('sqwish')
+ , _ = require('underscore')
+ , crypto = require('crypto')
+ , fs = require('fs');
+
+function md5(str) {
+ return crypto
+ .createHash('md5')
+ .update(str)
+ .digest('hex');
+}
+
+function _get(type, global_files, files) {
+ if (!ASEREJE[type + '_path']) throw (Error('You must configure asereje paths first!'));
+
+ function getCache(files) {
+ return _cache[files.join(';')];
+ }
+
+ function setAndCache(files) {
+ var funk = require('funk')('parallel')
+ , path = ASEREJE.paths[type];
+
+ files.forEach(function (el) {
+ fs.readFile(path + el + '.' + type, 'utf8', funk.result(el));
+ });
+
+ funk.run(function () {
+ var self = this, minified, bundled = '';
+
+ files.forEach(function (el) {
+ bundled += self[el];
+ });
+
+ if (type === 'css') {
+ minified = sqwish.minify(bundled, true);
+ } else if (type === 'js') {
+ (function () {
+ var jsp = uglify.parser
+ , pro = uglify.uglify
+ , ast = jsp.parse(bundled);
+
+ ast = pro.ast_mangle(ast);
+ ast = pro.ast_squeeze(ast);
+ minified = pro.gen_code(ast);
+ }());
+ }
+
+ _cache[files.join(';')] = ['dist/' + md5(minified), minified];
+ fs.writeFile(path + getCache(files)[0] + '.' + type, getCache(files)[1]);
+ });
+ }
+
+ function run(files) {
+ var cached = getCache(files);
+
+ if (cached) {
+ return [cached[0]];
+ } else {
+ if (files.length) {
+ setAndCache(files);
+ }
+ return files;
+ }
+ }
+
+ return run(global_files).concat(run(files));
+}
+
+/**
+ * Sets a config value
+ *
+ * @param {Object} values
+ *
+ * @return itself
+ */
+ASEREJE.config = function (values) {
+ _settings = _.extend(_settings, values);
+ return ASEREJE;
+};
+
+/**
+ * Returns an array of file names.
+ * Creates and caches the new css assets.
+ *
+ * @param {Array} files
+ *
+ * @return {Array}
+ */
+ASEREJE.css = function (files) {
+ if (!ASEREJE.css_globals) throw (Error('You must configure asereje `css_globals` first!'));
+
+ files = files || [];
+
+ if (ASEREJE.active) {
+ return _get('css', ASEREJE.css_globals, files);
+ } else {
+ ASEREJE.css_globals.forEach(function (el) {
+ files.unshift(el);
+ });
+ return files;
+ }
+};
+
+/**
+ * Returns an array of file names.
+ * Creates and caches the new javascript assets.
+ *
+ * @param {Array} global_files
+ * @param {Array} files
+ *
+ * @return {Array}
+ */
+ASEREJE.js = function (files) {
+ if (!ASEREJE.js_globals) throw (Error('You must configure asereje `js_globals` first!'));
+
+ files = files || [];
+
+ if (files.length) {
+ if (ASEREJE.active) {
+ return _get('js', ASEREJE.js_globals, files);
+ } else {
+ ASEREJE.js_globals.forEach(function (el) {
+ files.unshift(el);
+ });
+ return files;
+ }
+ } else {
+ return [];
+ }
+};
+
+module.exports = ASEREJE;
View
16 package.json
@@ -0,0 +1,16 @@
+{
+ "name": "asereje",
+ "description": "Asereje is a library that builds your assets on demand",
+ "version": "0.0.1",
+ "author": "Pau Ramon <masylum@gmail.com>",
+ "keywords": ["build", "assets", "css", "javascript"],
+ "main": "./index",
+ "dependencies": {
+ "ugilify-js": "1.0.6",
+ "funk": "1.0.1",
+ "sqwish": "0.2.0",
+ "underscore": "1.1.6"
+ },
+ "repository" : {"type": "git" , "url": "http://github.com/masylum/asereje.git" },
+ "engines": { "node": ">= 0.4.0" }
+}

0 comments on commit 6f75cce

Please sign in to comment.