From f47fdb8b289459f75d86304288e40b7905ec657d Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Thu, 31 Dec 2020 19:47:17 +0300 Subject: [PATCH] fix: error message on not installed module loaders for configuration --- packages/webpack-cli/lib/webpack-cli.js | 18 +++++++++++++++++- test/config-format/failure/failure.test.js | 19 +++++++++++++++++++ .../failure/webpack.config.coffee | 10 ++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/config-format/failure/failure.test.js create mode 100644 test/config-format/failure/webpack.config.coffee diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index c3b91fc554e..377695c2de0 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -701,7 +701,23 @@ class WebpackCLI { const interpreted = Object.keys(jsVariants).find((variant) => variant === ext); if (interpreted) { - rechoir.prepare(extensions, configPath); + try { + rechoir.prepare(extensions, configPath); + } catch (error) { + if (error.failures) { + logger.error(`Unable load '${configPath}'`); + logger.error(error.message); + + error.failures.forEach((failure) => { + logger.error(failure.error.message); + }); + logger.error('Please install one of them'); + process.exit(2); + } + + logger.error(error); + process.exit(2); + } } const { pathToFileURL } = require('url'); diff --git a/test/config-format/failure/failure.test.js b/test/config-format/failure/failure.test.js new file mode 100644 index 00000000000..125c94e31bf --- /dev/null +++ b/test/config-format/failure/failure.test.js @@ -0,0 +1,19 @@ +const path = require('path'); + +const { run } = require('../../utils/test-utils'); + +describe('webpack cli', () => { + it('should support mjs config format', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.coffee']); + + expect(exitCode).toBe(2); + expect(stderr).toContain(`Unable load '${path.resolve(__dirname, './webpack.config.coffee')}'`); + expect(stderr).toContain('Unable to use specified module loaders for ".coffee".'); + expect(stderr).toContain("Cannot find module 'coffeescript/register'"); + expect(stderr).toContain("Cannot find module 'coffee-script/register'"); + expect(stderr).toContain("Cannot find module 'coffeescript'"); + expect(stderr).toContain("Cannot find module 'coffee-script'"); + expect(stderr).toContain('Please install one of them'); + expect(stdout).toBeFalsy(); + }); +}); diff --git a/test/config-format/failure/webpack.config.coffee b/test/config-format/failure/webpack.config.coffee new file mode 100644 index 00000000000..15e1934891b --- /dev/null +++ b/test/config-format/failure/webpack.config.coffee @@ -0,0 +1,10 @@ +path = require 'path' + +config = + mode: 'production' + entry: './main.js' + output: + path: path.resolve(__dirname, 'dist') + filename: 'foo.bundle.js' + +module.exports = config;