[grunt] grunt-contrib-concat 介绍
发布于 4 年前 作者 qjzd 2082 次浏览 来自 码农

git地址

https://github.com/gruntjs/grunt-contrib-concat

介绍

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:

这个插件依赖Grunt  `>=0.4.0`
如果你之前还没有接触过Grunt, 务必先阅读Grunt入门指南, 它会解释如何创建一个GruntFile,以及安装使用Grunt插件。一旦你熟悉了这些流程, 你就可以使用以下命令安装插件。
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:

一旦这个插件已经安装,你可以在Gruntfile添加以下一行,来启用这个插件。
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.

使用grunt concat命令运行任务, 根据Grunt指南,任务的目标,文件和选项可以被指定。

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.

将加入这个字符串连接文件。如果你后期处理连接用压缩过JavaScript文件, 您可能需要使用分号“;\ n”作为分隔符。

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.

这个字符串将添加到连接输出文件的头部。它是使用[grunt.template处理的。

(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.
带上源代码上的注释。
“false”——任何注释都不会带上。
“true”——“/ *…* /”注释块会北带上,但不会带上“/ * !…* /的注释。
“options”对象:
在默认情况下,  表现就好像设置了true
“block”——如果为true,则所有块注释都会被带上
“line”——如果为true,任何连续“/ /”注释将会被带上。

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.
在连接前,处理源代码。或者用templates或者用自定义函数。
`false` - 不做任何处理。
`true` - 默认使用 [grunt.template.process][] 处理源代码
`data` object - 使用带有参数的 [grunt.template.process][]处理源代码
`function(src, filepath)` - 使用给定的函数处理源码,针对每一个文件都会调用一次,返回值会作为源码。

(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.

设置为true来创建一个源映射。这个源映射将在创建目标文件,并共享相同的文件名, 并在文件名末尾添加.map后缀

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.

自定义名字或者定位生成的源映射, 通过一个字符串来表示写源映射到的地方。 如果提供一个函数, 连接后的地址,目标参数和返回值将会作为文件名。
自定义生成的源图的名称或位置, 通过一个字符串来表示写源映射到的地方。如果提供一个函数, concat目的地传递参数和返回值将作为文件名。

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.

确定生成的源图的类型。默认值,“embed(嵌入)”,直接在源放进map。“link(链接)”将参考原始来源地图的链接。“inline(内联)”将整个硬黑色作为数据存储目标文件的URI。

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.

这个例子,运行grunt concat:dist或者grunt concat(因为concat是多任务的) 或连接三个指定源文件,使用;连接,输出到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.

这个例子, 运行grunt concat:dist将优先带上src/project.js文件先前的注释。 然后使用新生成的注释连接结果。写入到输出到dist/built.js。
这个生成的标识将“banner”的内容模板字符串插入配置对象。在这种情况下,这些属性的值从package.json的文件(可通过“pkg”配置属性)和今天的日期。
// 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'],
      },
    },
  },
});
回到顶部