Skip to content

Commit 78c5d12

Browse files
alan-agius4evilebottnawi
authored andcommitted
fix: handle undefined and null as stats value (#302)
In certain cases depending on the inputFileSystem `stats` doesn't throw, but returns a falsey value such as `undefined` and `null`. In this case an error from globby will be thrown `Patterns must be a string or an array of strings`
1 parent 7fe0c06 commit 78c5d12

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/preProcessPattern.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,29 @@ export default function preProcessPattern(globalRef, pattern) {
5959

6060
debug(`determined '${pattern.from}' to be read from '${pattern.absoluteFrom}'`);
6161

62-
return stat(inputFileSystem, pattern.absoluteFrom)
63-
.catch(() => {
62+
const noStatsHandler = () => {
6463
// If from doesn't appear to be a glob, then log a warning
6564
if (isGlob(pattern.from) || pattern.from.indexOf('*') !== -1) {
6665
pattern.fromType = 'glob';
6766
pattern.glob = escape(pattern.context, pattern.from);
6867
} else {
6968
const msg = `unable to locate '${pattern.from}' at '${pattern.absoluteFrom}'`;
70-
warning(msg);
71-
compilation.errors.push(`[copy-webpack-plugin] ${msg}`);
69+
const warningMsg = `[copy-webpack-plugin] ${msg}`;
70+
// only display the same message once
71+
if (compilation.errors.indexOf(warningMsg) === -1) {
72+
warning(msg);
73+
compilation.errors.push(warningMsg);
74+
}
75+
7276
pattern.fromType = 'nonexistent';
7377
}
74-
})
78+
};
79+
80+
return stat(inputFileSystem, pattern.absoluteFrom)
81+
.catch(() => noStatsHandler())
7582
.then((stat) => {
7683
if (!stat) {
84+
noStatsHandler();
7785
return pattern;
7886
}
7987

tests/index.js

+26
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ class MockCompiler {
5656
}
5757
}
5858

59+
60+
class MockCompilerNoStat extends MockCompiler {
61+
constructor (options = {}) {
62+
super(options);
63+
64+
this.inputFileSystem.stat = (file, cb) => cb(undefined, undefined);
65+
}
66+
}
67+
5968
describe('apply function', () => {
6069
// Ideally we pass in patterns and confirm the resulting assets
6170
const run = (opts) => {
@@ -621,6 +630,23 @@ describe('apply function', () => {
621630
.catch(done);
622631
});
623632

633+
it('warns when file not found and stats is undefined', (done) => {
634+
runEmit({
635+
compiler: new MockCompilerNoStat(),
636+
expectedAssetKeys: [],
637+
expectedErrors: [
638+
`[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${path.sep}nonexistent.txt'`
639+
],
640+
patterns: [{
641+
from: 'nonexistent.txt',
642+
to: '.',
643+
toType: 'dir'
644+
}]
645+
})
646+
.then(done)
647+
.catch(done);
648+
});
649+
624650
it('warns when tranform failed', (done) => {
625651
runEmit({
626652
expectedAssetKeys: [],

0 commit comments

Comments
 (0)