From d4568a02bf76aa65c40309804d843ee2d37e3d05 Mon Sep 17 00:00:00 2001 From: shellscape Date: Sat, 23 Jun 2018 08:54:52 -0400 Subject: [PATCH] fix: context overriden by flags default value. fixes #36 --- lib/config.js | 5 +++-- lib/flags/general.js | 4 ++-- test/tests/__snapshots__/config.js.snap | 11 +++++++++++ test/tests/__snapshots__/flags.js.snap | 3 --- test/tests/__snapshots__/reporters.js.snap | 4 ---- test/tests/config.js | 15 +++++++++++++++ test/tests/flags/__snapshots__/debug.js.snap | 1 - test/tests/flags/__snapshots__/devtool.js.snap | 1 - test/tests/flags/__snapshots__/entry.js.snap | 1 - test/tests/flags/__snapshots__/reporter.js.snap | 3 --- test/tests/flags/__snapshots__/run-mode.js.snap | 2 -- test/tests/flags/__snapshots__/watch.js.snap | 1 - 12 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/config.js b/lib/config.js index 19adad7..5672e1d 100644 --- a/lib/config.js +++ b/lib/config.js @@ -7,12 +7,13 @@ const WebpackCommandError = require('./WebpackCommandError'); module.exports = { distill(argv, config, options) { + const cwdContext = { context: process.cwd() }; let result; if (Array.isArray(config)) { - result = config.map((conf) => merge(conf, options)); + result = config.map((conf) => merge(cwdContext, conf, options)); } else { - result = merge(config, options); + result = merge(cwdContext, config, options); } if (argv.configName) { diff --git a/lib/flags/general.js b/lib/flags/general.js index a8c89e3..d6d9059 100644 --- a/lib/flags/general.js +++ b/lib/flags/general.js @@ -40,9 +40,9 @@ module.exports = { if (argv.context) { result.context = path.resolve(argv.context); - } else { - result.context = process.cwd(); } + // there is no else case for context, as we don't want a default value + // here to override a context value in a config if (argv.debug) { const { LoaderOptionsPlugin } = webpack; diff --git a/test/tests/__snapshots__/config.js.snap b/test/tests/__snapshots__/config.js.snap index 668af80..db1c0dc 100644 --- a/test/tests/__snapshots__/config.js.snap +++ b/test/tests/__snapshots__/config.js.snap @@ -3,10 +3,12 @@ exports[`lib/config > distill() #0 1`] = ` Array [ Object { + "context": "", "entry": "/common/entry-a.js", "mode": "development", }, Object { + "context": "", "entry": "/common/entry-b.js", "mode": "development", }, @@ -26,6 +28,7 @@ Object { exports[`lib/config > distill() plugins #0 1`] = ` Object { + "context": "", "plugins": Array [ 1, ], @@ -34,6 +37,7 @@ Object { exports[`lib/config > distill() plugins #1 1`] = ` Object { + "context": "", "plugins": Array [ 1, ], @@ -42,6 +46,7 @@ Object { exports[`lib/config > distill() plugins #2 1`] = ` Object { + "context": "", "plugins": Array [ 1, 1, @@ -52,6 +57,7 @@ Object { exports[`lib/config > distill() plugins from config array #0 1`] = ` Array [ Object { + "context": "", "entry": "/common/entry-a.js", "mode": "development", "plugins": Array [ @@ -59,6 +65,7 @@ Array [ ], }, Object { + "context": "", "entry": "/common/entry-b.js", "mode": "development", "plugins": Array [ @@ -71,6 +78,7 @@ Array [ exports[`lib/config > distill() plugins from config array #1 1`] = ` Array [ Object { + "context": "", "entry": "/common/entry-a.js", "mode": "development", "plugins": Array [ @@ -78,6 +86,7 @@ Array [ ], }, Object { + "context": "", "entry": "/common/entry-b.js", "mode": "development", "plugins": Array [ @@ -90,6 +99,7 @@ Array [ exports[`lib/config > distill() plugins from config array #2 1`] = ` Array [ Object { + "context": "", "entry": "/common/entry-a.js", "mode": "development", "plugins": Array [ @@ -97,6 +107,7 @@ Array [ ], }, Object { + "context": "", "entry": "/common/entry-b.js", "mode": "development", "plugins": Array [ diff --git a/test/tests/__snapshots__/flags.js.snap b/test/tests/__snapshots__/flags.js.snap index d5a78a9..fe2d14d 100644 --- a/test/tests/__snapshots__/flags.js.snap +++ b/test/tests/__snapshots__/flags.js.snap @@ -155,7 +155,6 @@ exports[`lib/flags > should display help #1 1`] = ` exports[`lib/flags > should parse compound flags: --run-dev #0 1`] = ` Object { - "context": "", "devtool": "eval-cheap-module-source-map", "mode": "development", "output": Object { @@ -180,7 +179,6 @@ Object { exports[`lib/flags > should parse compound flags: --run-prod #0 1`] = ` Object { - "context": "", "mode": "production", "output": Object {}, "plugins": Array [ @@ -207,7 +205,6 @@ Object { exports[`lib/flags > should parse flags cleanly #0 1`] = ` Object { - "context": "", "output": Object {}, } `; diff --git a/test/tests/__snapshots__/reporters.js.snap b/test/tests/__snapshots__/reporters.js.snap index 9216d96..a15f357 100644 --- a/test/tests/__snapshots__/reporters.js.snap +++ b/test/tests/__snapshots__/reporters.js.snap @@ -2,7 +2,6 @@ exports[`StylishReporter > problems/stylish-problems reporter should apply #0 1`] = ` Object { - "context": "", "entry": "/test/fixtures/reporters/stylish/problems/entry-problems.js", "mode": "development", "plugins": Array [ @@ -46,7 +45,6 @@ webpack v4.6.0 exports[`StylishReporter > stylish reporter should apply #0 1`] = ` Object { - "context": "", "entry": "/test/fixtures/common/entry-a.js", "mode": "development", "plugins": Array [ @@ -75,7 +73,6 @@ webpack v4.6.0 exports[`StylishReporter > stylish-multi reporter should apply #0 1`] = ` Array [ Object { - "context": "", "entry": "/test/fixtures/common/entry-a.js", "mode": "development", "plugins": Array [ @@ -85,7 +82,6 @@ Array [ ], }, Object { - "context": "", "entry": Array [ "/test/fixtures/common/entry-b.js", "/test/fixtures/common/entry-c.js", diff --git a/test/tests/config.js b/test/tests/config.js index af54a09..eba6323 100644 --- a/test/tests/config.js +++ b/test/tests/config.js @@ -1,5 +1,6 @@ const { test } = require('../util'); const { distill } = require('../../lib/config'); +const { apply } = require('../../lib/flags'); const config = [ { @@ -18,6 +19,20 @@ test('lib/config', module, () => { expect(result).toMatchSnapshot(); }); + it(`distill() context`, () => { + const argv = {}; + const options = apply({}, {}); + + let result = distill(argv, { context: 'batman' }, { context: 'superman' }); + expect(result.context).toBe('superman'); + + result = distill(argv, { context: 'batman' }, options); + expect(result.context).toBe('batman'); + + result = distill(argv, {}, options); + expect(result.context).toBe(process.cwd()); + }); + it(`distill() plugins`, () => { let result = distill({}, {}, { plugins: [1] }); expect(result).toMatchSnapshot(); diff --git a/test/tests/flags/__snapshots__/debug.js.snap b/test/tests/flags/__snapshots__/debug.js.snap index b3da88a..1ac3204 100644 --- a/test/tests/flags/__snapshots__/debug.js.snap +++ b/test/tests/flags/__snapshots__/debug.js.snap @@ -2,7 +2,6 @@ exports[`Flags > --debug > should apply #0 1`] = ` Object { - "context": "", "entry": "/test/fixtures/common/entry-a.js", "mode": "development", "plugins": Array [ diff --git a/test/tests/flags/__snapshots__/devtool.js.snap b/test/tests/flags/__snapshots__/devtool.js.snap index 35648c9..60605af 100644 --- a/test/tests/flags/__snapshots__/devtool.js.snap +++ b/test/tests/flags/__snapshots__/devtool.js.snap @@ -2,7 +2,6 @@ exports[`Flags > --devtool > should apply #0 1`] = ` Object { - "context": "", "devtool": "cheap-source-map", "entry": "/test/fixtures/common/entry-a.js", "mode": "development", diff --git a/test/tests/flags/__snapshots__/entry.js.snap b/test/tests/flags/__snapshots__/entry.js.snap index 092f237..3cb316c 100644 --- a/test/tests/flags/__snapshots__/entry.js.snap +++ b/test/tests/flags/__snapshots__/entry.js.snap @@ -2,7 +2,6 @@ exports[`Flags > --entry > multi should apply #0 1`] = ` Object { - "context": "", "entry": Object { "main": Array [ "./test/fixtures/common/entry-a.js", diff --git a/test/tests/flags/__snapshots__/reporter.js.snap b/test/tests/flags/__snapshots__/reporter.js.snap index d3978b7..afaa51a 100644 --- a/test/tests/flags/__snapshots__/reporter.js.snap +++ b/test/tests/flags/__snapshots__/reporter.js.snap @@ -2,7 +2,6 @@ exports[`Flags > --reporter > basic reporter should apply #0 1`] = ` Object { - "context": "", "entry": "/test/fixtures/common/entry-a.js", "mode": "development", "plugins": Array [ @@ -32,7 +31,6 @@ exports[`Flags > --reporter > basic reporter should build #1 1`] = `"bf58edd8"`; exports[`Flags > --reporter > basic-multi reporter should apply #0 1`] = ` Array [ Object { - "context": "", "entry": "/test/fixtures/common/entry-a.js", "mode": "development", "plugins": Array [ @@ -43,7 +41,6 @@ Array [ "reporter": "basic", }, Object { - "context": "", "entry": Array [ "/test/fixtures/common/entry-b.js", "/test/fixtures/common/entry-c.js", diff --git a/test/tests/flags/__snapshots__/run-mode.js.snap b/test/tests/flags/__snapshots__/run-mode.js.snap index 120854a..7e1e1c8 100644 --- a/test/tests/flags/__snapshots__/run-mode.js.snap +++ b/test/tests/flags/__snapshots__/run-mode.js.snap @@ -2,7 +2,6 @@ exports[`Flags > --run-dev > should apply #0 1`] = ` Object { - "context": "", "devtool": "eval-cheap-module-source-map", "entry": "/test/fixtures/common/entry-a.js", "mode": "development", @@ -35,7 +34,6 @@ exports[`Flags > --run-dev > should build #1 1`] = `"1f4895c0"`; exports[`Flags > --run-prod > should apply #0 1`] = ` Object { - "context": "", "entry": "/test/fixtures/common/entry-a.js", "mode": "development", "plugins": Array [ diff --git a/test/tests/flags/__snapshots__/watch.js.snap b/test/tests/flags/__snapshots__/watch.js.snap index 67c9946..a3cf581 100644 --- a/test/tests/flags/__snapshots__/watch.js.snap +++ b/test/tests/flags/__snapshots__/watch.js.snap @@ -2,7 +2,6 @@ exports[`Flags > --watch > should apply #0 1`] = ` Object { - "context": "", "entry": "/test/fixtures/common/entry-a.js", "mode": "development", "name": "single-flag",