javascript grunt-contrib-copy 中的“扩展”选项有什么作用?示例都使用它,但文档没有说明它的作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16977884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:39:19  来源:igfitidea点击:

What does the "expand" option do in grunt-contrib-copy? The examples all use it but the docs say nothing about what it does

javascriptgruntjs

提问by Patrick

  1. Here is the README and examples: https://github.com/gruntjs/grunt-contrib-copy/blob/master/README.md.
  2. 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:
  1. 这是自述文件和示例:https: //github.com/gruntjs/grunt-contrib-copy/blob/master/README.md
  2. 这是来自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++;
        }
      });
    });

采纳答案by Vladimir Georgiev

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

展开让您指定是要创建完整的目标路径(例如:)/path/missing1/missing2,还是仅在其父目录存在时创建最后一个目录(/path/existing/missing)。

回答by David P?rsson

Since expandis 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 expandto trueto enable the following options:

  • cwdAll srcmatches are relative to (but don't include) this path.
  • srcPattern(s) to match, relative to the cwd.
  • destDestination path prefix.
  • extReplace any existing extension with this value in generated destpaths.
  • extDotUsed 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'.
  • flattenRemove all path parts from generated destpaths.
  • renameThis function is called for each matched srcfile, (after extension renaming and flattening). The destand matched srcpath are passed in, and this function must return a new destvalue. If the same destis returned more than once, each srcwhich used it will be added to an array of sources for it.

设置expandtrue启用以下选项:

  • cwd所有src匹配项都相对于(但不包括)此路径。
  • src要匹配的模式,相对于cwd.
  • dest目标路径前缀。
  • ext在生成的dest路径中用这个值替换任何现有的扩展。
  • extDot用于指示指示分机的句点所在的位置。可以采用'first'(扩展名在文件名中的第一个句点之后开始)或'last'(扩展名在最后一个句点之后开始),并且默认设置为'first'.
  • flatten从生成的dest路径中删除所有路径部分。
  • rename为每个匹配的src文件调用此函数(在扩展名重命名和扁平化之后)。在dest与匹配的src路径传递,而这个函数必须返回一个新dest值。如果dest多次返回相同的值,src则使用它的每个都将添加到它的源数组中。

Additionally it seems like destwill always be considered to be a destination directory if setting expandto true.

此外,dest如果设置expandtrue.