“扩展”选项在grunt-contrib-copy中做了什么?这些例子都使用它,但文档没有说它的作用

时间:2023-01-14 09:26:25
  1. Here is the README and examples: https://github.com/gruntjs/grunt-contrib-copy/blob/master/README.md.
  2. 以下是README和示例:https://github.com/gruntjs/grunt-contrib-copy/blob/master/README.md。
  3. Here is the relevant part of the code (that I apparently cannot understand) from https://github.com/gruntjs/grunt-contrib-copy/blob/master/tasks/copy.js:
  4. 以下是https://github.com/gruntjs/grunt-contrib-copy/blob/master/tasks/copy.js代码的相关部分(我显然无法理解):
module.exports = function(grunt) {
  'use strict';

  var path = require('path');

  grunt.registerMultiTask('copy', 'Copy files.', function() {
    var kindOf = grunt.util.kindOf;

    var options = this.options({
      processContent: false,
      processContentExclude: []
    });

    var copyOptions = {
      process: options.processContent,
      noProcess: options.processContentExclude
    };

    grunt.verbose.writeflags(options, 'Options');

    var dest;
    var isExpandedPair;
    var tally = {
      dirs: 0,
      files: 0
    };

    this.files.forEach(function(filePair) {
      isExpandedPair = filePair.orig.expand || false;

      filePair.src.forEach(function(src) {
        if (detectDestType(filePair.dest) === 'directory') {
          dest = (isExpandedPair) ? filePair.dest : unixifyPath(path.join(filePair.dest, src));
        } else {
          dest = filePair.dest;
        }

        if (grunt.file.isDir(src)) {
          grunt.verbose.writeln('Creating ' + dest.cyan);
          grunt.file.mkdir(dest);
          tally.dirs++;
        } else {
          grunt.verbose.writeln('Copying ' + src.cyan + ' -> ' + dest.cyan);
          grunt.file.copy(src, dest, copyOptions);
          tally.files++;
        }
      });
    });

2 个解决方案

#1


57  

Expand lets you specify whether you want to create the destination path in full (e.g: /path/missing1/missing2), or only create the last directory when its parent exists (/path/existing/missing).

Expand允许您指定是否要完全创建目标路径(例如:/ path / missing1 / missing2),或仅在其父项存在时创建最后一个目录(/ path / existing / missing)。

#2


74  

Since expand is a part of Grunt, and not specific for grunt-contrib-copy, information about it can be found in Grunt's file configuration API:

由于expand是Grunt的一部分,并不是特定于grunt-contrib-copy,因此可以在Grunt的文件配置API中找到有关它的信息:

Set expand to true to enable the following options:

将expand设置为true以启用以下选项:

  • cwd All src matches are relative to (but don't include) this path.
  • cwd所有src匹配都与此路径相关(但不包括)。
  • src Pattern(s) to match, relative to the cwd.
  • src要匹配的模式,相对于cwd。
  • dest Destination path prefix.
  • dest目标路径前缀。
  • ext Replace any existing extension with this value in generated dest paths.
  • ext在生成的dest路径中用此值替换任何现有扩展。
  • extDot Used to indicate where the period indicating the extension is located. Can take either 'first' (extension begins after the first period in the file name) or 'last' (extension begins after the last period), and is set by default to 'first'.
  • extDot用于指示指示扩展名的句点所在的位置。可以采用“first”(扩展名在文件名中的第一个句点之后开始)或“last”(扩展名在最后一个句点之后开始),默认设置为“first”。
  • flatten Remove all path parts from generated dest paths.
  • flatten从生成的dest路径中删除所有路径部分。
  • rename This function is called for each matched src file, (after extension renaming and flattening). The dest and matched src path are passed in, and this function must return a new dest value. If the same dest is returned more than once, each src which used it will be added to an array of sources for it.
  • 重命名为每个匹配的src文件调用此函数(扩展名重命名和展平后)。传入dest和匹配的src路径,此函数必须返回新的dest值。如果多次返回相同的dest,则使用它的每个src将被添加到它的源数组中。

Additionally it seems like dest will always be considered to be a destination directory if setting expand to true.

此外,如果将expand设置为true,似乎dest将始终被视为目标目录。

#1


57  

Expand lets you specify whether you want to create the destination path in full (e.g: /path/missing1/missing2), or only create the last directory when its parent exists (/path/existing/missing).

Expand允许您指定是否要完全创建目标路径(例如:/ path / missing1 / missing2),或仅在其父项存在时创建最后一个目录(/ path / existing / missing)。

#2


74  

Since expand is a part of Grunt, and not specific for grunt-contrib-copy, information about it can be found in Grunt's file configuration API:

由于expand是Grunt的一部分,并不是特定于grunt-contrib-copy,因此可以在Grunt的文件配置API中找到有关它的信息:

Set expand to true to enable the following options:

将expand设置为true以启用以下选项:

  • cwd All src matches are relative to (but don't include) this path.
  • cwd所有src匹配都与此路径相关(但不包括)。
  • src Pattern(s) to match, relative to the cwd.
  • src要匹配的模式,相对于cwd。
  • dest Destination path prefix.
  • dest目标路径前缀。
  • ext Replace any existing extension with this value in generated dest paths.
  • ext在生成的dest路径中用此值替换任何现有扩展。
  • extDot Used to indicate where the period indicating the extension is located. Can take either 'first' (extension begins after the first period in the file name) or 'last' (extension begins after the last period), and is set by default to 'first'.
  • extDot用于指示指示扩展名的句点所在的位置。可以采用“first”(扩展名在文件名中的第一个句点之后开始)或“last”(扩展名在最后一个句点之后开始),默认设置为“first”。
  • flatten Remove all path parts from generated dest paths.
  • flatten从生成的dest路径中删除所有路径部分。
  • rename This function is called for each matched src file, (after extension renaming and flattening). The dest and matched src path are passed in, and this function must return a new dest value. If the same dest is returned more than once, each src which used it will be added to an array of sources for it.
  • 重命名为每个匹配的src文件调用此函数(扩展名重命名和展平后)。传入dest和匹配的src路径,此函数必须返回新的dest值。如果多次返回相同的dest,则使用它的每个src将被添加到它的源数组中。

Additionally it seems like dest will always be considered to be a destination directory if setting expand to true.

此外,如果将expand设置为true,似乎dest将始终被视为目标目录。