CC 4.0 许可证

本节内容源于以下链接的内容,并受 CC BY 4.0 许可证约束。

以下内容可以假设为基于原始内容进行修改和删除的结果,除非另有说明。

CopyRspackPlugin

Rspack 

将已存在的单个文件或整个目录复制到构建目录。

new rspack.CopyRspackPlugin(options);
  • 选项

    • 类型
    export type CopyRspackPluginOptions = {
      patterns: (
        | string // If input is string, it's the same as { from: `your input string` }
        | {
            from: string;
            to?:
              | string
              | ((pathData: {
                  context: string;
                  absoluteFilename?: string;
                }) => string); // Determine based on `from`
            context?: string; // Default to `context` in Rspack config
            toType?: 'dir' | 'file' | 'template'; // Determine based on `from`
            noErrorOnMissing?: boolean; // Default to false
            force?: boolean; // Default to false
            priority?: number; // Default to 0
            globOptions?: {
              caseSensitiveMatch?: boolean; // Default to true
              dot?: boolean; // Default to true
              ignore?: string[]; // ignore specific path
            };
            transform?: (
              input: Buffer,
              absoluteFilename: string,
            ) => string | Buffer | Promise<string> | Promise<Buffer>;
          }
      )[];
    };
    • 默认: undefined
    名称类型默认描述
    from字符串未定义复制操作的源路径,可以是绝对路径、相对路径或 glob 搜索字符串。它可以引用文件或目录。如果传递的是相对路径,则相对于 context 配置。
    to字符串 | ((pathData: { context: string; absoluteFilename?: string }) => string)未定义复制操作的目标,可以是绝对路径、相对路径或模板字符串,例如 '[name].[hash][ext]'。如果未指定,则等于输出路径。
    context字符串未定义此配置确定如何匹配 "from" 路径以及复制后的结果结构。
    toType'dir'|'file'|'template'未定义指定 to 的类型,可以是 rspack 中的目录、文件或模板名称。如果未指定,它将自动推断。
    noErrorOnMissing布尔值false如果存在缺少的文件或目录,则忽略错误。
    force布尔值false如果资产已存在,是否覆盖它。
    priority数字0force 设置为 true 时,如果找到匹配的文件,则优先级更高的文件将覆盖优先级较低的文件。
    globOptions对象未定义glob 查询的配置:caseSensitiveMatch 确定匹配是否区分大小写,dot 确定是否匹配以 . 开头的文件。ignore 是一个以 glob 格式表示的字符串数组,可用于忽略特定路径。
    transform函数未定义允许修改文件内容。

例如

rspack.config.js
const rspack = require('@rspack/core');
module.exports = {
  entry: './src/index.js',
  plugins: [
    new rspack.CopyRspackPlugin({
      patterns: [
        {
          from: 'file.txt',
        },
      ],
    }),
  ],
};

使用上述配置运行的结果将是:"dist/file.txt"

rspack.config.js
const rspack = require('@rspack/core');
module.exports = {
  entry: './src/index.js',
  plugins: [
    new rspack.CopyRspackPlugin({
      patterns: [
        {
          from: 'directory',
        },
      ],
    }),
  ],
};

使用上述配置运行的结果将是:directory 内部的文件和目录将被放置在输出路径中。

rspack.config.js
const rspack = require('@rspack/core');
module.exports = {
  entry: './src/index.js',
  plugins: [
    new rspack.CopyRspackPlugin({
      patterns: [
        {
          from: 'directory/**/*',
          to: 'newdirectory',
        },
      ],
    }),
  ],
};

使用上述配置运行的结果是将 directory 文件夹移动到输出文件夹内的 newdirectory 文件夹中,例如 dist/newdirectory/directory/foo