diff --git a/lib/compiler.js b/lib/compiler.js index b2223d7..72e3815 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -1,3 +1,5 @@ +const path = require('path'); + const camelcase = require('camelcase'); const capitalize = require('titleize'); const chalk = require('chalk'); @@ -36,23 +38,28 @@ function makeCallback(options) { /** * Attempts to require a specified reporter. Tries the local built-ins first, * and if that fails, attempts to load an npm module that exports a reporter - * handler function. + * handler function, and if that fails, attempts to load the reporter relative + * to the current working directory. */ -function requireReporter(name, local = true) { +function requireReporter(name) { const prefix = capitalize(camelcase(name)); - const target = local ? `./reporters/${prefix}Reporter` : name; - - try { - // eslint-disable-next-line import/no-dynamic-require, global-require - const result = require(target); - return result; - } catch (e) { - if (local) { - return requireReporter(name, false); + const locations = [`./reporters/${prefix}Reporter`, name, path.resolve(name)]; + let result = null; + + for (const location of locations) { + try { + // eslint-disable-next-line import/no-dynamic-require, global-require + result = require(location); + } catch (e) { + // noop } - return null; + if (result) { + break; + } } + + return result; } /** diff --git a/test/tests/__snapshots__/cli.js.snap b/test/tests/__snapshots__/cli.js.snap index 2b24236..8334bf8 100644 --- a/test/tests/__snapshots__/cli.js.snap +++ b/test/tests/__snapshots__/cli.js.snap @@ -1,5 +1,22 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Commands > should accept custom reporters relative to the current working directory #0 1`] = ` +"ℹ 「webpack」: Starting Build +ℹ 「webpack」: Build Finished +Hash: 953ce188d35af8c002f0 + + + + Asset Size Chunks Chunk Names +main.js 573 bytes 0 [emitted] main +Entrypoint main = main.js +[0] ../fixtures/flags/config/src/index.js 43 bytes {0} [built] + +WARNING in configuration +The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. +You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/" +`; + exports[`Commands > should add directories to entries #0 1`] = ` "ℹ 「webpack」: Starting Build ℹ 「webpack」: Build Finished diff --git a/test/tests/cli.js b/test/tests/cli.js index 7c26869..837fff1 100644 --- a/test/tests/cli.js +++ b/test/tests/cli.js @@ -79,4 +79,23 @@ test('Commands', module, () => { strip(result.stdout).replace(/Δt \d+ms/g, '') ).toMatchSnapshot(); }).timeout(4000); + + it('should accept custom reporters relative to the current working directory', () => { + const cliPath = resolve(__dirname, '../../lib/cli.js'); + const srcPath = resolve(__dirname, '../../test/fixtures/flags/config/src'); + const result = execa.sync( + cliPath, + ['--reporter', '../../lib/reporters/BasicReporter', srcPath], + { + cwd: __dirname, + } + ); + + expect( + strip(result.stdout) + .replace(/Version: webpack \d\.\d\.\d/, '') + .replace(/Time: \d+ms/g, '') + .replace(/Built at: .+(?=\n)/g, '') + ).toMatchSnapshot(); + }).timeout(4000); });