@@ -321,6 +321,35 @@ const notIgnored = () => false;
321321/** @type {IgnoreMatcher } */
322322const NOTHING_IGNORED = { file : notIgnored , directory : notIgnored } ;
323323
324+ /**
325+ * Whether a path is `output.path` or sits below it.
326+ * @callback InOutputPathMatcher
327+ * @param {string } normalizedPath path with `/` separators
328+ * @returns {boolean } true when the path is the output directory or inside it
329+ */
330+
331+ /**
332+ * @param {string } outputPath `output.path` with `/` separators
333+ * @returns {InOutputPathMatcher } what is inside the output directory
334+ */
335+ const createInOutputPathMatcher = ( outputPath ) => {
336+ const inside = `${ outputPath } /` ;
337+ return ( normalizedPath ) =>
338+ normalizedPath === outputPath || normalizedPath . startsWith ( inside ) ;
339+ } ;
340+
341+ /**
342+ * @param {InOutputPathMatcher } isInOutputPath what is inside the output directory
343+ * @param {IgnoreMatcher } ignore what the pattern itself does not copy
344+ * @returns {IgnoreMatcher } the same, with the output directory added
345+ */
346+ const ignoringOutputPath = ( isInOutputPath , ignore ) => ( {
347+ file : ( normalizedPath ) =>
348+ isInOutputPath ( normalizedPath ) || ignore . file ( normalizedPath ) ,
349+ directory : ( normalizedPath ) =>
350+ isInOutputPath ( normalizedPath ) || ignore . directory ( normalizedPath )
351+ } ) ;
352+
324353// the tail of a glob naming everything inside a directory, which names the
325354// directory itself once it is gone
326355const IGNORED_DIRECTORY_CONTENTS_REGEXP = / \/ \* \* (?: \/ \* ) ? $ / ;
@@ -474,10 +503,14 @@ class CopyPlugin {
474503 caseSensitive : globOptions . caseSensitive ,
475504 requireLiteralLeadingDot : globOptions . dot === false
476505 } ;
477- const ignore = this . _createIgnoreMatcher (
478- context ,
479- globOptions . ignore ,
480- matchOptions
506+ // a copy reading its own output copies it one directory deeper on every
507+ // build, which invalidates the watch and grows the output without bound
508+ const isInOutputPath = createInOutputPathMatcher (
509+ normalizePathSeparatorsForPath ( compiler . outputPath )
510+ ) ;
511+ const ignore = ignoringOutputPath (
512+ isInOutputPath ,
513+ this . _createIgnoreMatcher ( context , globOptions . ignore , matchOptions )
481514 ) ;
482515 /** @type {Set<string> } */
483516 const links = new Set ( ) ;
@@ -489,6 +522,7 @@ class CopyPlugin {
489522 globOptions,
490523 matchOptions,
491524 ignore,
525+ isInOutputPath,
492526 links
493527 } )
494528 ) ;
@@ -570,14 +604,15 @@ class CopyPlugin {
570604 * @param {CopyGlobOptions } options.globOptions options of the glob
571605 * @param {GlobMatchOptions } options.matchOptions options the glob is matched with
572606 * @param {IgnoreMatcher } options.ignore what is not copied
607+ * @param {InOutputPathMatcher } options.isInOutputPath what is inside the output directory
573608 * @param {Set<string> } options.links absolute paths of the symlinks copied as links, filled by the walk
574609 * @returns {Promise<ResolvedFrom> } the files it reaches
575610 */
576611 async _resolveFrom (
577612 compiler ,
578613 compilation ,
579614 logger ,
580- { from, context, globOptions, matchOptions, ignore, links }
615+ { from, context, globOptions, matchOptions, ignore, isInOutputPath , links }
581616 ) {
582617 const fs = /** @type {InputFileSystem } */ ( compiler . inputFileSystem ) ;
583618 const followSymlinks = globOptions . followSymlinks !== false ;
@@ -657,16 +692,20 @@ class CopyPlugin {
657692 ) ;
658693 } ) ;
659694
660- await this . _addPatternDependency (
661- fs ,
662- compilation ,
663- typeOfFrom ,
664- absoluteFrom ,
665- base ,
666- // only a glob can name a base which is not there — a directory `from`
667- // was stat'd above, and a file one watches itself
668- entries . length === 0 && typeOfFrom === "glob"
669- ) ;
695+ // a base inside the output directory copies nothing, so watching it would
696+ // only rebuild on what this compilation itself wrote there
697+ if ( ! isInOutputPath ( normalizedBase ) ) {
698+ await this . _addPatternDependency (
699+ fs ,
700+ compilation ,
701+ typeOfFrom ,
702+ absoluteFrom ,
703+ base ,
704+ // only a glob can name a base which is not there — a directory `from`
705+ // was stat'd above, and a file one watches itself
706+ entries . length === 0 && typeOfFrom === "glob"
707+ ) ;
708+ }
670709
671710 return { from, typeOfFrom, glob, base, entries } ;
672711 }
0 commit comments