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.
Copies individual files or entire directories, which already exist, to the build directory.
new rspack.CopyRspackPlugin(options);import { rspack } from '@rspack/core';
export default {
entry: './src/index.js',
plugins: [
new rspack.CopyRspackPlugin({
// `./src/file.txt` -> `./dist/file.txt`
patterns: [{ from: 'src/file.txt' }],
}),
],
};patterns can be a string, or an array of objects.import { rspack } from '@rspack/core';
export default {
entry: './src/index.js',
plugins: [
new rspack.CopyRspackPlugin({
// This is equivalent to `patterns: [{ from: 'src/file.txt' }]`
patterns: 'src/file.txt',
}),
],
};import { rspack } from '@rspack/core';
export default {
entry: './src/index.js',
plugins: [
new rspack.CopyRspackPlugin({
// `./dir/**/*` -> `./dist`
// For example, `./dir/foo.txt` -> `./dist/foo.txt`
patterns: [{ from: 'dir' }],
}),
],
};import { rspack } from '@rspack/core';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export default {
entry: './src/index.js',
plugins: [
new rspack.CopyRspackPlugin({
// `./src/*.json` -> `./dist/*.json`
// 例如 `./src/foo.json` -> `./dist/foo.json`
patterns: [
{
from: '*.json',
context: path.join(__dirname, 'src'),
},
],
}),
],
};to to specify the destination path.import { rspack } from '@rspack/core';
export default {
entry: './src/index.js',
plugins: [
new rspack.CopyRspackPlugin({
// `./dir/**/*` -> `./dist/other-dir`
// For example, `./dir/foo.txt` -> `./dist/other-dir/foo.txt`
patterns: [{ from: 'dir', to: 'other-dir' }],
}),
],
};stringundefinedThe source path of the copy operation, which can be an absolute path, a relative path, or a glob pattern.
It can refer to a file or a directory. If a relative path is passed, it is relative to the context option.
import { rspack } from '@rspack/core';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
// relative path
{ from: 'relative/path/to/file.js' },
{ from: 'relative/path/to/dir' },
// absolute path
{ from: path.resolve(__dirname, 'src', 'file.js') },
{ from: path.resolve(__dirname, 'src', 'dir') },
// glob
{ from: 'dir/**/*' },
// If absolute path is a `glob` we replace backslashes with forward slashes,
// because only forward slashes can be used in the `glob`
{
from: path.posix.join(
path.resolve(__dirname, 'src').replace(/\\/g, '/'),
'*.txt',
),
},
],
}),
],
};type To =
| string
| ((pathData: { context: string; absoluteFilename?: string }) => string);The destination path of the copy operation, which can be an absolute path, a relative path, or a template string. If not specified, it is equal to Rspack's output.path.
export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
{
from: 'dir',
to: 'relative/path/to/dest/',
},
{
from: 'dir',
to: '/absolute/path/to/dest/',
},
{
from: 'dir',
to: '[path][name].[contenthash][ext]',
},
],
}),
],
};stringcontext is a path to be prepended to from and removed from the start of the result paths.
import { rspack } from '@rspack/core';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export default {
plugins: [
new rspack.CopyRspackPlugin({
// `./src/*.json` -> `./dist/*.json`
patterns: [{ from: '*.json', context: path.join(__dirname, 'src') }],
}),
],
};context can be an absolute path or a relative path. If it is a relative path, then it will be converted to an absolute path based on Rspack's context.
context should be explicitly set only when from contains a glob. Otherwise, context is automatically set based on whether from is a file or a directory:
from is a file, then context is its directory. The result path will be the filename alone.from is a directory, then context equals from. The result paths will be the paths of the directory's contents (including nested contents), relative to the directory.'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.
The automatic inference rules are as follows:
dir: If to has no extension, or ends on /.file: If to is not a directory and is not a template.template: If to contains a template pattern.Examples:
dir:export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
{
from: 'path/to/file.txt',
to: 'directory/with/extension.ext',
toType: 'dir',
},
],
}),
],
};file:export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
{
from: 'path/to/file.txt',
to: 'file/without/extension',
toType: 'file',
},
],
}),
],
};template:export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
{
from: 'src/',
to: 'dest/[name].[contenthash][ext]',
toType: 'template',
},
],
}),
],
};booleanfalseWhether to ignore the error if there are missing files or directories.
export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
{
from: path.resolve(__dirname, 'missing-file.txt'),
noErrorOnMissing: true,
},
],
}),
],
};booleanfalseWhether to overwrite the asset if it already exists.
export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [{ from: 'file.txt', force: true }],
}),
],
};number0Allows to specify the priority of copying files with the same destination name.
When force is set to true, if a matching file is found, the one with higher priority will overwrite the one with lower priority.
export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
// Copied first
{
from: 'dir-1/file.txt',
to: 'newfile.txt',
priority: 5,
},
// Copied second and will overwrite "dir-1/file.txt"
{
from: 'dir-2/file.txt',
to: 'newfile.txt',
force: true,
priority: 10,
},
],
}),
],
};type GlobOptions = {
// Whether the match is case sensitive
// @default true
caseSensitiveMatch?: boolean;
// Whether to match files starting with `.`
// @default true
dot?: boolean;
// An array of strings in glob format, which can be used to ignore specific paths
// @default undefined
ignore?: string[];
};undefinedSet the glob options for the copy operation.
export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
{
from: 'public/**/*',
globOptions: {
dot: false,
caseSensitiveMatch: false,
ignore: ['**/file.*', '**/ignored-directory/**'],
},
},
],
}),
],
};type transform =
| {
transformer: (
input: Buffer,
absoluteFilename: string,
) => string | Buffer | Promise<string> | Promise<Buffer>;
}
| ((
input: Buffer,
absoluteFilename: string,
) => string | Buffer | Promise<string> | Promise<Buffer>);undefinedAllows to modify the file contents.
export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
{
from: 'src/*.png',
// The `content` argument is a [`Buffer`](https://nodejs.org/api/buffer.html) object,
// it could be converted to a string to be processed using `content.toString()`.
// The `absoluteFilename` argument is a string, it is absolute path from where the file is being copied.
transform(content, absoluteFilename) {
return optimize(content);
},
},
],
}),
],
};booleanfalseWhether to copy the file permissions from the source to the destination.
export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
{
from: 'src/executable.sh',
copyPermissions: true, // Preserves the executable permission
},
],
}),
],
};This is particularly useful when copying executable files, scripts, or any files where permissions are important. When set to true, the plugin will attempt to set the same permissions on the destination file as the source file has.
type Info = {
immutable?: boolean;
// Whether to skip minification for the copied files.
// @default false
minimized?: boolean;
chunkHash?: string[];
contentHash?: string[];
development?: boolean;
hotModuleReplacement?: boolean;
related?: {
sourceMap?: string;
};
version?: string;
};undefinedAllows to add some assets info to the copied files, which may affect some behaviors in the build process.
For example, by default, the copied JS and CSS files will be minified by Rspack's minimizer, if you want to skip minification for copied files, you can set info.minimized to true.
export default {
plugins: [
new rspack.CopyRspackPlugin({
patterns: [
{
from: 'src/file.js',
info: { minimized: true },
},
],
}),
],
};