Rush Stack商店博客活动
跳过到主要内容

复制文件插件

插件包@rushstack/heft (内置)
插件名称copy-files-pluginCopyFilesPlugin.ts 实现
插件配置文件(无)
heft.json 选项copy-files-options.schema.json

此插件使用各种通配符复制指定的文件或文件夹。

何时使用它

典型使用场景

  • 将资产文件(如图像或字体)复制到“dist”文件夹中
  • 在编译之前将 .d.ts 文件复制到 temp/typings 文件夹中
  • 复制 node_modules 依赖项以进行重新打包

一些注意事项

  • 不建议使用 copy-files-plugin 将资产复制到 TypeScript 发射文件夹中;请改用 staticAssetsToCopy,因为它与 additionalModuleKindsToEmit 和监视模式更好地协同工作。
  • 避免使用此任务读取/写入项目文件夹之外的文件。这样做将违反 Rush 的 项目隔离原则
  • 在可能的情况下,避免使用 ** 等低效的通配符,这些通配符会递归遍历目录树。这些磁盘密集型操作会减慢构建速度。
  • 过于广泛的通配符有时会包含 Git 未跟踪的杂散文件夹。

package.json 依赖项

无 - 此功能内置于 @rushstack/heft 中。

配置

copy-files-plugin 是一个内置插件,直接从 @rushstack/heft 加载。以下是加载此插件的任务的代码示例

<项目文件夹>/config/heft.json

{
"$schema": "https://developer.microsoft.com/json-schemas/heft/v0/heft.schema.json",
"extends": "@rushstack/heft-web-rig/profiles/library/config/heft.json",

"phasesByName": {
// ("build" is a user-defined name, not a schema field)
"build": {
"tasksByName": {
// ("post-compile-copy" is a user-defined name, not a schema field)
"post-compile-copy": {
// The "post-compile-copy" task should not run until after "typescript" completes
"taskDependencies": ["typescript"],

"taskPlugin": {
"pluginName": "copy-files-plugin",
"pluginPackage": "@rushstack/heft",

// --------------------------------------------------------------
// EXAMPLE OPTIONS FOR copy-files-plugin
"options": {
"copyOperations": [
{
"sourcePath": "assets/images",
"destinationFolders": ["dist"],
"fileExtensions": [ ".png", ".jpg" ]
}
]
}
// --------------------------------------------------------------
}
}
}
}
}
}

heft.json 插件选项

此注释模板记录了可用选项。在上面的示例中,它将粘贴到 ------ 栏之间。

// OPTIONS FOR copy-files-plugin
// JSON Schema: https://developer.microsoft.com/json-schemas/heft/v0/copy-files-options.schema.json
"options": {
/**
* An array of copy operations to be performed by this task.
*/
"copyOperations": [
/**
* (REQUIRED) One or more folders that files and folders will be copied into,
* relative to the project root.
*/
"destinationFolders": [ "dist" ],

/**
* Absolute path to the source file or folder, relative to the project root.
* If "fileExtensions", "excludeGlobs", or "includeGlobs" are specified, then "sourcePath"
* is assumed to be a folder; if it is not a folder, an error will be thrown.
* Settings such as "includeGlobs" and "excludeGlobs" will be resolved relative to this path.
* If no globs or file extensions are specified, the entire folder will be copied.
* If this parameter is not provided, it defaults to the project root.
*/
// "sourcePath": "assets/images",

/**
* If specified, this option recursively scans all folders under "sourcePath" and includes
* any files that match the specified extensions. If "fileExtensions" and "includeGlobs"
* are both specified, their selections are added together.
*/
// "fileExtensions": [ ".png" ],

/**
* A list of glob patterns that select files to be copied. The paths are resolved relative
* to "sourcePath", which must be a folder path. If "fileExtensions" and "includeGlobs"
* are both specified, their selections are added together.
*
* For glob syntax, refer to: https://npmjs.net.cn/package/fast-glob
*/
// "includeGlobs": [],

/**
* A list of glob patterns that exclude files or folders from being copied. The paths are resolved
* relative to "sourcePath", which must be a folder path. These exclusions eliminate items that
* were selected by the "includeGlobs" or "fileExtensions" setting.
*
* For glob syntax, refer to: https://npmjs.net.cn/package/fast-glob
*/
// "excludeGlobs": [ "**/temp" ],

/**
* Normally, copying will preserve the path relative to "sourcePath" under the destination folder.
* (For example, if "sourcePath" is "src/test" and "destinationFolders" is ["out"], then
* "src/test/a/b/c.txt" will be copied to "out/a/b/c.txt".) Specify "flatten: true" to discard
* path information and keep only the filename (for example, "out/c.txt"). If two files have
* the same name, an error will be reported. The default value is false.
*/
// "flatten": true,

/**
* If true, filesystem hard links will be created instead of copying the file. Depending on the
* operating system, this may be faster. The default value is false.
*
* NOTE: This may cause unexpected behavior if a tool modifies the link. The contained directory
* structure will be re-created and all files will be individually hardlinked. This means that
* folders will be new filesystem entities and will have separate folder metadata, while the
* contained files will maintain normal hardlink behavior. This is done since folders do not
* have a cross-platform equivalent of a hardlink, and since file symlinks provide fundamentally
* different functionality in comparison to hardlinks.
*/
// "hardlink": true
]
}