-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ddcded1
Showing
6 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules/ | ||
.idea/ | ||
index.html | ||
.DS_Store | ||
.anvil/ | ||
plugins/anvil.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |