Repository: webpack-contrib/copy-webpack-plugin (js)

Issue:
Ignore file that does not exist
Hello, is there a way to ignore the files that does not exist instead of throwing errors?

Pull request:
feat: emit warning instead error if file doesn't exist
<!--
  HOLY CRAP a Pull Request. We ❤️ those!

  If you remove or skip this template, you'll make the 🐼 sad and the mighty god
  of Github will appear and pile-drive the close button from a great height
  while making animal noises.

  Please place an x (no spaces!) in all [ ] that apply
-->

This PR contains a:

- [ ] **bugfix**
- [x] new **feature**
- [ ] **code refactor**
- [x] **test update** <!-- if bug or feature is checked, this should be too -->
- [ ] **typo fix**
- [ ] **metadata update**

### Motivation / Use-Case

fixes #126

### Breaking Changes

Yes

plugin emit warning instead error if file doesn't exist

### Additional Info

No

Patch:
diff --git a/src/preProcessPattern.js b/src/preProcessPattern.js
index 234249e..19a9f42 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';


Test patch:
diff --git a/test/CopyPlugin.test.js b/test/CopyPlugin.test.js
index b7307f9..8794d2e 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: [
           {