GithubHelp home page GithubHelp logo

web5design / grunt-contrib-concat Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gruntjs/grunt-contrib-concat

0.0 3.0 0.0 90 KB

Concatenate files.

Home Page: http://gruntjs.com/

License: MIT License

grunt-contrib-concat's Introduction

grunt-contrib-concat Build Status

Concatenate files.

Getting Started

This plugin requires Grunt ~0.4.0

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: 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 ';' as the separator.

banner

Type: String Default: empty string

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: empty string

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 Default: false

Process source files as templates before concatenating.

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

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'
    }
  }
});

Release History

  • 2013-02-21   v0.1.3   Support footer option.
  • 2013-02-14   v0.1.2   First official release for Grunt 0.4.0.
  • 2013-01-17   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-08   v0.1.2rc5   Updating to work with grunt v0.4.0rc5. Switching back to this.files api.
  • 2012-11-12   v0.1.1   Switch to this.file api internally.
  • 2012-10-02   v0.1.0   Work in progress, not yet officially released.

Task submitted by "Cowboy" Ben Alman

This file was generated on Fri Feb 22 2013 09:34:22.

grunt-contrib-concat's People

Contributors

cbotsikas avatar cowboy avatar shama avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.