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

Expose toString as a static method on Config #57

Merged
merged 1 commit into from
May 16, 2018
Merged
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
85 changes: 44 additions & 41 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,49 +43,9 @@ module.exports = class extends ChainedMap {
]);
}

entry(name) {
if (!this.entryPoints.has(name)) {
this.entryPoints.set(name, new ChainedSet(this));
}

return this.entryPoints.get(name);
}

plugin(name) {
if (!this.plugins.has(name)) {
this.plugins.set(name, new Plugin(this, name));
}

return this.plugins.get(name);
}

toConfig() {
const entryPoints = this.entryPoints.entries() || {};

return this.clean(
Object.assign(this.entries() || {}, {
node: this.node.entries(),
output: this.output.entries(),
resolve: this.resolve.toConfig(),
resolveLoader: this.resolveLoader.toConfig(),
devServer: this.devServer.toConfig(),
module: this.module.toConfig(),
optimization: this.optimization.entries(),
plugins: this.plugins.values().map(plugin => plugin.toConfig()),
performance: this.performance.entries(),
entry: Object.keys(entryPoints).reduce(
(acc, key) =>
Object.assign(acc, { [key]: entryPoints[key].values() }),
{}
),
})
);
}

toString({ verbose = false, configPrefix = 'config' } = {}) {
static toString(config, { verbose = false, configPrefix = 'config' } = {}) {
// eslint-disable-next-line global-require
const stringify = require('javascript-stringify');
const config = this.toConfig();

return stringify(
config,
Expand Down Expand Up @@ -134,6 +94,49 @@ module.exports = class extends ChainedMap {
);
}

entry(name) {
if (!this.entryPoints.has(name)) {
this.entryPoints.set(name, new ChainedSet(this));
}

return this.entryPoints.get(name);
}

plugin(name) {
if (!this.plugins.has(name)) {
this.plugins.set(name, new Plugin(this, name));
}

return this.plugins.get(name);
}

toConfig() {
const entryPoints = this.entryPoints.entries() || {};

return this.clean(
Object.assign(this.entries() || {}, {
node: this.node.entries(),
output: this.output.entries(),
resolve: this.resolve.toConfig(),
resolveLoader: this.resolveLoader.toConfig(),
devServer: this.devServer.toConfig(),
module: this.module.toConfig(),
optimization: this.optimization.entries(),
plugins: this.plugins.values().map(plugin => plugin.toConfig()),
performance: this.performance.entries(),
entry: Object.keys(entryPoints).reduce(
(acc, key) =>
Object.assign(acc, { [key]: entryPoints[key].values() }),
{}
),
})
);
}

toString(options) {
return module.exports.toString(this.toConfig(), options);
}

merge(obj = {}, omit = []) {
const omissions = [
'node',
Expand Down