From 8cb9d4a924d39d94207125bea71641a21a75b600 Mon Sep 17 00:00:00 2001 From: Lyrkan Date: Fri, 16 Nov 2018 19:49:31 +0100 Subject: [PATCH 1/2] Display a warning when calling "copyFiles" with an invalid path --- lib/config-generator.js | 18 ++++++++++++++++-- test/functional.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index 00e6933d..52e99ad2 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -148,11 +148,25 @@ 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) || !fs.lstatSync(copyFrom).isDirectory()) { + 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.`); + 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 4e70f258..46e24e58 100644 --- a/test/functional.js +++ b/test/functional.js @@ -1603,6 +1603,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: './bar/baz', + 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 be one'); + expect(stdout).to.contain('should be set to an existing directory but "./bar/baz" does not seem to be one'); + + done(); + }); + }); }); describe('entrypoints.json', () => { From 5161d3010f78960c2feab18256b2a62dfd6468b3 Mon Sep 17 00:00:00 2001 From: Lyrkan Date: Fri, 16 Nov 2018 20:21:03 +0100 Subject: [PATCH 2/2] Display a different warning when trying to copy from an unexisting directory or an invalid one --- lib/config-generator.js | 9 +++++++-- test/functional.js | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index 52e99ad2..dc3cd269 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -154,8 +154,13 @@ class ConfigGenerator { entry.from ); - if (!fs.existsSync(copyFrom) || !fs.lstatSync(copyFrom).isDirectory()) { - 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.`); + 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; } diff --git a/test/functional.js b/test/functional.js index 46e24e58..7160f49e 100644 --- a/test/functional.js +++ b/test/functional.js @@ -1621,7 +1621,7 @@ module.exports = { to: 'assets/[path][name].[ext]', includeSubdirectories: false }, { - from: './bar/baz', + from: './images/symfony_logo.png', includeSubdirectories: true }]); @@ -1634,8 +1634,8 @@ module.exports = { 'manifest.json' ]); - expect(stdout).to.contain('should be set to an existing directory but "./foo" does not seem to be one'); - expect(stdout).to.contain('should be set to an existing directory but "./bar/baz" does not seem to be one'); + 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(); });