Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
grunt-contrib-uglify
Advanced tools
The grunt-contrib-uglify package is a Grunt plugin that provides the ability to minify JavaScript files using UglifyJS. It helps in reducing the file size of JavaScript files by removing unnecessary characters, comments, and whitespace, which can improve the performance of web applications.
Minify JavaScript files
This feature allows you to minify multiple JavaScript files into a single output file. The code sample demonstrates how to configure the grunt-contrib-uglify task to take two input files (input1.js and input2.js) and produce a minified output file (output.min.js).
{
"uglify": {
"my_target": {
"files": {
"dest/output.min.js": ["src/input1.js", "src/input2.js"]
}
}
}
}
Generate source maps
This feature allows you to generate source maps for the minified JavaScript files. Source maps help in debugging by mapping the minified code back to the original source code. The code sample shows how to configure the grunt-contrib-uglify task to generate a source map file (output.map) along with the minified output file (output.min.js).
{
"uglify": {
"my_target": {
"options": {
"sourceMap": true,
"sourceMapName": "dest/output.map"
},
"files": {
"dest/output.min.js": ["src/input1.js", "src/input2.js"]
}
}
}
}
Customize UglifyJS options
This feature allows you to customize the UglifyJS options for the minification process. The code sample demonstrates how to configure the grunt-contrib-uglify task to disable variable name mangling and remove console statements from the output file (output.min.js).
{
"uglify": {
"my_target": {
"options": {
"mangle": false,
"compress": {
"drop_console": true
}
},
"files": {
"dest/output.min.js": ["src/input1.js", "src/input2.js"]
}
}
}
}
UglifyJS is a JavaScript parser, minifier, compressor, and beautifier toolkit. It is the underlying library used by grunt-contrib-uglify. While grunt-contrib-uglify is a Grunt plugin, uglify-js can be used directly in Node.js scripts or other build tools for more fine-grained control over the minification process.
Terser is a JavaScript parser and mangler/compressor toolkit for ES6+. It is a fork of UglifyJS that aims to support modern JavaScript syntax. Terser can be used as an alternative to UglifyJS for projects that use ES6+ features. It can be integrated with various build tools, including Grunt, via plugins like grunt-terser.
Babel Minify (also known as babel-preset-minify) is a minifier based on the Babel toolchain. It provides a set of Babel plugins that perform code transformations to reduce the size of JavaScript files. Babel Minify is particularly useful for projects that already use Babel for transpiling ES6+ code to ES5.
Minify files with UglifyJS.
Note that this plugin has not yet been released, and only works with the latest bleeding-edge, in-development version of grunt. See the When will I be able to use in-development feature 'X'? FAQ entry for more information.
If you haven't used grunt before, be sure to check out the Getting Started guide, as it explains how to create a gruntfile as well as install and use grunt plugins. Once you're familiar with that process, install this plugin with this command:
npm install grunt-contrib-uglify --save-dev
Run this task with the grunt uglify
command.
This task is a [multi task][] so any targets, files and options should be specified according to the [multi task][] documentation. [multi task]: https://github.com/gruntjs/grunt/wiki/Configuring-tasks
This task primarily delegates to UglifyJS2, so please consider the UglifyJS documentation as required reading for advanced configuration.
Type: Boolean
Object
Default: {}
Turn on or off mangling with default options. If an Object
is specified, it is passed directly to ast.mangle_names()
and ast.compute_char_frequency()
(mimicking command line behavior).
Type: Boolean
Object
Default: {}
Turn on or off source compression with default options. If an Object
is specified, it is passed as options to UglifyJS.Compressor()
.
Type: Boolean
Object
Default: false
Turns on beautification of the generated source code. An Object
will be merged and passed with the options sent to UglifyJS.OutputStream()
.
Type: String
Default: undefined
Specify the location to output the source map.
Type: String
Default: undefined
The location where your source files can be found.
Type: String
Default: undefined
The location of an input source map from an earlier compilation, e.g. from CoffeeScript.
Type: String
Default: undefined
The location of your sourcemap. Defaults to the location you use for sourceMap, override if you need finer control
Type: Number
Default: undefined
The number of directories to drop from the path prefix when declaring files in the source map.
Type: String
Default: undefined
Wrap all of the code in a closure, an easy way to make sure nothing is leaking.
For variables that need to be public exports
and global
variables are made available.
The value of wrap is the global variable exports will be available as.
Type: Boolean
Default: false
When using wrap
this will make all global functions and variables available via the export variable.
Type: Boolean
String
Function
Default: undefined
Options: false
'all'
'some'
Turn on preservation of comments.
false
will strip all comments'all'
will preserve all comments in code blocks that have not been squashed or dropped'some'
will preserve all comments that start with a bang (!
) or include a closure compiler style directive (@preserve
@license
@cc_on
)Function
specify your own comment preservation function. You will be passed the current node and the current comment and are expected to return either true
or false
Type: String
Default: empty string
This string will be prepended to the beginning of the minified output. It is processed using grunt.template.process, using the default options.
(Default processing options are explained in the grunt.template.process documentation)
In this example, running grunt uglify:my_target
(or grunt uglify
because uglify
is a [multi task][]) will mangle and compress the input files using the default options.
// Project configuration.
grunt.initConfig({
uglify: {
my_target: {
files: {
'dest/output.min.js': ['src/input1.js', 'src/input2.js']
}
}
}
});
Specify mangle: false
to prevent changes to your variable and function names.
// Project configuration.
grunt.initConfig({
uglify: {
options: {
mangle: false
},
my_target: {
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
You can specify identifiers to leave untouched with an except
array in the mangle
options.
// Project configuration.
grunt.initConfig({
uglify: {
options: {
mangle: {
except: ['jQuery', 'Backbone']
}
},
my_target: {
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
Configure basic source map output by specifying a file path for the sourceMap
option.
// Project configuration.
grunt.initConfig({
uglify: {
my_target: {
options: {
sourceMap: 'path/to/source-map.js'
},
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
You can specify the parameters to pass to UglifyJS.SourceMap()
which will
allow you to configure advanced settings.
Refer to the UglifyJS SourceMap Documentation for more information.
// Project configuration.
grunt.initConfig({
uglify: {
my_target: {
options: {
sourceMap: 'path/to/source-map.js',
sourceMapRoot: 'http://example.com/path/to/src/', // the location to find your original source
sourceMapIn: 'example/coffeescript-sourcemap.js', // input sourcemap from a previous compilation
}
},
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
Specify beautify: true
to beautify your code for debugging/troubleshooting purposes.
Pass an object to manually configure any other output options passed directly to UglifyJS.OutputStream()
.
See UglifyJS Codegen documentation for more information.
Note that manual configuration will require you to explicitly set beautify: true
if you want traditional, beautified output.
// Project configuration.
grunt.initConfig({
uglify: {
my_target: {
options: {
beautify: true
},
files: {
'dest/output.min.js': ['src/input.js']
}
},
my_advanced_target: {
options: {
beautify: {
width: 80,
beautify: true
}
},
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
In this example, running grunt uglify:my_target
will prepend a banner created by interpolating the banner
template string with the config object. Here, those properties are the values imported from the package.json
file (which are available via the pkg
config property) plus today's date.
Note: you don't have to use an external JSON file. It's also valid to create the pkg
object inline in the config. That being said, if you already have a JSON file, you might as well reference it.
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */'
},
my_target: {
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
Task submitted by "Cowboy" Ben Alman
This file was generated on Fri Jan 18 2013 10:16:17.
FAQs
Minify JavaScript files with UglifyJS
The npm package grunt-contrib-uglify receives a total of 113,232 weekly downloads. As such, grunt-contrib-uglify popularity was classified as popular.
We found that grunt-contrib-uglify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.