A grunt plugin to minify given JavaScript files
This plugin was originally intended to add some features grunt-contrib-uglify did not supply under Grunt 0.3. After the Grunt 0.4 release, there's no longer need for grunt-minified.
I will no longer develop this plugin any further and I strongly suggest users of this plugin to either fork it and continue the development on their hand, or use the fully featured grunt-contrib-uglify plugin.
Install this grunt plugin next to your project's Gruntfile.js with:
npm install grunt-minified
Then add this line to your project's Gruntfile.js
:
grunt.loadNpmTasks('grunt-minified');
Add something like this in your gruntfile:
minified : {
files: {
src: [
'/js/src/**/*.js',
'/js/src/*.js'
],
dest: '/js/min/'
},
options : {
sourcemap: true,
allinone: false
}
}
With this configuration, grunt-minified will output a minified file and a sourcemap per parsed file with this filename structure: <filename>.min.js
in the dest
folder.
Grunt-minified currently supports these options:
- Type:
Boolean
- Defaults:
false
Generate a sourcemap for the generated files(s) if toggled. The output files is saved with this filename structure: <filename>.js.map
in the dest
folder.
- Type:
Boolean
- Defaults:
false
Generate just one minified file if toggled. Output file is saved as minified.js
if options.dest_filename
is not set.
- Type:
String
- Defaults:
minified.js
If allinone
is set, grunt-minified will use dest_filename
for the generated sourcemap. No point of setting this if allinone
is set to false.
Currently, this is a copy & paste from UgliyJS2, YMMV.
- Type:
String
- Defaults:
null
If ext
is set, grunt-minified will replace the files current extention with ext
. The extension must begin with a .
to work correctly. Eg .min.js
. This will also override the extension of dest_filename
if set.
- Type:
Object
- Defaults:
null
If mirrorSource.path
is set, grunt-minified will mirror the destination path to the source path of each file that is processed. You must supply at path
property for mirrorSource
to work.
- Type: 'String'
- Defaults:
null
mirrorSource.path
is a String
that describes the base path of the source folder that you would like to mirror. The mirrorSource.path
will be replaced by files.dest
.
Example configuration using mirrorSource
:
minified : {
dev: {
files: {
src: [
'source/**/*.js'
],
dest: 'deploy'
},
options : {
sourcemap: false,
allinone: false,
mirrorSource: {
path: 'source/js/'
},
ext: '.min.js'
}
}
}
Example folder output using mirrorSource
:
/cwd/
├─┬ source
| ├─┬ folder_a
| ├── a.js
| └─┬ folder_ab
| └── ab.js
├─┬ deploy
| ├─┬ folder_a
| ├── a.min.js
| └─┬ folder_ab
| └── ab.min.js
- Type:
Object
- Defaults:
{}
If uglifyOpts
is set, grunt-minified will pass along the options to uglify-js. Below is a list of options that are available:
warnings
: (defaultfalse
) - passtrue
to display compressor warnings.fromString
: (defaultfalse
) - if you passtrue
then you can pass JavaScript source code, rather than file names.mangle
: passfalse
to skip mangling names.output
: (defaultnull
) - pass an object if you wish to specify additional Beautifier options. The defaults are optimized for best compressions. Please see UglifyJS - the code generator for more details.compress
: (default{}
) - passfalse
to skip compressing entirely. Pass an object to specify custom compressor options. Please see UglifyJS - the compressor for more details.
beautify
(defaulttrue
) -- whether to actually beautify the output.indent-level
(default 4)indent-start
(default 0) -- prefix all lines by that many spacesquote-keys
(defaultfalse
) -- passtrue
to quote all keys in literal objectsspace-colon
(defaulttrue
) -- insert a space after the colon signsascii-only
(defaultfalse
) -- escape Unicode characters in strings and regexpsinline-script
(defaultfalse
) -- escape the slash in occurrences of</script
in stringswidth
(default 80) -- only takes effect when beautification is on, this specifies an (orientative) line width that the beautifier will try to obey. It refers to the width of the line text (excluding indentation). It doesn't work very well currently, but it does make the code generated by UglifyJS more readable.max-line-len
(default 32000) -- maximum line length (for uglified code)ie-proof
(defaulttrue
) -- generate “IE-proof” code (for now this means add brackets around the do/while in code like this:if (foo) do something(); while (bar); else ...
.bracketize
(defaultfalse
) -- always insert brackets inif
,for
,do
,while
orwith
statements, even if their body is a single statement.semicolons
(defaulttrue
) -- separate statements with semicolons. If you passfalse
then whenever possible we will use a newline instead of a semicolon, leading to more readable output of uglified code (size before gzip could be smaller; size after gzip insignificantly larger).
You need to pass --compress
(-c
) to enable the compressor. Optionally you can pass a comma-separated list of options. Options are in the form foo=bar
, or just foo
(the latter implies a boolean option that you want to set true
; it's effectively a shortcut for foo=true
).
The defaults should be tuned for maximum compression on most code. Here are the available options (all are true
by default, except hoist_vars
):
-
sequences
-- join consecutive simple statements using the comma operator -
properties
-- rewrite property access using the dot notation, for examplefoo["bar"] → foo.bar
-
dead-code
-- remove unreachable code -
drop-debugger
-- removedebugger;
statements -
unsafe
-- apply "unsafe" transformations (discussion below) -
conditionals
-- apply optimizations forif
-s and conditional expressions -
comparisons
-- apply certain optimizations to binary nodes, for example:!(a <= b) → a > b
(only whenunsafe
), attempts to negate binary nodes, e.g.a = !b && !c && !d && !e → a=!(b||c||d||e)
etc. -
evaluate
-- attempt to evaluate constant expressions -
booleans
-- various optimizations for boolean context, for example!!a ? b : c → a ? b : c
-
loops
-- optimizations fordo
,while
andfor
loops when we can statically determine the condition -
unused
-- drop unreferenced functions and variables -
hoist-funs
-- hoist function declarations -
hoist-vars
-- hoistvar
declarations (this isfalse
by default because it seems to increase the size of the output in general) -
if-return
-- optimizations for if/return and if/continue -
join-vars
-- join consecutivevar
statements -
cascade
-- small optimization for sequences, transformx, x
intox
andx = something(), x
intox = something()
-
warnings
-- display warnings when dropping unreachable code or unused declarations etc.For more information on UglifyJS, please see the UglifyJS Website
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
(Until v1.0.0, this will only be updated when major or breaking changes are made)
- 2013/01/13 - v0.0.5 - Added support for mirroring source to dest and the ability to set a custom file extension on minified files.
- 2013/01/11 - v0.0.4 - Added support for UglifyJS options
- 2013/01/10 - v0.0.3 - Added support for grunt v 0.4.0rc5
Copyright (c) 2012 Alexander Vassbotn Røyne-Helgesen Licensed under the GPL license.