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

Support Config.node false value #252

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Comment on lines +845 to +846
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can set `node` option to object, or a `false` value.
`false` value always rewrite `config.node` object.
You can set the `node` option to an object, or a `false` value.
A `false` value always overrides the `config.node` chained map.


```js
config.set('node', false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
config.set('node', false);
config.set('node', false);
// To use the value generated from `config.node` ChainedMap again
config.delete('node');

```

```js
config.node : ChainedMap

16 changes: 13 additions & 3 deletions src/Config.js
Original file line number Diff line number Diff line change
@@ -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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true for obj.node == []. Though the input is unexpected it might cause issues.

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']);
}
};
62 changes: 62 additions & 0 deletions test/Config.js
Original file line number Diff line number Diff line change
@@ -48,6 +48,18 @@ test('node', () => {
});
});

test('node with false value', () => {
const config = new Config();
const instance = config
.set('node', false)
.node.set('__dirname', 'mock')
.set('__filename', 'mock')
.end();

expect(instance).toBe(config);
expect(config.get('node')).toBe(false);
});

test('entry', () => {
const config = new Config();

@@ -171,6 +183,16 @@ test('toConfig with values', () => {
});
});

test('toConfig with node false value', () => {
const config = new Config();

config.set('node', false).node.set('__dirname', 'mock').end();

expect(config.toConfig()).toStrictEqual({
node: false,
});
});

test('merge empty', () => {
const config = new Config();

@@ -323,6 +345,46 @@ test('merge with omit', () => {
});
});

test('merge with node false value', () => {
const config = new Config();

config.node.set('__dirname', 'mock');

const obj = {
node: false,
};

const instance = config.merge(obj);

expect(instance).toBe(config);

expect(config.toConfig()).toStrictEqual({
node: false,
});
});

test('merge with node object value', () => {
const config = new Config();

config.set('node', false);

const obj = {
node: {
__dirname: 'mock',
},
};

const instance = config.merge(obj);

expect(instance).toBe(config);

expect(config.toConfig()).toStrictEqual({
node: {
__dirname: 'mock',
},
});
});

test('validate empty', () => {
const config = new Config();

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

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

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