Skip to content

Commit

Permalink
bug #456 Display a warning when calling "copyFiles" with an invalid p…
Browse files Browse the repository at this point in the history
…ath (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
  • Loading branch information
weaverryan committed Dec 13, 2018
2 parents 366f32c + 5161d30 commit b5750b2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down

0 comments on commit b5750b2

Please sign in to comment.