Skip to content

Commit b5750b2

Browse files
committed
bug #456 Display a warning when calling "copyFiles" with an invalid path (Lyrkan)
This PR was squashed before being merged into the master branch (closes #456). Discussion ---------- Display a warning when calling "copyFiles" with an invalid path This PR adds a warning when `copyFiles()` is called with a `from` value that isn't an existing directory. This is kind of related to #445 but even if the friendly-errors-webpack-plugin worked properly in this case the origin of the error would probably not be obvious (it'd most likely contain a reference to the temporary entry). Commits ------- 5161d30 Display a different warning when trying to copy from an unexisting directory or an invalid one 8cb9d4a Display a warning when calling "copyFiles" with an invalid path
2 parents 366f32c + 5161d30 commit b5750b2

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

lib/config-generator.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,30 @@ 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)) {
158+
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.`);
159+
return false;
160+
}
161+
162+
if (!fs.lstatSync(copyFrom).isDirectory()) {
163+
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.`);
164+
return false;
165+
}
166+
167+
return true;
168+
});
169+
170+
if (copyFilesConfigs.length > 0) {
152171
const tmpFileObject = tmp.fileSync();
153172
fs.writeFileSync(
154173
tmpFileObject.name,
155-
this.webpackConfig.copyFilesConfigs.reduce((buffer, entry, index) => {
174+
copyFilesConfigs.reduce((buffer, entry, index) => {
156175
const copyFrom = path.resolve(
157176
this.webpackConfig.getContext(),
158177
entry.from

test/functional.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,43 @@ module.exports = {
16601660
done();
16611661
});
16621662
});
1663+
1664+
it('Do not try to copy files from an invalid path', (done) => {
1665+
const config = createWebpackConfig('www/build', 'production');
1666+
config.addEntry('main', './js/no_require');
1667+
config.setPublicPath('/build');
1668+
config.copyFiles([{
1669+
from: './images',
1670+
to: 'assets/[path][name].[ext]',
1671+
includeSubdirectories: false
1672+
}, {
1673+
from: './foo',
1674+
to: 'assets/[path][name].[ext]',
1675+
includeSubdirectories: false
1676+
}, {
1677+
from: './fonts',
1678+
to: 'assets/[path][name].[ext]',
1679+
includeSubdirectories: false
1680+
}, {
1681+
from: './images/symfony_logo.png',
1682+
includeSubdirectories: true
1683+
}]);
1684+
1685+
testSetup.runWebpack(config, (webpackAssert, stats, stdout) => {
1686+
expect(config.outputPath).to.be.a.directory()
1687+
.with.files([
1688+
'entrypoints.json',
1689+
'runtime.js',
1690+
'main.js',
1691+
'manifest.json'
1692+
]);
1693+
1694+
expect(stdout).to.contain('should be set to an existing directory but "./foo" does not seem to exist');
1695+
expect(stdout).to.contain('should be set to an existing directory but "./images/symfony_logo.png" seems to be a file');
1696+
1697+
done();
1698+
});
1699+
});
16631700
});
16641701

16651702
describe('entrypoints.json & splitChunks()', () => {

0 commit comments

Comments
 (0)