Skip to content

Commit a1c5372

Browse files
authoredFeb 18, 2019
feat: emit warning instead error if file doesn't exist (#338)
BREAKING CHANGE: plugin emit warning instead error if file doesn't exist
1 parent f551c0d commit a1c5372

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed
 

‎src/preProcessPattern.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,21 @@ export default function preProcessPattern(globalRef, pattern) {
9595
// when we already in watch mode and this directories are not in context dependencies
9696
contextDependencies.add(pattern.context);
9797
} else {
98-
const msg = `unable to locate '${pattern.from}' at '${
99-
pattern.absoluteFrom
100-
}'`;
101-
const warningMsg = `[copy-webpack-plugin] ${msg}`;
102-
103-
// only display the same message once
104-
if (compilation.errors.indexOf(warningMsg) === -1) {
105-
warning(msg);
106-
107-
compilation.errors.push(warningMsg);
98+
const newWarning = new Error(
99+
`[copy-webpack-plugin] unable to locate '${pattern.from}' at '${
100+
pattern.absoluteFrom
101+
}'`
102+
);
103+
const hasWarning = compilation.warnings.some(
104+
// eslint-disable-next-line no-shadow
105+
(warning) => warning.message === newWarning.message
106+
);
107+
108+
// Only display the same message once
109+
if (!hasWarning) {
110+
warning(newWarning.message);
111+
112+
compilation.warnings.push(newWarning);
108113
}
109114

110115
pattern.fromType = 'nonexistent';

‎test/CopyPlugin.test.js

+27-12
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ describe('apply function', () => {
108108
{
109109
assets: {},
110110
errors: [],
111+
warnings: [],
111112
fileDependencies: new Set(),
112113
contextDependencies: new Set(),
113114
},
@@ -142,6 +143,13 @@ describe('apply function', () => {
142143
} else if (compilation.errors.length > 0) {
143144
throw compilation.errors[0];
144145
}
146+
147+
if (opts.expectedWarnings) {
148+
expect(compilation.warnings).toEqual(opts.expectedWarnings);
149+
} else if (compilation.warnings.length > 0) {
150+
throw compilation.warnings[0];
151+
}
152+
145153
resolve(compilation);
146154
})
147155
.catch(reject);
@@ -206,6 +214,7 @@ describe('apply function', () => {
206214
const compilation = {
207215
assets: {},
208216
errors: [],
217+
warnings: [],
209218
fileDependencies: new Set(),
210219
contextDependencies: new Set(),
211220
};
@@ -756,10 +765,12 @@ describe('apply function', () => {
756765
it('warns when file not found', (done) => {
757766
runEmit({
758767
expectedAssetKeys: [],
759-
expectedErrors: [
760-
`[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${
761-
path.sep
762-
}nonexistent.txt'`,
768+
expectedWarnings: [
769+
new Error(
770+
`[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${
771+
path.sep
772+
}nonexistent.txt'`
773+
),
763774
],
764775
patterns: [
765776
{
@@ -775,10 +786,12 @@ describe('apply function', () => {
775786
runEmit({
776787
compiler: new MockCompilerNoStat(),
777788
expectedAssetKeys: [],
778-
expectedErrors: [
779-
`[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${
780-
path.sep
781-
}nonexistent.txt'`,
789+
expectedWarnings: [
790+
new Error(
791+
`[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${
792+
path.sep
793+
}nonexistent.txt'`
794+
),
782795
],
783796
patterns: [
784797
{
@@ -1414,10 +1427,12 @@ describe('apply function', () => {
14141427
it('warns when directory not found', (done) => {
14151428
runEmit({
14161429
expectedAssetKeys: [],
1417-
expectedErrors: [
1418-
`[copy-webpack-plugin] unable to locate 'nonexistent' at '${HELPER_DIR}${
1419-
path.sep
1420-
}nonexistent'`,
1430+
expectedWarnings: [
1431+
new Error(
1432+
`[copy-webpack-plugin] unable to locate 'nonexistent' at '${HELPER_DIR}${
1433+
path.sep
1434+
}nonexistent'`
1435+
),
14211436
],
14221437
patterns: [
14231438
{

0 commit comments

Comments
 (0)
Please sign in to comment.