Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhusar committed Feb 9, 2013
0 parents commit f3c8a2e
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .gitignore
@@ -0,0 +1,34 @@
# Include your project-specific ignores in this file
# Read about how to use .gitignore: https://help.github.com/articles/ignoring-files

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes

# Node.js #
######################
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules
113 changes: 113 additions & 0 deletions Readme.md
@@ -0,0 +1,113 @@
# node.packer

An assets combine and minify tool



## Description

`node.packer` is a simple tool to combine and minify css and javascript files.



## Requires

node >= 0.4.x



## Installation

npm install node.packer



## Options

> log
description: whether to log errors
data type: boolean
default value: false
possible value: true | false

> minify
description: whether to minify output file
data type: boolean
default value: false
possible value: true | false

> uglify
description: whether to uglify javascript variables
data type: boolean
default value: true
possible value: true | false

> input
description: files to be combined
data type: string
default value: undefined
possible value: '/path/to/the/css.css' ...

> output
description: path to save the combined file
data type: string
default value: undefined
possible value: '/path/to/the/js.min.js' ...

> callback
description: callback function
data type: function
default value: undefined
possible value: function ( err, stdout, stderr ){ ... }

## Usage

> Example
var packer = require( 'node.packer' ),
path = '~/Desktop/packer/';

packer({
log : true,
input : [
path + 'dojo.js',
path + 'jquery.js'
],
output : path + 'pack.min.js',
callback: function ( err, code ){
err && console.log( err );
}
});



## License

(The MIT License)

Copyright (c) 2011 dreamerslab <ben@dreamerslab.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require( './lib/combiner' );
113 changes: 113 additions & 0 deletions lib/combiner.js
@@ -0,0 +1,113 @@
/*
* js.combiner for client side js files
* Copyright(c) 2013 Daniel Husar <dano.husar@gmail.com.com>
* MIT Licensed
*
* @fileoverview
* An js files combine and minify tool.
*/

/**
* Module dependencies.
* @private
*/
var fs = require('fs'),
js_min = require('uglify-js'),
_ = require('underscore');


//default settings
var config = {
'files' : [], //array of files where search for reauire directives
'minify' : false, //minify the whole files
'reload' : true, //recreate file when theres some change in required files
'log' : true, //output logs
'suffix' : 'packed', //suffix of the new recreated file
'folder' : '/public/js', //folder where are the js files located
'cwd' : process.cwd(), //current workign directory
'vars' : {} //extra variables to put into client js file
};


/**
* Main combiner function, combine all js files, and replace ${variable} with real server variable (good mainly for configs)
* @param {object} options object that extend default settings, only property files is required
* @param {object} conf config varialbes to pass into client js
* @return {void}
*/
var combiner = function(options){
_.extend(config, options);
var requireMatch = /require\([',"]?(.*?)[',"]?\);?/gi,
variableMatch = /\${(.*?)}/gi,
vars = config.vars;

//run through all files from config
config.files.forEach(function (file) {
fs.readFile(config.cwd + config.folder + file, 'utf8', function (err, data) {
if (err) {

log('Error retrieving file: ' + file);

} else {

//replace all require with file contents
data = data.replace(requireMatch, function(pattern, path){
var fullPath = config.cwd + config.folder + path;

//read file
if(config.reload){
listenOnChange(fullPath, combiner);
}
return ('\n\n/*** REQUIRE: ' + path + ' ***/ \n\n' + fs.readFileSync(fullPath) ) || '/* ' + pattern + ' cannot be retrieved */';
});

//replace all variables with file contents
data = data.replace(variableMatch, function(pattern, variable){
return eval(variable) || '';
});

//minify file
if(config.minify){
var minify = js_min.parse(data);
minify.figure_out_scope();
minify.compute_char_frequency();
minify.mangle_names();
data = minify.print_to_string();
}

fs.writeFile(config.cwd + config.folder + file.replace('.js', '.' + config.suffix + '.js'), data);

}
});

});

};


var listenOnChange = function(file, callback){
fs.watchFile(file, function(curr, prev){
if(curr.size != prev.size){
log('Reloading file:' + file);
callback();
}
});
};

var log = function(message){
if(config.log){
console.dir(message);
}
}



/**
* @public
*/
combiner.version = JSON.parse( fs.readFileSync( __dirname + '/../package.json', 'utf8' )).version;

/**
* Exports module.
*/
module.exports = combiner;
39 changes: 39 additions & 0 deletions package.json
@@ -0,0 +1,39 @@
{
"name": "js-combiner",
"version": "1.0.0",
"description": "An assets combine and minify tool",
"keywords": [
"assets",
"asset",
"combine",
"minify",
"packer",
"compressor",
"compress"
],
"author": {
"name": "Daniel Husár",
"email": "dano.husar@gmail.com"
},
"dependencies": {
"uglify-js": "2.2.3",
"underscore" : ">=1.1.7"
},
"repository": {
"type": "git",
"url": "https://github.com/danielhusar/js-combiner.git"
},
"main": "index",
"engines": [
"node >= 0.8.0"
],
"licenses": [
{
"type": "MIT",
"url": "http://en.wikipedia.org/wiki/MIT_License"
}
],
"readmeFilename": "Readme.md",
"_id": "js-combiner@1.0.0",
"_from": "js-combiner"
}

0 comments on commit f3c8a2e

Please sign in to comment.