diff --git a/lib/config-generator.js b/lib/config-generator.js index 71567f5d..09400d9e 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -148,11 +148,30 @@ class ConfigGenerator { entry[sharedEntryTmpName] = tmpFilename; } - if (this.webpackConfig.copyFilesConfigs.length > 0) { + const copyFilesConfigs = this.webpackConfig.copyFilesConfigs.filter(entry => { + const copyFrom = path.resolve( + this.webpackConfig.getContext(), + entry.from + ); + + if (!fs.existsSync(copyFrom)) { + logger.warning(`The "from" option of copyFiles() should be set to an existing directory but "${entry.from}" does not seem to exist. Nothing will be copied for this copyFiles() config object.`); + return false; + } + + if (!fs.lstatSync(copyFrom).isDirectory()) { + logger.warning(`The "from" option of copyFiles() should be set to an existing directory but "${entry.from}" seems to be a file. Nothing will be copied for this copyFiles() config object.`); + return false; + } + + return true; + }); + + if (copyFilesConfigs.length > 0) { const tmpFileObject = tmp.fileSync(); fs.writeFileSync( tmpFileObject.name, - this.webpackConfig.copyFilesConfigs.reduce((buffer, entry, index) => { + copyFilesConfigs.reduce((buffer, entry, index) => { const copyFrom = path.resolve( this.webpackConfig.getContext(), entry.from diff --git a/test/functional.js b/test/functional.js index 8cc8e9a5..bd3ad9a8 100644 --- a/test/functional.js +++ b/test/functional.js @@ -1660,6 +1660,43 @@ module.exports = { done(); }); }); + + it('Do not try to copy files from an invalid path', (done) => { + const config = createWebpackConfig('www/build', 'production'); + config.addEntry('main', './js/no_require'); + config.setPublicPath('/build'); + config.copyFiles([{ + from: './images', + to: 'assets/[path][name].[ext]', + includeSubdirectories: false + }, { + from: './foo', + to: 'assets/[path][name].[ext]', + includeSubdirectories: false + }, { + from: './fonts', + to: 'assets/[path][name].[ext]', + includeSubdirectories: false + }, { + from: './images/symfony_logo.png', + includeSubdirectories: true + }]); + + testSetup.runWebpack(config, (webpackAssert, stats, stdout) => { + expect(config.outputPath).to.be.a.directory() + .with.files([ + 'entrypoints.json', + 'runtime.js', + 'main.js', + 'manifest.json' + ]); + + expect(stdout).to.contain('should be set to an existing directory but "./foo" does not seem to exist'); + expect(stdout).to.contain('should be set to an existing directory but "./images/symfony_logo.png" seems to be a file'); + + done(); + }); + }); }); describe('entrypoints.json & splitChunks()', () => {