Skip to content
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

fix: throw err when supplied config is absent #1760

Merged
merged 4 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 3 additions & 7 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const webpackMerge = require('webpack-merge');
const { extensions, jsVariants } = require('interpret');
const GroupHelper = require('../utils/GroupHelper');
const rechoir = require('rechoir');
const MergeError = require('../utils/errors/MergeError');
const ConfigError = require('../utils/errors/ConfigError');
const logger = require('../utils/logger');

// Order defines the priority, in increasing order
Expand Down Expand Up @@ -157,11 +157,7 @@ class ConfigGroup extends GroupHelper {
const configPath = resolve(process.cwd(), config);
const configFiles = getConfigInfoFromFileName(configPath);
if (!configFiles.length) {
this.opts.processingMessageBuffer.push({
lvl: 'warn',
msg: `Configuration ${config} not found in ${configPath}`,
});
return;
throw new ConfigError(`The specified config file doesn't exist in ${configPath}`);
}
const foundConfig = configFiles[0];
const resolvedConfig = this.requireConfig(foundConfig);
Expand Down Expand Up @@ -196,7 +192,7 @@ class ConfigGroup extends GroupHelper {
const newConfigPath = this.resolveFilePath(merge);

if (!newConfigPath) {
throw new MergeError("The supplied merge config doesn't exist.");
throw new ConfigError("The supplied merge config doesn't exist.", 'MergeError');
}

const configFiles = getConfigInfoFromFileName(newConfigPath);
Expand Down
11 changes: 11 additions & 0 deletions packages/webpack-cli/lib/utils/errors/ConfigError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ConfigError extends Error {
constructor(message, name) {
super(message);
this.name = name || 'ConfigError';
// No need to show stack trace for known errors
this.stack = '';
process.exitCode = 2;
}
}

module.exports = ConfigError;
10 changes: 0 additions & 10 deletions packages/webpack-cli/lib/utils/errors/MergeError.js

This file was deleted.

1 change: 1 addition & 0 deletions test/config/absent/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Zenitsu');
17 changes: 17 additions & 0 deletions test/config/absent/config-absent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const { existsSync } = require('fs');
const { resolve } = require('path');
const { run } = require('../../utils/test-utils');

describe('Config:', () => {
it('supplied config file is absent', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.js')], false);
// should throw with correct exit code
expect(exitCode).toBe(2);
expect(stdout).toBeFalsy();
// Should contain the correct error message
expect(stderr).toContain("ConfigError: The specified config file doesn't exist in");
anshumanv marked this conversation as resolved.
Show resolved Hide resolved
// Should not bundle
expect(existsSync(resolve(__dirname, './binary/a.bundle.js'))).toBeFalsy();
});
});
9 changes: 9 additions & 0 deletions test/config/absent/webpack.config-absent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './a.js',
output: {
path: resolve(__dirname, 'binary'),
filename: 'a.bundle.js',
},
};