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
What does the "expand" option do in grunt-contrib-copy? The examples all use it but the docs say nothing about what it does
提问by Patrick
- Here is the README and examples: https://github.com/gruntjs/grunt-contrib-copy/blob/master/README.md.
- 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:
- 这是自述文件和示例:https: //github.com/gruntjs/grunt-contrib-copy/blob/master/README.md。
- 这是来自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
expandtotrueto enable the following options:
cwdAllsrcmatches are relative to (but don't include) this path.srcPattern(s) to match, relative to thecwd.destDestination path prefix.extReplace any existing extension with this value in generateddestpaths.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 generateddestpaths.renameThis function is called for each matchedsrcfile, (after extension renaming and flattening). Thedestand matchedsrcpath are passed in, and this function must return a newdestvalue. If the samedestis returned more than once, eachsrcwhich used it will be added to an array of sources for it.
设置
expand为true启用以下选项:
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如果设置expand为true.

