Use mincer to concatenate your sources.
Install this grunt plugin next to your project's grunt.js gruntfile with: npm install grunt-mincer
Then add this line to your project's grunt.js
gruntfile:
grunt.loadNpmTasks('grunt-mincer');
Mincer is a multi task, meaning that grunt will automatically iterate over all
mince
targets if a target is not specified.
Inside your grunt.js
file, add a section named mince
. Task targets, files and options may be
specified according to the grunt Configuring tasks guide.
List of directories that are added to mincer load path. If you have only one directory it can be specified as a single string.
List of configurations like autoprefixer
or source_maps
that should be enabled in mincer's environment.
Use to define helpers functions for EJS module. You can preprocess any resource with EJS as long as
you use ejs
extension.
Object with configuration options for each mincer engine.
Optional configure function that is called before before compile
phase: allows for direct access to mincer object.
Path to manifest.json
file that will be generated from assets. It will contain several attributes
for faster access to assets. Check out mincer documentation
for more information about manifest usage. If manifestPath
is set assets are compiled into this
directory, the destination information in the files configuration will be ignored.
JavaScript compression function or predefined mincer
js compressor identifier "uglify"
. If predefined identifier is used - uglify-js
package needs to be installed. Check out mincer jsCompressor documentation for more details.
CSS compression function or predefined mincer
css compressor identifier "csso"
. If predefined identifier is used - csso
package needs to be installed. Check out mincer cssCompressor documentation for more details.
The base url to use when referencing source-maps in compiled assets. Defaults to ""
if not explicitly set.
Optional function that is called to determine the sourceMappingURL for a file. Has access to the grunt task options as well as the file object for current asset. By default returns the destination filename prefixed with the sourceMappingBaseURL
The files on which the task operates can be defined using all the powerful options provided by Grunt. See the Configuring tasks guide for details on the different ways to configure the file sets.
There are couple of formats you can use to configure mincer task.
mince: {
main: {
options: {
include: ["src", "module/src"]
},
files: [{
src: "main.js",
dest: "build/main.js"
}]
}
}
You can compile multiple assets into one output file:
mince: {
main: {
options: {
include: ["src", "module/src"]
},
files: [{
src: ["main.js", "extra.js"],
dest: "build/main.js"
}]
}
}
You can dynamically build the file mapping:
mince: {
main: {
options: {
include: ["src", "module/src"]
},
files: [
{
cwd: "javascripts",
src: ["**/*.coffee", "**/*.js", "!**/index.js"],
dest: "build/main.js",
expand: true
}
]
}
}
And if you only have one include
directory you can specify it as string:
mince: {
main: {
options: {
include: "src"
}
}
}
Manifest generation:
mince: {
main: {
options: {
include: ["src", "module/src"],
manifestPath: "build/manifest.json",
manifestOptions: {
compress: false,
sourceMaps: false,
embedMappingComments: false
}
},
files: [
{
src: ["**/*.coffee", "**/*.js", "**/*.scss"],
dest: "build/"
}
]
}
}
By setting the manifestPath
you enable compiling and writing the assets to the given directory. The
assets will be written to fingerprinted files. Optionally you can specify manifestOptions
. This object
will be passed through to mincer unmodified. See the mincer documentation for supported options.
The defaults are to disable compression and source maps.
You can use a different format for each target.
You can configure Mincer engines: this configures CoffeeEngine
to use bare
compilation option,
and instructs StylusEngine
to use nib
.
mince: {
main: {
options: {
include: "src",
engines: {
Coffee: {
bare: true
},
Stylus: function(stylus) {
stylus.use(require("nib")());
}
}
},
files: [{
src: "main.js",
dest: "build/main.js"
}]
}
}
You can also define EJS helpers in your gruntfile:
mince: {
main: {
options: {
include: "src",
helpers: {
version: function() {
return "3.2.1";
}
}
},
files: [{
src: "main.js.ejs",
dest: "build/main.js"
}]
}
}
To access Mincer
directly used configure
option:
mince: {
main: {
options: {
include: "src",
configure: function(mincer) {
mincer.logger.use({
error: function(msg) {}
});
}
},
files: [{
src: "main.js.ejs",
dest: "build/main.js"
}]
}
}
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.
- grunt-mincer 0.2 is compatible with grunt 0.3
- grunt-mincer 0.3 and newer is compatible with grunt 0.4
See History.md
Copyright (c) 2012 (pirxpilot) Damian Krzeminski Licensed under the MIT license.