Skip to content

Commit

Permalink
Initial checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
arobson committed Aug 22, 2012
0 parents commit ddcded1
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
.idea/
index.html
.DS_Store
.anvil/
plugins/anvil.*
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Anvil Uglify Plugin

This plugin requires anvil.js version 0.8.* or greater.

## Installation

anvil install anvil.uglify

## Usage

If this plugin is installed and enabled, it will automatically minify .js files using the uglify-js node library.

### Minifying All The Things
Add the following snippet to the build.json:

"anvil.uglify": {
"all": "true"
}

### Inclusive Minification
Add the following snippet to the build.json to compile **only** the listed files:

"anvil.uglify": {
"include": [
"./path/to/file1",
"./path/to/file2",
"./path/to/file4",
]
}

### Exclusive Minification
Add the following snippet to the build.json to compile everything but the listed files:

"anvil.uglify": {
"exclude": [
"./path/to/file1",
"./path/to/file2",
"./path/to/file4",
]
}
9 changes: 9 additions & 0 deletions header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
{{{name}}} - {{{description}}}
version: {{{version}}}
author: {{{author}}}
copyright: 2012
license: Dual licensed
MIT (http://www.opensource.org/licenses/mit-license)
GPL (http://www.opensource.org/licenses/gpl-license)
*/
87 changes: 87 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
var jsp = require( "uglify-js" ).parser;
var pro = require( "uglify-js" ).uglify;

var uglifyFactory = function( _, anvil ) {
return anvil.plugin( {
name: "anvil.uglify",
activity: "post-process",
all: false,
inclusive: false,
exclusive: false,
fileList: [],
commander: [
[ "-u, --uglify", "uglify all javascripts" ]
],

configure: function( config, command, done ) {
if( config["anvil.config"] ) {
if( config["anvil.uglify"].all ) {
this.all = true;
} else if ( config["anvil.uglify"].include ) {
this.inclusive = true;
this.fileList = config["anvil.uglify"].include;
} else if (config["anvil.uglify"].exclude ) {
this.exclusive = true;
this.fileList = config["anvil.uglify"].exclude;
}
} else if( command.uglify ) {
this.all = true;
}
done();
},

run: function( done ) {
var jsFiles = [];
if ( this.inclusive ) {
jsFiles = this.inclusive;
} else {
jsFiles = _.filter( anvil.project.files, function( file ) {
return file.extension() === ".js";
} );

if( this.exclusive ) {
jsFiles = _.reject( jsFiles, function( file ) {
return _.any( this.fileList, function( excluded ) {
return excluded.fullPath === file.fullPath;
} );
} );
}
}
anvil.log.step( "Uglifying " + jsFiles.length + " files" );
anvil.scheduler.parallel( jsFiles, this.minify, function() { done(); } );
},

minify: function( file, done ) {
var self = this;
anvil.fs.read( [ file.workingPath, file.name ], function( content, err ) {
if( !err ) {
ast = jsp.parse( content );
ast = pro.ast_mangle( ast );
ast = pro.ast_squeeze( ast );
var final = pro.gen_code( ast ),
newName = self.rename( file.name );

anvil.fs.write( [file.workingPath, newName ], final, function( err ) {
if( err ) {
anvil.log.error( "Error writing " + file.fullPath + " for uglification: \n" + err.stack );
} else {
var minified = _.clone( file );
minified.name = newName;
anvil.project.files.push( minified );
}
done();
} );
} else {
anvil.log.error( "Error reading " + file.fullPath + " for uglification: \n" + err.stack );
done();
}
} );
},

rename: function( name ) {
return name.replace( ".js", ".min.js" );
}
} );
};

module.exports = uglifyFactory;
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"author": "Alex Robson <alex@sharplearningcurve.com> (http://sharplearningcurve.com)",
"name": "anvil.uglify",
"description": "Uglify plugin for anvil.js",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "git://github.com/arobson/anvil.uglify.git"
},
"engines": {
"node": "~0.8.3"
},
"main": "./lib/plugin.js",
"dependencies": { "uglify-js": "~1.3.3" },
"devDependencies": {},
"optionalDependencies": {}
}
80 changes: 80 additions & 0 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var jsp = require( "uglify-js" ).parser;
var pro = require( "uglify-js" ).uglify;

var uglifyFactory = function( _, anvil ) {
return anvil.plugin( {
name: "anvil.uglify",
activtiy: "post-process",
all: false,
inclusive: false,
exclusive: false,
fileList: [],

configure: function( config, command, done ) {
if( config["anvil.uglify"].all ) {
this.all = true;
} else if ( config["anvil.uglify"].include ) {
this.inclusive = true;
this.fileList = config["anvil.uglify"].include;
} else if (config["anvil.uglify"].exclude ) {
this.exclusive = true;
this.fileList = config["anvil.uglify"].exclude;
}
done();
},

run: function( done ) {
var jsFiles = [];
if ( this.inclusive ) {
jsFiles = this.inclusive;
} else {
jsFiles = _.filter( anvil.project.files, function( file ) {
return file.extension() == "js";
} );

if( this.exclusive ) {
jsFiles = _.reject( jsFiles, function( file ) {
return _.any( this.fileList, function( excluded ) {
return excluded.fullPath === file.fullPath;
} );
} );
}
}

anvil.scheduler.parallel( jsFiles, this.minify, function() { done(); } );
},

minify: function( file, done ) {
var self = this;
anvil.fs.read( [ file.workingPath, file.name ], function( content, err ) {
if( !err ) {
ast = jsp.parse( content );
ast = pro.ast_mangle( ast );
ast = pro.ast_squeeze( ast );
var final = pro.gen_code( ast ),
newName = self.rename( file.name );

anvil.fs.write( [file.workingPath, newName ], final, function( err ) {
if( err ) {
anvil.log.error( "Error writing " + file.fullPath + " for uglification: \n" + err.stack );
} else {
var minified = _.clone( file );
minified.name = newName;
anvil.project.files.push( minified );
}
done();
} );
} else {
anvil.log.error( "Error reading " + file.fullPath + " for uglification: \n" + err.stack );
done();
}
} );
},

rename: function( name ) {
return name.replace( ".js", ".min.js" );
}
} );
};

module.exports = uglifyFactory;

0 comments on commit ddcded1

Please sign in to comment.