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 2 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
11 changes: 3 additions & 8 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 @@ -98,7 +98,6 @@ class ConfigGroup extends GroupHelper {
const newOptionsObject = {
outputOptions: {},
options: {},
processingMessageBuffer: [],
};

if (!moduleObj) {
Expand Down Expand Up @@ -157,11 +156,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 +191,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
20 changes: 10 additions & 10 deletions packages/webpack-cli/lib/utils/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Compiler {
}
}

compilerCallback(err, stats, lastHash, options, outputOptions, processingMessageBuffer) {
compilerCallback(err, stats, lastHash, options, outputOptions) {
const statsErrors = [];

if (!outputOptions.watch || err) {
Expand All @@ -91,26 +91,26 @@ class Compiler {
statsErrors.push({ name: statErr.message, loc: errLoc });
});
}
return this.generateOutput(outputOptions, stats, statsErrors, processingMessageBuffer);
return this.generateOutput(outputOptions, stats, statsErrors);
}
if (!outputOptions.watch && stats.hasErrors()) {
process.exitCode = 2;
}
}

async invokeCompilerInstance(lastHash, options, outputOptions, processingMessageBuffer) {
async invokeCompilerInstance(lastHash, options, outputOptions) {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
await this.compiler.run((err, stats) => {
const content = this.compilerCallback(err, stats, lastHash, options, outputOptions, processingMessageBuffer);
const content = this.compilerCallback(err, stats, lastHash, options, outputOptions);
resolve(content);
});
});
}

async invokeWatchInstance(lastHash, options, outputOptions, watchOptions, processingMessageBuffer) {
async invokeWatchInstance(lastHash, options, outputOptions, watchOptions) {
return this.compiler.watch(watchOptions, (err, stats) => {
return this.compilerCallback(err, stats, lastHash, options, outputOptions, processingMessageBuffer);
return this.compilerCallback(err, stats, lastHash, options, outputOptions);
});
}

Expand All @@ -131,7 +131,7 @@ class Compiler {
}

async webpackInstance(opts) {
const { outputOptions, processingMessageBuffer, options } = opts;
const { outputOptions, options } = opts;
const lastHash = null;

const { ProgressPlugin } = webpack;
Expand All @@ -141,7 +141,7 @@ class Compiler {

if (outputOptions.interactive) {
const interactive = require('./interactive');
return interactive(options, outputOptions, processingMessageBuffer);
return interactive(options, outputOptions);
}

if (this.compiler.compilers) {
Expand All @@ -160,9 +160,9 @@ class Compiler {
});
process.stdin.resume();
}
await this.invokeWatchInstance(lastHash, options, outputOptions, watchOptions, processingMessageBuffer);
await this.invokeWatchInstance(lastHash, options, outputOptions, watchOptions);
} else {
return await this.invokeCompilerInstance(lastHash, options, outputOptions, processingMessageBuffer);
return await this.invokeCompilerInstance(lastHash, options, outputOptions);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/webpack-cli/lib/utils/GroupHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class GroupHelper {
this.opts = {
outputOptions: {},
options: {},
processingMessageBuffer: [],
};
this.strategy = undefined;
}
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.

6 changes: 3 additions & 3 deletions packages/webpack-cli/lib/utils/interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ const ENTER_KEY = '\n';
const B_KEY = 'b';
const C_KEY = 'c';

module.exports = async function (config, outputOptions, processingMessageBuffer) {
module.exports = async function (config, outputOptions) {
const stdin = process.stdin;
stdin.setEncoding('utf-8');
stdin.setRawMode(true);
readline.emitKeypressEvents(stdin);

outputOptions.interactive = false;

const webpackCompilation = await webpack({ options: config, outputOptions, processingMessageBuffer });
const webpackCompilation = await webpack({ options: config, outputOptions });
/* if(errors) {
Hngggg
} */
Expand Down Expand Up @@ -164,7 +164,7 @@ module.exports = async function (config, outputOptions, processingMessageBuffer)
if (state.length) {
state.pop();
}
const webpackCompilation = await webpack({ options: config, outputOptions, processingMessageBuffer });
const webpackCompilation = await webpack({ options: config, outputOptions });
state.push(webpackCompilation);
informActions();
isSub = true;
Expand Down
16 changes: 0 additions & 16 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class WebpackCLI extends GroupHelper {
this.groupMap = new Map();
this.groups = [];
this.args = {};
this.processingMessageBuffer = [];
this.compilation = new Compiler();
this.defaultEntry = 'index';
this.possibleFileNames = [
Expand Down Expand Up @@ -186,19 +185,6 @@ class WebpackCLI extends GroupHelper {
}
}

/**
* Responsible for updating the buffer
*
* @param {string[]} messageBuffer The current buffer message
* @private
* @returns {void}
*/
_mergeProcessingMessageBuffer(messageBuffer) {
if (messageBuffer) {
this.processingMessageBuffer = this.processingMessageBuffer.concat(...messageBuffer);
}
}

/**
* It receives a group helper, it runs and it merges its result inside
* the file result that will be passed to the compiler
Expand All @@ -212,7 +198,6 @@ class WebpackCLI extends GroupHelper {
const result = await groupHelper.run();
this._mergeOptionsToConfiguration(result.options, groupHelper.strategy);
this._mergeOptionsToOutputConfiguration(result.outputOptions);
this._mergeProcessingMessageBuffer(result.processingMessageBuffer);
}
}

Expand Down Expand Up @@ -270,7 +255,6 @@ class WebpackCLI extends GroupHelper {
const webpack = await this.compilation.webpackInstance({
options: this.compilerConfiguration,
outputOptions: this.outputConfiguration,
processingMessageBuffer: this.processingMessageBuffer,
});
return webpack;
}
Expand Down
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',
},
};