Skip to content

Display a warning when calling "copyFiles" with an invalid path #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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: './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', () => {
Expand Down