Visitar URL original
feat(output): add the output.copy option by alexander-akait · Pull Request #21938 · webpack/webpack · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
454041e
feat(output): add the output.copy option
alexander-akait Aug 27, 2026
c625bc0
fix(output): stop the copy walk where the glob cannot reach
alexander-akait Sep 3, 2026
d3ba629
test(output): give the copy glob a non-matching file git keeps
alexander-akait Sep 3, 2026
454cbdd
fix(output): read the copy base under the name the watcher purges
alexander-akait Sep 3, 2026
dc1f818
test(output): cover the copy plugin's file-system fallbacks
alexander-akait Sep 3, 2026
c3d2144
fix(output): anchor a copy glob on the base the walk resolves to
alexander-akait Sep 3, 2026
1a8781e
perf(output): read the copy walk's entry types with the directory
alexander-akait Sep 3, 2026
2cef17a
fix(output): read a copy pattern's windows separators as separators
alexander-akait Sep 3, 2026
ec2c03a
docs(output): say where a copy pattern lands and what its glob takes
alexander-akait Sep 3, 2026
3cd1f28
feat(output): let a copy pattern set case sensitivity and symlink fol…
alexander-akait Sep 3, 2026
4151b8e
feat(output): gather a copy pattern's glob settings under globOptions
alexander-akait Sep 3, 2026
3a48983
feat(output): widen the output.copy option surface
alexander-akait Sep 4, 2026
c43c68b
feat(output): let a copy pattern pick 'to' per file
alexander-akait Sep 4, 2026
9bd63ed
docs(examples): show output.copy, and transformAll as a plugin
alexander-akait Sep 4, 2026
4a9dcca
feat(output): copy a symbolic link as a symbolic link
alexander-akait Sep 4, 2026
97ccfd5
feat(output): let output.copy pick the stage it copies at
alexander-akait Sep 4, 2026
4bf3f10
feat(output): add 'preserveTimestamps' to an 'output.copy' pattern
alexander-akait Sep 5, 2026
3a3bd3e
fix: stamp an asset's times onto a file the emit did not rewrite
alexander-akait Sep 5, 2026
3605850
feat(output): add 'preservePermissions' to an 'output.copy' pattern
alexander-akait Sep 5, 2026
4a389b5
chore: say what the copy reload TODO needs
alexander-akait Sep 5, 2026
5188088
refactor(output): copy with webpack's own async helpers
alexander-akait Sep 6, 2026
6d4ad52
fix(test): build the copy symlink fixture on the node baseline
alexander-akait Sep 6, 2026
9080100
fix(test): keep the copy fixtures on LF
alexander-akait Sep 6, 2026
42e840e
fix(output): stop a copy replacing an asset the compilation emits
alexander-akait Sep 7, 2026
c23849f
feat(output): take only patterns in 'output.copy'
alexander-akait Sep 7, 2026
af550c1
docs(examples): show 'output.copy' used as a plugin
alexander-akait Sep 7, 2026
37f90d8
fix(output): keep a copy inside 'output.path' and off stale asset info
alexander-akait Sep 7, 2026
06195d8
fix(stats): give 'copied' a place in the asset info order
alexander-akait Sep 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(output): gather a copy pattern's glob settings under globOptions
`caseSensitive` and `followSymlinks` move there, where the other copy plugins
keep them, and `dot` joins them: false leaves a file or a directory whose name
starts with a dot to a pattern which names it, as `globUtils` already knew how
to and the plugin never asked for. All three default to true, so a pattern
without `globOptions` copies what it did before.
  • Loading branch information
alexander-akait committed Sep 7, 2026
commit 4151b8ec80e2ec19ec81711ebe744d7c84a70cf5
29 changes: 21 additions & 8 deletions declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3199,22 +3199,18 @@ export interface CleanOptions {
* A pattern of files which are copied to the output directory.
*/
export interface CopyObjectPattern {
/**
* Whether the glob matches the case of a file name. Defaults to 'true'.
*/
caseSensitive?: boolean;
/**
* Filename template of a copied file inside 'to'. Defaults to '[path][base]', which keeps the name and the directory structure below 'from'.
*/
filename?: string | import("../lib/CopyPlugin").CopyFilenameFunction;
/**
* Whether a symbolic link is walked into and copied. Defaults to 'true'.
*/
followSymlinks?: boolean;
/**
* Glob or path from where the files are copied. A glob separates with '/', matches dot files, and takes '*', '**', '?', '[]' and '{}'.
*/
from: string;
/**
* Options of the glob in 'from'.
*/
globOptions?: CopyGlobOptions;
/**
* Directory the files are copied to, relative to 'output.path', which is where they land by default.
*/
Expand All @@ -3224,6 +3220,23 @@ export interface CopyObjectPattern {
*/
transform?: import("../lib/CopyPlugin").CopyTransform;
}
/**
* Options of the glob in 'from'.
*/
export interface CopyGlobOptions {
/**
* Whether the glob matches the case of a file name. Defaults to 'true'.
*/
caseSensitive?: boolean;
/**
* Whether the glob reaches a file or a directory whose name starts with a dot without naming it. Defaults to 'true'.
*/
dot?: boolean;
/**
* Whether a symbolic link is walked into and copied. Defaults to 'true'.
*/
followSymlinks?: boolean;
}
/**
* The abilities of the environment where the webpack generated code should run.
*/
Expand Down
18 changes: 14 additions & 4 deletions lib/CopyPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { join } = require("./util/fs");
const {
escapeGlobPattern,
extractGlobBaseDir,
globMatchWithOptions,
globMatchNormalizedWithExplicitDot,
normalizePathSeparators,
normalizePathSeparatorsForPath,
unescapeGlobPath
Expand All @@ -30,6 +30,7 @@ const { ABSOLUTE_PATH_REGEXP } = require("./util/identifier");
/** @import { Snapshot } from "./FileSystemInfo" */
/** @import { Logger } from "./logging/Logger" */
/** @import { Dirent, InputFileSystem, IStats } from "./util/fs" */
/** @import { GlobMatchOptions } from "./util/globUtils" */
/** @import { Source } from "webpack-sources" */

/**
Expand Down Expand Up @@ -344,7 +345,8 @@ class CopyPlugin {
/** @type {CopyObjectPattern} */
const pattern = typeof item === "string" ? { from: item } : item;
const { from } = pattern;
const followSymlinks = pattern.followSymlinks !== false;
const globOptions = pattern.globOptions || {};
const followSymlinks = globOptions.followSymlinks !== false;
const absoluteFrom = path.isAbsolute(from)
? path.normalize(from)
: path.resolve(compiler.context, from);
Expand Down Expand Up @@ -390,15 +392,23 @@ class CopyPlugin {
);
logger.debug(`'${from}' is a ${typeOfFrom}, globbing '${glob}'`);

const matchOptions = { caseSensitive: pattern.caseSensitive };
// the glob is normalized already, and the base once here, so the match
// does not redo either for every entry
const normalizedBase = normalizePathSeparatorsForPath(base);
/** @type {GlobMatchOptions} */
const matchOptions = {
caseSensitive: globOptions.caseSensitive,
requireLiteralLeadingDot: globOptions.dot === false
};
const entries =
typeOfFrom === "file"
? [absoluteFrom]
: (await walkBase(fs, base, maxDepth, followSymlinks)).filter(
(filePath) =>
globMatchWithOptions(
globMatchNormalizedWithExplicitDot(
glob,
normalizePathSeparatorsForPath(filePath),
normalizedBase,
matchOptions
)
);
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions schemas/WebpackOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,30 @@
],
"added": "5.111.0"
},
"CopyObjectPattern": {
"description": "A pattern of files which are copied to the output directory.",
"CopyGlobOptions": {
"description": "Options of the glob in 'from'.",
"type": "object",
"additionalProperties": false,
"properties": {
"caseSensitive": {
"description": "Whether the glob matches the case of a file name. Defaults to 'true'.",
"type": "boolean"
},
"dot": {
"description": "Whether the glob reaches a file or a directory whose name starts with a dot without naming it. Defaults to 'true'.",
"type": "boolean"
},
"followSymlinks": {
"description": "Whether a symbolic link is walked into and copied. Defaults to 'true'.",
"type": "boolean"
}
}
},
"CopyObjectPattern": {
"description": "A pattern of files which are copied to the output directory.",
"type": "object",
"additionalProperties": false,
"properties": {
"filename": {
"description": "Filename template of a copied file inside 'to'. Defaults to '[path][base]', which keeps the name and the directory structure below 'from'.",
"anyOf": [
Expand All @@ -446,15 +461,14 @@
}
]
},
"followSymlinks": {
"description": "Whether a symbolic link is walked into and copied. Defaults to 'true'.",
"type": "boolean"
},
"from": {
"description": "Glob or path from where the files are copied. A glob separates with '/', matches dot files, and takes '*', '**', '?', '[]' and '{}'.",
"type": "string",
"minLength": 1
},
"globOptions": {
"$ref": "#/definitions/CopyGlobOptions"
},
"to": {
"description": "Directory the files are copied to, relative to 'output.path', which is where they land by default.",
"type": "string",
Expand Down
53 changes: 33 additions & 20 deletions test/__snapshots__/Cli.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13172,57 +13172,70 @@ Object {
"multiple": true,
"simpleType": "string",
},
"output-copy-case-sensitive": Object {
"output-copy-filename": Object {
"configs": Array [
Object {
"description": "Whether the glob matches the case of a file name. Defaults to 'true'.",
"description": "Filename template of a copied file inside 'to'. Defaults to '[path][base]', which keeps the name and the directory structure below 'from'.",
"multiple": true,
"path": "output.copy[].caseSensitive",
"type": "boolean",
"path": "output.copy[].filename",
"type": "string",
},
],
"description": "Whether the glob matches the case of a file name. Defaults to 'true'.",
"description": "Filename template of a copied file inside 'to'. Defaults to '[path][base]', which keeps the name and the directory structure below 'from'.",
"multiple": true,
"simpleType": "boolean",
"simpleType": "string",
},
"output-copy-filename": Object {
"output-copy-from": Object {
"configs": Array [
Object {
"description": "Filename template of a copied file inside 'to'. Defaults to '[path][base]', which keeps the name and the directory structure below 'from'.",
"description": "Glob or path from where the files are copied. A glob separates with '/', matches dot files, and takes '*', '**', '?', '[]' and '{}'.",
"multiple": true,
"path": "output.copy[].filename",
"path": "output.copy[].from",
"type": "string",
},
],
"description": "Filename template of a copied file inside 'to'. Defaults to '[path][base]', which keeps the name and the directory structure below 'from'.",
"description": "Glob or path from where the files are copied. A glob separates with '/', matches dot files, and takes '*', '**', '?', '[]' and '{}'.",
"multiple": true,
"simpleType": "string",
},
"output-copy-follow-symlinks": Object {
"output-copy-glob-options-case-sensitive": Object {
"configs": Array [
Object {
"description": "Whether a symbolic link is walked into and copied. Defaults to 'true'.",
"description": "Whether the glob matches the case of a file name. Defaults to 'true'.",
"multiple": true,
"path": "output.copy[].followSymlinks",
"path": "output.copy[].globOptions.caseSensitive",
"type": "boolean",
},
],
"description": "Whether a symbolic link is walked into and copied. Defaults to 'true'.",
"description": "Whether the glob matches the case of a file name. Defaults to 'true'.",
"multiple": true,
"simpleType": "boolean",
},
"output-copy-from": Object {
"output-copy-glob-options-dot": Object {
"configs": Array [
Object {
"description": "Glob or path from where the files are copied. A glob separates with '/', matches dot files, and takes '*', '**', '?', '[]' and '{}'.",
"description": "Whether the glob reaches a file or a directory whose name starts with a dot without naming it. Defaults to 'true'.",
"multiple": true,
"path": "output.copy[].from",
"type": "string",
"path": "output.copy[].globOptions.dot",
"type": "boolean",
},
],
"description": "Glob or path from where the files are copied. A glob separates with '/', matches dot files, and takes '*', '**', '?', '[]' and '{}'.",
"description": "Whether the glob reaches a file or a directory whose name starts with a dot without naming it. Defaults to 'true'.",
"multiple": true,
"simpleType": "string",
"simpleType": "boolean",
},
"output-copy-glob-options-follow-symlinks": Object {
"configs": Array [
Object {
"description": "Whether a symbolic link is walked into and copied. Defaults to 'true'.",
"multiple": true,
"path": "output.copy[].globOptions.followSymlinks",
"type": "boolean",
},
],
"description": "Whether a symbolic link is walked into and copied. Defaults to 'true'.",
"multiple": true,
"simpleType": "boolean",
},
"output-copy-reset": Object {
"configs": Array [
Expand Down
10 changes: 10 additions & 0 deletions test/configCases/copy/glob/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@ it("should ignore the case when 'caseSensitive' is false", () => {
expect(read("insensitive/Upper.TXT")).toBe("upper");
expect(read("insensitive/keep.txt")).toBe("keep");
});

it("should reach a dot file the glob does not name by default", () => {
expect(read("all/.dot.txt")).toBe("dot");
});

it("should leave a dot file alone when 'dot' is false", () => {
expect(read("no-dot/keep.txt")).toBe("keep");
expect(exists("no-dot/.dot.txt")).toBe(false);
expect(exists("no-dot/.hidden/inside.txt")).toBe(false);
});
11 changes: 10 additions & 1 deletion test/configCases/copy/glob/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ module.exports = {
from: [path.resolve(__dirname, "assets"), "*.txt"].join("\\"),
to: "backslash"
},
{ from: "assets/*.txt", caseSensitive: false, to: "insensitive" }
{
from: "assets/*.txt",
globOptions: { caseSensitive: false },
to: "insensitive"
},
{
from: "assets/**/*.txt",
globOptions: { dot: false },
to: "no-dot"
}
]
}
};
2 changes: 1 addition & 1 deletion test/configCases/copy/symlink/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
output: {
copy: [
{ from: source },
{ from: source, followSymlinks: false, to: "no-follow" }
{ from: source, globOptions: { followSymlinks: false }, to: "no-follow" }
]
},
plugins: [
Expand Down
23 changes: 19 additions & 4 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5338,29 +5338,44 @@ declare interface ContextTimestampAndHash {
type ContextTypes = KnownContext & Record<any, any>;

/**
* A pattern of files which are copied to the output directory.
* Options of the glob in 'from'.
*/
declare interface CopyObjectPattern {
declare interface CopyGlobOptions {
/**
* Whether the glob matches the case of a file name. Defaults to 'true'.
*/
caseSensitive?: boolean;

/**
* Filename template of a copied file inside 'to'. Defaults to '[path][base]', which keeps the name and the directory structure below 'from'.
* Whether the glob reaches a file or a directory whose name starts with a dot without naming it. Defaults to 'true'.
*/
filename?: string | ((pathData: PathData, assetInfo?: AssetInfo) => string);
dot?: boolean;

/**
* Whether a symbolic link is walked into and copied. Defaults to 'true'.
*/
followSymlinks?: boolean;
}

/**
* A pattern of files which are copied to the output directory.
*/
declare interface CopyObjectPattern {
/**
* Filename template of a copied file inside 'to'. Defaults to '[path][base]', which keeps the name and the directory structure below 'from'.
*/
filename?: string | ((pathData: PathData, assetInfo?: AssetInfo) => string);

/**
* Glob or path from where the files are copied. A glob separates with '/', matches dot files, and takes '*', '**', '?', '[]' and '{}'.
*/
from: string;

/**
* Options of the glob in 'from'.
*/
globOptions?: CopyGlobOptions;

/**
* Directory the files are copied to, relative to 'output.path', which is where they land by default.
*/
Expand Down