diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..11f1f24 --- /dev/null +++ b/.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 \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..6362432 --- /dev/null +++ b/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. diff --git a/index.js b/index.js new file mode 100644 index 0000000..37015a7 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require( './lib/combiner' ); \ No newline at end of file diff --git a/lib/combiner.js b/lib/combiner.js new file mode 100644 index 0000000..bab4aaf --- /dev/null +++ b/lib/combiner.js @@ -0,0 +1,113 @@ +/* + * js.combiner for client side js files + * Copyright(c) 2013 Daniel Husar + * 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; diff --git a/package.json b/package.json new file mode 100644 index 0000000..7b152d2 --- /dev/null +++ b/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" +}