Skip to content

Commit 8cb9d4a

Browse files
committed
Display a warning when calling "copyFiles" with an invalid path
1 parent 2a73fbc commit 8cb9d4a

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

lib/config-generator.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,25 @@ class ConfigGenerator {
148148
entry[sharedEntryTmpName] = tmpFilename;
149149
}
150150

151-
if (this.webpackConfig.copyFilesConfigs.length > 0) {
151+
const copyFilesConfigs = this.webpackConfig.copyFilesConfigs.filter(entry => {
152+
const copyFrom = path.resolve(
153+
this.webpackConfig.getContext(),
154+
entry.from
155+
);
156+
157+
if (!fs.existsSync(copyFrom) || !fs.lstatSync(copyFrom).isDirectory()) {
158+
logger.warning(`The "from" option of copyFiles() should be set to an existing directory but "${entry.from}" does not seem to be one. No file will be copied for this entry.`);
159+
return false;
160+
}
161+
162+
return true;
163+
});
164+
165+
if (copyFilesConfigs.length > 0) {
152166
const tmpFileObject = tmp.fileSync();
153167
fs.writeFileSync(
154168
tmpFileObject.name,
155-
this.webpackConfig.copyFilesConfigs.reduce((buffer, entry, index) => {
169+
copyFilesConfigs.reduce((buffer, entry, index) => {
156170
const copyFrom = path.resolve(
157171
this.webpackConfig.getContext(),
158172
entry.from

test/functional.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,43 @@ module.exports = {
16031603
done();
16041604
});
16051605
});
1606+
1607+
it('Do not try to copy files from an invalid path', (done) => {
1608+
const config = createWebpackConfig('www/build', 'production');
1609+
config.addEntry('main', './js/no_require');
1610+
config.setPublicPath('/build');
1611+
config.copyFiles([{
1612+
from: './images',
1613+
to: 'assets/[path][name].[ext]',
1614+
includeSubdirectories: false
1615+
}, {
1616+
from: './foo',
1617+
to: 'assets/[path][name].[ext]',
1618+
includeSubdirectories: false
1619+
}, {
1620+
from: './fonts',
1621+
to: 'assets/[path][name].[ext]',
1622+
includeSubdirectories: false
1623+
}, {
1624+
from: './bar/baz',
1625+
includeSubdirectories: true
1626+
}]);
1627+
1628+
testSetup.runWebpack(config, (webpackAssert, stats, stdout) => {
1629+
expect(config.outputPath).to.be.a.directory()
1630+
.with.files([
1631+
'entrypoints.json',
1632+
'runtime.js',
1633+
'main.js',
1634+
'manifest.json'
1635+
]);
1636+
1637+
expect(stdout).to.contain('should be set to an existing directory but "./foo" does not seem to be one');
1638+
expect(stdout).to.contain('should be set to an existing directory but "./bar/baz" does not seem to be one');
1639+
1640+
done();
1641+
});
1642+
});
16061643
});
16071644

16081645
describe('entrypoints.json', () => {

0 commit comments

Comments
 (0)