CC 4.0 License

The content of this section is derived from the content of the following links and is subject to the CC BY 4.0 license.

The following contents can be assumed to be the result of modifications and deletions based on the original contents if not specifically stated.

CopyRspackPlugin

Rspack only

Copies individual files or entire directories, which already exist, to the build directory.

new rspack.CopyRspackPlugin(options);
  • options

    • Type:
    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>;
          }
      )[];
    };
    • Default: undefined
    NameTypeDefaultDescription
    fromstringundefinedThe source path of the copy operation, which can be an absolute path, a relative path, or a glob search string. It can refer to a file or a directory. If a relative path is passed, it is relative to the context configuration.
    tostring | ((pathData: { context: string; absoluteFilename?: string }) => string)undefinedThe destination of the copy operation, which can be an absolute path, a relative path, or a template string, such as '[name].[hash][ext]'. If not specified, it is equal to output path.
    contextstringundefinedThis configuration determines how the "from" path is matched and the resulting structure after copying.
    toType'dir'|'file'|'template'undefinedSpecify the type of to, which can be a directory, a file, or a template name in rspack. If not specified, it will be automatically inferred.
    noErrorOnMissingbooleanfalseIgnore error if there are missing files or directories.
    forcebooleanfalseWhether to overwrite the asset if it already exist.
    prioritynumber0When force is set to true, if a matching file is found, the one with higher priority will overwrite the one with lower priority.
    globOptionsobjectundefinedThe configuration for glob queries: caseSensitiveMatch determines whether the matching is case sensitive, and dot determines whether files starting with . are matched. ignore is an array of strings in glob format that can be used to ignore specific paths.
    transformfunctionundefinedAllows to modify the file contents.

For example:

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

The result of running with the above configuration would be: "dist/file.txt".

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

The result of running with the above configuration would be: files and directories inside directory will be placed at output path.

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

The result of running with the above configuration would be that the directory folder is moved to the newdirectory folder within the output folder, for example dist/newdirectory/directory/foo.