diff --git a/src/preProcessPattern.js b/src/preProcessPattern.js index 234249e1..19a9f42f 100644 --- a/src/preProcessPattern.js +++ b/src/preProcessPattern.js @@ -95,16 +95,21 @@ export default function preProcessPattern(globalRef, pattern) { // when we already in watch mode and this directories are not in context dependencies contextDependencies.add(pattern.context); } else { - const msg = `unable to locate '${pattern.from}' at '${ - pattern.absoluteFrom - }'`; - const warningMsg = `[copy-webpack-plugin] ${msg}`; - - // only display the same message once - if (compilation.errors.indexOf(warningMsg) === -1) { - warning(msg); - - compilation.errors.push(warningMsg); + const newWarning = new Error( + `[copy-webpack-plugin] unable to locate '${pattern.from}' at '${ + pattern.absoluteFrom + }'` + ); + const hasWarning = compilation.warnings.some( + // eslint-disable-next-line no-shadow + (warning) => warning.message === newWarning.message + ); + + // Only display the same message once + if (!hasWarning) { + warning(newWarning.message); + + compilation.warnings.push(newWarning); } pattern.fromType = 'nonexistent'; diff --git a/test/CopyPlugin.test.js b/test/CopyPlugin.test.js index b7307f9c..8794d2ec 100644 --- a/test/CopyPlugin.test.js +++ b/test/CopyPlugin.test.js @@ -108,6 +108,7 @@ describe('apply function', () => { { assets: {}, errors: [], + warnings: [], fileDependencies: new Set(), contextDependencies: new Set(), }, @@ -142,6 +143,13 @@ describe('apply function', () => { } else if (compilation.errors.length > 0) { throw compilation.errors[0]; } + + if (opts.expectedWarnings) { + expect(compilation.warnings).toEqual(opts.expectedWarnings); + } else if (compilation.warnings.length > 0) { + throw compilation.warnings[0]; + } + resolve(compilation); }) .catch(reject); @@ -206,6 +214,7 @@ describe('apply function', () => { const compilation = { assets: {}, errors: [], + warnings: [], fileDependencies: new Set(), contextDependencies: new Set(), }; @@ -756,10 +765,12 @@ describe('apply function', () => { it('warns when file not found', (done) => { runEmit({ expectedAssetKeys: [], - expectedErrors: [ - `[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${ - path.sep - }nonexistent.txt'`, + expectedWarnings: [ + new Error( + `[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${ + path.sep + }nonexistent.txt'` + ), ], patterns: [ { @@ -775,10 +786,12 @@ describe('apply function', () => { runEmit({ compiler: new MockCompilerNoStat(), expectedAssetKeys: [], - expectedErrors: [ - `[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${ - path.sep - }nonexistent.txt'`, + expectedWarnings: [ + new Error( + `[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${ + path.sep + }nonexistent.txt'` + ), ], patterns: [ { @@ -1414,10 +1427,12 @@ describe('apply function', () => { it('warns when directory not found', (done) => { runEmit({ expectedAssetKeys: [], - expectedErrors: [ - `[copy-webpack-plugin] unable to locate 'nonexistent' at '${HELPER_DIR}${ - path.sep - }nonexistent'`, + expectedWarnings: [ + new Error( + `[copy-webpack-plugin] unable to locate 'nonexistent' at '${HELPER_DIR}${ + path.sep + }nonexistent'` + ), ], patterns: [ {