From 9d354f198b2b91d665da0cec63d471575f491f4b Mon Sep 17 00:00:00 2001 From: Spencer Elliott Date: Sat, 2 Jun 2018 12:50:39 -0700 Subject: [PATCH 1/5] test: pass --reporter values relative to CWD This test ensures that a --reporter value can be passed with a path relative to the CWD. The test should fail with this error: 1) Commands should accept custom reporters relative to the current working directory: Command failed: /Users/spencerelliott/Dev/elliottsj/webpack-command/lib/cli.js --reporter ../../lib/reporters/BasicReporter /Users/spencerelliott/Dev/elliottsj/webpack-command/test/fixtures/flags/config/src TypeError: ReporterClass is not a constructor at module.exports (/Users/spencerelliott/Dev/elliottsj/webpack-command/lib/compiler.js:91:20) at load.then (/Users/spencerelliott/Dev/elliottsj/webpack-command/lib/index.js:45:47) at process._tickCallback (internal/process/next_tick.js:68:7) at Function.Module.runMain (internal/modules/cjs/loader.js:746:11) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3) --- test/tests/__snapshots__/cli.js.snap | 17 +++++++++++++++++ test/tests/cli.js | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/test/tests/__snapshots__/cli.js.snap b/test/tests/__snapshots__/cli.js.snap index 7c71f41..6b25897 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); }); From a2167b3e852fe55005b384b5051be3460cd977bb Mon Sep 17 00:00:00 2001 From: Spencer Elliott Date: Sat, 2 Jun 2018 13:19:05 -0700 Subject: [PATCH 2/5] feat: allow --reporter to be relative to CWD Previously, the --reporter path must have been either absolute or in node_modules or relative to lib/compiler.js. This change ensures that a path passed to --reporter is resolved relative to process.cwd(). e.g. you can now run this inside of a project directory to use a custom reporter: webpack --reporter ./MyCustomReporter.js --- lib/compiler.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index d89b88e..fef2d3c 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 merge = require('merge-options'); @@ -34,23 +36,31 @@ 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) { - const prefix = capitalize(camelcase(name)); - const target = local ? `./reporters/${prefix}Reporter` : name; - +function requireReporter(name) { try { + const prefix = capitalize(camelcase(name)); // eslint-disable-next-line import/no-dynamic-require, global-require - const result = require(target); + const result = require(`./reporters/${prefix}Reporter`); return result; - } catch (e) { - if (local) { - return requireReporter(name, false); + } catch (e1) { + try { + // eslint-disable-next-line import/no-dynamic-require, global-require + const result = require(name); + return result; + } catch (e2) { + try { + // eslint-disable-next-line import/no-dynamic-require, global-require + const result = require(path.resolve(name)); + return result; + } catch (e3) { + // empty + } } - - return null; } + return null; } /** From 41e2bbb58ad1256185bb480842128031ec825f5e Mon Sep 17 00:00:00 2001 From: Andrew Powell Date: Mon, 25 Jun 2018 09:27:21 -0400 Subject: [PATCH 3/5] refactor: loop rather than nest. makes for a little better readability --- lib/compiler.js | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index fef2d3c..49709b8 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -40,27 +40,20 @@ function makeCallback(options) { * to the current working directory. */ function requireReporter(name) { - try { - const prefix = capitalize(camelcase(name)); - // eslint-disable-next-line import/no-dynamic-require, global-require - const result = require(`./reporters/${prefix}Reporter`); - return result; - } catch (e1) { + const prefix = capitalize(camelcase(name)); + 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 - const result = require(name); - return result; - } catch (e2) { - try { - // eslint-disable-next-line import/no-dynamic-require, global-require - const result = require(path.resolve(name)); - return result; - } catch (e3) { - // empty - } + result = require(location); + } catch (e) { + // noop } } - return null; + + return result; } /** From e565bc76de8e13008d68a0e98b4ba0e642c75900 Mon Sep 17 00:00:00 2001 From: Andrew Powell Date: Mon, 25 Jun 2018 09:29:22 -0400 Subject: [PATCH 4/5] fix: exit the loop if reporter was found --- lib/compiler.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/compiler.js b/lib/compiler.js index 49709b8..3fd7be9 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -51,6 +51,10 @@ function requireReporter(name) { } catch (e) { // noop } + + if (result) { + break; + } } return result; From 1789cc67ec7b6e3a853f1a45b56211397f863b9a Mon Sep 17 00:00:00 2001 From: Andrew Powell Date: Mon, 25 Jun 2018 09:32:29 -0400 Subject: [PATCH 5/5] fix: remove whitespace leftover from github editor --- lib/compiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compiler.js b/lib/compiler.js index 3fd7be9..a66a5d4 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -51,7 +51,7 @@ function requireReporter(name) { } catch (e) { // noop } - + if (result) { break; }