• Stars
    star
    811
  • Rank 56,215 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Concatenate files.

grunt-contrib-concat v2.1.0 Build Status

Concatenate files.

Getting Started

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, you may install this plugin with this command:

npm install grunt-contrib-concat --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-contrib-concat');

Concat task

Run this task with the grunt concat command.

Task targets, files and options may be specified according to the Grunt Configuring tasks guide.

Options

separator

Type: String
Default: grunt.util.linefeed

Concatenated files will be joined on this string. If you're post-processing concatenated JavaScript files with a minifier, you may need to use a semicolon ';\n' as the separator.

banner

Type: String
Default: ''

This string will be prepended to the beginning of the concatenated output. It is processed using grunt.template.process, using the default options.

(Default processing options are explained in the grunt.template.process documentation)

footer

Type: String
Default: ''

This string will be appended to the end of the concatenated output. It is processed using grunt.template.process, using the default options.

(Default processing options are explained in the grunt.template.process documentation)

stripBanners

Type: Boolean Object
Default: false

Strip JavaScript banner comments from source files.

  • false - No comments are stripped.
  • true - /* ... */ block comments are stripped, but NOT /*! ... */ comments.
  • options object:
    • By default, behaves as if true were specified.
    • block - If true, all block comments are stripped.
    • line - If true, any contiguous leading // line comments are stripped.

process

Type: Boolean Object Function
Default: false

Process source files before concatenating, either as templates or with a custom function.

  • false - No processing will occur.
  • true - Process source files using grunt.template.process defaults.
  • data object - Process source files using grunt.template.process, using the specified options.
  • function(src, filepath) - Process source files using the given function, called once for each file. The returned value will be used as source code.

(Default processing options are explained in the grunt.template.process documentation)

sourceMap

Type: Boolean
Default: false

Set to true to create a source map. The source map will be created alongside the destination file, and share the same file name with the .map extension appended to it.

sourceMapName

Type: String Function
Default: undefined

To customize the name or location of the generated source map, pass a string to indicate where to write the source map to. If a function is provided, the concat destination is passed as the argument and the return value will be used as the file name.

sourceMapStyle

Type: String
Default: embed

Determines the type of source map that is generated. The default value, embed, places the content of the sources directly into the map. link will reference the original sources in the map as links. inline will store the entire map as a data URI in the destination file.

Usage Examples

Concatenating with a custom separator

In this example, running grunt concat:dist (or grunt concat because concat is a multi task) will concatenate the three specified source files (in order), joining files with ; and writing the output to dist/built.js.

// Project configuration.
grunt.initConfig({
  concat: {
    options: {
      separator: ';',
    },
    dist: {
      src: ['src/intro.js', 'src/project.js', 'src/outro.js'],
      dest: 'dist/built.js',
    },
  },
});

Banner comments

In this example, running grunt concat:dist will first strip any preexisting banner comment from the src/project.js file, then concatenate the result with a newly-generated banner comment, writing the output to dist/built.js.

This generated banner will be the contents of the banner template string interpolated with the config object. In this case, 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'),
  concat: {
    options: {
      stripBanners: true,
      banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
        '<%= grunt.template.today("yyyy-mm-dd") %> */',
    },
    dist: {
      src: ['src/project.js'],
      dest: 'dist/built.js',
    },
  },
});

Multiple targets

In this example, running grunt concat will build two separate files. One "basic" version, with the main file essentially just copied to dist/basic.js, and another "with_extras" concatenated version written to dist/with_extras.js.

While each concat target can be built individually by running grunt concat:basic or grunt concat:extras, running grunt concat will build all concat targets. This is because concat is a multi task.

// Project configuration.
grunt.initConfig({
  concat: {
    basic: {
      src: ['src/main.js'],
      dest: 'dist/basic.js',
    },
    extras: {
      src: ['src/main.js', 'src/extras.js'],
      dest: 'dist/with_extras.js',
    },
  },
});

Multiple files per target

Like the previous example, in this example running grunt concat will build two separate files. One "basic" version, with the main file essentially just copied to dist/basic.js, and another "with_extras" concatenated version written to dist/with_extras.js.

This example differs in that both files are built under the same target.

Using the files object, you can have list any number of source-destination pairs.

// Project configuration.
grunt.initConfig({
  concat: {
    basic_and_extras: {
      files: {
        'dist/basic.js': ['src/main.js'],
        'dist/with_extras.js': ['src/main.js', 'src/extras.js'],
      },
    },
  },
});

Dynamic filenames

Filenames can be generated dynamically by using <%= %> delimited underscore templates as filenames.

In this example, running grunt concat:dist generates a destination file whose name is generated from the name and version properties of the referenced package.json file (via the pkg config property).

// Project configuration.
grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),
  concat: {
    dist: {
      src: ['src/main.js'],
      dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js',
    },
  },
});

Advanced dynamic filenames

In this more involved example, running grunt concat will build two separate files (because concat is a multi task). The destination file paths will be expanded dynamically based on the specified templates, recursively if necessary.

For example, if the package.json file contained {"name": "awesome", "version": "1.0.0"}, the files dist/awesome/1.0.0/basic.js and dist/awesome/1.0.0/with_extras.js would be generated.

// Project configuration.
grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),
  dirs: {
    src: 'src/files',
    dest: 'dist/<%= pkg.name %>/<%= pkg.version %>',
  },
  concat: {
    basic: {
      src: ['<%= dirs.src %>/main.js'],
      dest: '<%= dirs.dest %>/basic.js',
    },
    extras: {
      src: ['<%= dirs.src %>/main.js', '<%= dirs.src %>/extras.js'],
      dest: '<%= dirs.dest %>/with_extras.js',
    },
  },
});

Invalid or Missing Files Warning

If you would like the concat task to warn if a given file is missing or invalid be sure to set nonull to true:

grunt.initConfig({
  concat: {
    missing: {
      src: ['src/invalid_or_missing_file'],
      dest: 'compiled.js',
      nonull: true,
    },
  },
});

See configuring files for a task for how to configure file globbing in Grunt.

Custom process function

If you would like to do any custom processing before concatenating, use a custom process function:

grunt.initConfig({
  concat: {
    dist: {
      options: {
        // Replace all 'use strict' statements in the code with a single one at the top
        banner: "'use strict';\n",
        process: function(src, filepath) {
          return '// Source: ' + filepath + '\n' +
            src.replace(/(^|\n)[ \t]*('use strict'|"use strict");?\s*/g, '$1');
        },
      },
      files: {
        'dist/built.js': ['src/project.js'],
      },
    },
  },
});

Release History

  • 2022-04-03   v2.1.0   Updated dependencies
  • 2021-10-07   v2.0.0   Update dependencies Requires node.js v12+
  • 2016-04-20   v1.0.1   Fix for concatenating multiple source map files.
  • 2016-02-20   v1.0.0   Update source-map to 0.5.3. Tag Grunt as peerDep to >=0.4.0. Make source maps generation a little faster. Add charset:utf-8 to sourceMappingURL.
  • 2015-02-20   v0.5.1   Fix path issues with source maps on Windows.
  • 2014-07-19   v0.5.0   Adds sourceMap option.
  • 2014-03-21   v0.4.0   README updates. Output updates.
  • 2013-04-25   v0.3.0   Add option to process files with a custom function.
  • 2013-04-08   v0.2.0   Don't normalize separator to allow user to set LF even on a Windows environment.
  • 2013-02-22   v0.1.3   Support footer option.
  • 2013-02-15   v0.1.2   First official release for Grunt 0.4.0.
  • 2013-01-18   v0.1.2rc6   Updating grunt/gruntplugin dependencies to rc6. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions.
  • 2013-01-09   v0.1.2rc5   Updating to work with grunt v0.4.0rc5. Switching back to this.files API.
  • 2012-11-13   v0.1.1   Switch to this.file API internally.
  • 2012-10-03   v0.1.0   Work in progress, not yet officially released.

Task submitted by "Cowboy" Ben Alman

This file was generated on Sun Apr 03 2022 07:55:57.

More Repositories

1

grunt

Grunt: The JavaScript Task Runner
JavaScript
12,273
star
2

grunt-contrib-watch

Run tasks whenever watched files change.
JavaScript
1,982
star
3

grunt-contrib-uglify

Minify files with UglifyJS.
JavaScript
1,483
star
4

grunt-contrib-imagemin

Minify PNG, JPG, GIF and SVG images.
JavaScript
1,214
star
5

grunt-contrib-sass

Compile Sass to CSS.
JavaScript
848
star
6

grunt-contrib-cssmin

Compress CSS files.
JavaScript
793
star
7

grunt-contrib-copy

Copy files and folders.
JavaScript
726
star
8

grunt-contrib-connect

Start a static web server.
JavaScript
714
star
9

grunt-contrib-jshint

Validate files with JSHint.
JavaScript
709
star
10

grunt-cli

Grunt's command line interface.
JavaScript
707
star
11

grunt-contrib-less

Compile LESS files to CSS.
JavaScript
667
star
12

grunt-contrib-compass

Compile Compass to CSS.
JavaScript
625
star
13

grunt-contrib-clean

Clear files and folders.
JavaScript
512
star
14

grunt-contrib-requirejs

Optimize RequireJS projects using r.js.
JavaScript
504
star
15

grunt-contrib

[DEPRECATED] A collection of common grunt tasks.
JavaScript
475
star
16

grunt-contrib-htmlmin

Minify HTML.
JavaScript
425
star
17

grunt-contrib-jasmine

Run jasmine specs headlessly through Headless Chrome
JavaScript
354
star
18

grunt-contrib-compress

Compress files and folders.
JavaScript
345
star
19

grunt-contrib-coffee

Compile CoffeeScript files to JavaScript.
JavaScript
329
star
20

grunt-contrib-pug

Compile Pug templates.
JavaScript
328
star
21

grunt-contrib-handlebars

Precompile Handlebars templates to JST file.
JavaScript
282
star
22

grunt-contrib-csslint

Lint CSS files.
JavaScript
240
star
23

grunt-contrib-qunit

Run QUnit tests in Headless Chrome.
JavaScript
214
star
24

grunt-contrib-livereload

Reload assets live in the browser.
JavaScript
202
star
25

grunt-init

Generate project scaffolding from a template.
JavaScript
193
star
26

grunt-contrib-stylus

Compile Stylus files to CSS.
JavaScript
174
star
27

grunt-init-gruntfile

Create a basic Gruntfile with grunt-init.
JavaScript
156
star
28

gruntjs.com

Grunt website
Less
155
star
29

grunt-contrib-jst

Compile underscore templates to JST file.
JavaScript
113
star
30

grunt-lib-phantomjs

Grunt and PhantomJS, sitting in a tree.
JavaScript
93
star
31

grunt-next

Grunt v1.0 alpha
JavaScript
84
star
32

grunt-docs

Grunt documentation. https://github.com/gruntjs/gruntjs.com
79
star
33

grunt-init-gruntplugin

Create a gruntplugin module with grunt-init, including Nodeunit unit tests.
JavaScript
77
star
34

grunt-contrib-yuidoc

Compile YUIDoc Documentation.
JavaScript
75
star
35

grunt-contrib-nodeunit

Run Nodeunit unit tests.
JavaScript
70
star
36

grunt-init-jquery

Create a jQuery plugin with grunt-init, including QUnit unit tests.
JavaScript
58
star
37

grunt-contrib-symlink

Create symbolic links.
JavaScript
56
star
38

grunt-init-node

Create a Node.js module with grunt-init, including Nodeunit unit tests.
JavaScript
55
star
39

grunt-contrib-bump

A work-in-progress Grunt plugin for bumping a version number in JSON files.
JavaScript
52
star
40

grunt-init-commonjs

Create a commonjs module with grunt-init, including Nodeunit unit tests.
JavaScript
17
star
41

grunt-contrib-internal

Internal tasks for managing the grunt-contrib project.
JavaScript
16
star
42

grunt-init-gruntfile-sample

This is sample output generated by the grunt-init "gruntfile" template.
JavaScript
14
star
43

grunt-init-jquery-sample

This is sample output generated by the grunt-init "jquery" template.
JavaScript
12
star
44

grunt-lib-contrib

Common functionality shared across grunt-contrib tasks.
JavaScript
12
star
45

example-subgrunt

Run a Gruntfile in multiple subdirectories.
JavaScript
11
star
46

grunt-contrib-mincss

Renamed to grunt-contrib-cssmin.
9
star
47

grunt-init-gruntplugin-sample

This is sample output generated by the grunt-init "gruntplugin" template.
JavaScript
9
star
48

grunt-init-node-sample

This is sample output generated by the grunt-init "node" template.
JavaScript
8
star
49

grunt-known-options

The known options used in Grunt
JavaScript
7
star
50

grunt-init-commonjs-sample

This is sample output generated by the grunt-init "commonjs" template.
JavaScript
7
star
51

clone-repos

Quickly clone all gruntjs repos (for grunt development)
Ruby
6
star
52

grunt-lib-legacyhelpers

Some old grunt helpers provided for backwards compatability.
JavaScript
6
star
53

grunt-legacy-util

deprecated utility methods
JavaScript
5
star
54

grunt-legacy-log

The Grunt logger.
JavaScript
5
star
55

rfcs

RFCs for changes to Grunt
4
star
56

grunt-legacy-log-utils

Static methods for the Grunt 0.4.x logger.
JavaScript
4
star
57

grunt-plugin-list

[Deprecated] Generates a list of all grunt plugins as json
JavaScript
4
star
58

grunt-legacy-event-logger

Event logger for Grunt legacy libs.
JavaScript
3
star
59

grunt-legacy-config

Grunt's config methods, as a standalone library.
JavaScript
2
star
60

grunt-legacy-option

Grunt's option methods, as a standalone library.
JavaScript
1
star
61

grunt-legacy-cli

Grunt's CLI methods, as a standalone library.
JavaScript
1
star
62

grunt-legacy-task

Grunt's task methods, as a standalone library.
JavaScript
1
star