From a45b5129c7ec9fce3ed3645513d9b76f2c980a64 Mon Sep 17 00:00:00 2001 From: "o.drapeza" Date: Mon, 30 Mar 2020 18:01:23 +0300 Subject: [PATCH] Support Config.node false value Resolves #209. --- README.md | 7 ++++ src/Config.js | 16 ++++++-- test/Config.js | 65 +++++++++++++++++++++++++++++++ types/test/webpack-chain-tests.ts | 3 ++ 4 files changed, 88 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 795c9a2..58e611b 100644 --- a/README.md +++ b/README.md @@ -842,6 +842,13 @@ config.resolve #### Config node +You can set `node` option to object, or a `false` value. +`false` value always rewrite `config.node` object. + +```js +config.set('node', false); +``` + ```js config.node : ChainedMap diff --git a/src/Config.js b/src/Config.js index 4452e04..49fedb2 100644 --- a/src/Config.js +++ b/src/Config.js @@ -116,10 +116,12 @@ module.exports = class extends ChainedMap { toConfig() { const entryPoints = this.entryPoints.entries() || {}; + const nodeValue = this.get('node'); + const hasNodeValue = typeof nodeValue !== 'undefined'; return this.clean( Object.assign(this.entries() || {}, { - node: this.node.entries(), + node: hasNodeValue ? nodeValue : this.node.entries(), output: this.output.entries(), resolve: this.resolve.toConfig(), resolveLoader: this.resolveLoader.toConfig(), @@ -143,7 +145,6 @@ module.exports = class extends ChainedMap { merge(obj = {}, omit = []) { const omissions = [ - 'node', 'output', 'resolve', 'resolveLoader', @@ -165,12 +166,21 @@ module.exports = class extends ChainedMap { ); } + if (!omit.includes('node') && 'node' in obj) { + if (typeof obj.node === 'object') { + this.delete('node'); + this.node.merge(obj.node); + } else { + this.set('node', obj.node); + } + } + omissions.forEach(key => { if (!omit.includes(key) && key in obj) { this[key].merge(obj[key]); } }); - return super.merge(obj, [...omit, ...omissions, 'entry', 'plugin']); + return super.merge(obj, [...omit, ...omissions, 'entry', 'plugin', 'node']); } }; diff --git a/test/Config.js b/test/Config.js index 1b9be06..589214f 100644 --- a/test/Config.js +++ b/test/Config.js @@ -46,6 +46,18 @@ test('node', t => { t.deepEqual(config.node.entries(), { __dirname: 'mock', __filename: 'mock' }); }); +test('node with false value', t => { + const config = new Config(); + const instance = config + .set('node', false) + .node.set('__dirname', 'mock') + .set('__filename', 'mock') + .end(); + + t.is(instance, config); + t.false(config.get('node')); +}); + test('entry', t => { const config = new Config(); @@ -172,6 +184,19 @@ test('toConfig with values', t => { }); }); +test('toConfig with node false value', t => { + const config = new Config(); + + config + .set('node', false) + .node.set('__dirname', 'mock') + .end(); + + t.deepEqual(config.toConfig(), { + node: false, + }); +}); + test('merge empty', t => { const config = new Config(); @@ -324,6 +349,46 @@ test('merge with omit', t => { }); }); +test('merge with node false value', t => { + const config = new Config(); + + config.node.set('__dirname', 'mock'); + + const obj = { + node: false, + }; + + const instance = config.merge(obj); + + t.is(instance, config); + + t.deepEqual(config.toConfig(), { + node: false, + }); +}); + +test('merge with node object value', t => { + const config = new Config(); + + config.set('node', false); + + const obj = { + node: { + __dirname: 'mock', + }, + }; + + const instance = config.merge(obj); + + t.is(instance, config); + + t.deepEqual(config.toConfig(), { + node: { + __dirname: 'mock', + }, + }); +}); + test('validate empty', t => { const config = new Config(); diff --git a/types/test/webpack-chain-tests.ts b/types/test/webpack-chain-tests.ts index 0a90acc..0b45704 100644 --- a/types/test/webpack-chain-tests.ts +++ b/types/test/webpack-chain-tests.ts @@ -217,6 +217,9 @@ config .clear() .end() + .set('node', false) + .delete('node') + .devServer .allowedHosts .add('host.com')