Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Support Config.node false value
Browse files Browse the repository at this point in the history
Resolves #209.
  • Loading branch information
o.drapeza committed Mar 30, 2020
1 parent 529ae0c commit 8b22501
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 13 additions & 3 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -143,7 +145,6 @@ module.exports = class extends ChainedMap {

merge(obj = {}, omit = []) {
const omissions = [
'node',
'output',
'resolve',
'resolveLoader',
Expand All @@ -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']);
}
};
68 changes: 68 additions & 0 deletions test/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ 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();

Expand Down Expand Up @@ -172,6 +185,20 @@ 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();

Expand Down Expand Up @@ -324,6 +351,47 @@ 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();

Expand Down
3 changes: 3 additions & 0 deletions types/test/webpack-chain-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ config
.clear()
.end()

.set('node', false)
.delete('node')

.devServer
.allowedHosts
.add('host.com')
Expand Down

0 comments on commit 8b22501

Please sign in to comment.