You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
6.1 KiB

  1. [Grunt homepage](https://github.com/gruntjs/grunt) | [Documentation table of contents](toc.md)
  2. # concat (built-in task)
  3. Concatenate one or more input files (and/or [directives](helpers_directives.md) output, like `<banner>`) into an output file.
  4. ## About
  5. This task is a [multi task](types_of_tasks.md), meaning that grunt will automatically iterate over all `concat` targets if a target is not specified.
  6. _Need some help getting started with grunt? Visit the [getting started](getting_started.md) page. And if you're creating your own tasks or helpers, be sure to check out the [types of tasks](types_of_tasks.md) page as well as the [API documentation](api.md)._
  7. ## A Very Important Note
  8. Your `grunt.js` gruntfile **must** contain this code, once and **only** once. If it doesn't, grunt won't work. For the sake of brevity, this "wrapper" code has been omitted from all examples on this page, but it needs to be there.
  9. ```javascript
  10. module.exports = function(grunt) {
  11. // Your grunt code goes in here.
  12. };
  13. ```
  14. ## Project configuration
  15. This example shows a brief overview of the [grunt.js gruntfile](getting_started.md) config properties used by the `concat` task. For a more in-depth explanation, see the usage examples.
  16. ```javascript
  17. // Project configuration.
  18. grunt.initConfig({
  19. // Project metadata, used by the <banner> directive.
  20. meta: {},
  21. // Lists of files to be concatenated.
  22. concat: {}
  23. });
  24. ```
  25. ## Usage examples
  26. ### Concatenating multiple files
  27. In this example, running `grunt concat:dist` (or `grunt concat` because `concat` is a [multi task](types_of_tasks.md)) will simply concatenate the three specified source files, in order, writing the output to `dist/built.js`.
  28. ```javascript
  29. // Project configuration.
  30. grunt.initConfig({
  31. concat: {
  32. dist: {
  33. src: ['src/intro.js', 'src/project.js', 'src/outro.js'],
  34. dest: 'dist/built.js'
  35. }
  36. }
  37. });
  38. ```
  39. With a slight modification, running `grunt concat` will join the specified source files using `;` instead of the default newline character.
  40. ```javascript
  41. // Project configuration.
  42. grunt.initConfig({
  43. concat: {
  44. dist: {
  45. src: ['src/intro.js', 'src/project.js', 'src/outro.js'],
  46. dest: 'dist/built.js',
  47. separator: ';'
  48. }
  49. }
  50. });
  51. ```
  52. ### Banner comments
  53. In this example, running `grunt concat:dist` (or `grunt concat` because `concat` is a [multi task](types_of_tasks.md)) 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`.
  54. This generated banner will be the contents of the `meta.banner` underscore 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.
  55. _Note: you don't have to use an external JSON file. It's completely 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. See the [directives](helpers_directives.md) page for more information on the `<banner>` and `<json>` directives and their options._
  56. ```javascript
  57. // Project configuration.
  58. grunt.initConfig({
  59. pkg: '<json:package.json>',
  60. meta: {
  61. banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
  62. '<%= grunt.template.today("yyyy-mm-dd") %> */'
  63. },
  64. concat: {
  65. dist: {
  66. src: ['<banner>', '<file_strip_banner:src/project.js>'],
  67. dest: 'dist/built.js'
  68. }
  69. }
  70. });
  71. ```
  72. ### Multiple build targets
  73. 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`.
  74. 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](types_of_tasks.md).
  75. ```javascript
  76. // Project configuration.
  77. grunt.initConfig({
  78. concat: {
  79. basic: {
  80. src: ['src/main.js'],
  81. dest: 'dist/basic.js'
  82. },
  83. extras: {
  84. src: ['src/main.js', 'src/extras.js'],
  85. dest: 'dist/with_extras.js'
  86. }
  87. }
  88. });
  89. ```
  90. ### Dynamic filenames
  91. Filenames can be generated dynamically by using `<%= %>` delimited underscore templates as filenames.
  92. 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).
  93. ```javascript
  94. // Project configuration.
  95. grunt.initConfig({
  96. pkg: '<json:package.json>',
  97. concat: {
  98. dist: {
  99. src: ['src/main.js'],
  100. dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
  101. }
  102. },
  103. });
  104. ```
  105. ### Advanced dynamic filenames
  106. In this more involved example, running `grunt concat` will build two separate files (because `concat` is a [multi task](types_of_tasks.md)). The destination file paths will be expanded dynamically based on the specified underscore templates, recursively if necessary.
  107. 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.
  108. ```javascript
  109. // Project configuration.
  110. grunt.initConfig({
  111. pkg: '<json:package.json>',
  112. dirs: {
  113. src: 'src/files',
  114. dest: 'dist/<%= pkg.name %>/<%= pkg.version %>'
  115. },
  116. concat: {
  117. basic: {
  118. src: ['<%= dirs.src %>/main.js'],
  119. dest: '<%= dirs.dest %>/basic.js'
  120. },
  121. extras: {
  122. src: ['<%= dirs.src %>/main.js', '<%= dirs.src %>/extras.js'],
  123. dest: '<%= dirs.dest %>/with_extras.js'
  124. }
  125. }
  126. });
  127. ```
  128. ## Helpers
  129. A generic `concat` helper is available for use in any other task where file and/or [directive](helpers_directives.md) concatenation might be useful. In this example, a `;` separator is specified, although it defaults to linefeed if omitted:
  130. ```javascript
  131. var fooPlusBar = grunt.helper('concat', ['foo.txt', 'bar.txt'], {separator: ';'});
  132. ```
  133. See the [concat task source](../tasks/concat.js) for more information.