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

Commit

Permalink
feat(webpack): now it is compatible with webpack 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mdreizin committed Oct 21, 2016
1 parent f0674a4 commit 141bd46
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
52 changes: 24 additions & 28 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
has
} from 'lodash';
import ConfigDependency from './ConfigDependency';
import ConfigCommandInvoker from './ConfigCommandInvoker';

/**
* @private
* @type {String}
* @type {WeakMap}
*/
const DEPENDENCY_TREE = 'DEPENDENCY_TREE';
const DEPENDENCY_TREE = new WeakMap();

/**
* @private
Expand All @@ -36,21 +37,6 @@ const MERGE_COMMAND = new WeakMap();
*/
const EXTEND_COMMAND = new WeakMap();

/**
* @private
* @param {Config} config
* @param {ConfigCommand} command
* @param {...*} values
* @returns {Config}
*/
const executeCommand = (config, command, ...values) => {
for (const value of values) {
command.execute(config, value);
}

return config;
};

/**
* @class
*/
Expand Down Expand Up @@ -109,24 +95,32 @@ class Config {
*
* config.extend('./test/fixtures/webpack.1.config.js');
*
* for (let {node} of config.dependencyTree) {
* for (const {node} of config.dependencyTree) {
* console.log(node.root.filename);
* }
* // ./test/fixtures/webpack.1.config.js
* // ./test/fixtures/webpack.2.config.js
* // ./test/fixtures/webpack.3.config.js
* // ./test/fixtures/webpack.5.config.js
* // ./test/fixtures/webpack.4.config.js
* @description Keeps information about configs which have been loaded via {@link Config#extend}
* @description Holds information about [included]{@link Config#extend} configs
* @readonly
* @type {ConfigDependency}
*/
get dependencyTree() {
if (!this[DEPENDENCY_TREE]) {
this[DEPENDENCY_TREE] = new ConfigDependency(this);
if (!DEPENDENCY_TREE.has(this)) {
DEPENDENCY_TREE.set(this, new ConfigDependency(this));
}

return this[DEPENDENCY_TREE];
return DEPENDENCY_TREE.get(this);
}

/**
* @private
* @param {ConfigDependency} value
*/
set dependencyTree(value) {
DEPENDENCY_TREE.set(this, value);
}

/**
Expand All @@ -150,7 +144,7 @@ class Config {
* @returns {Config}
*/
defaults(...values) {
return executeCommand(this, this.defaultsCommand, ...values);
return new ConfigCommandInvoker(this.defaultsCommand).invoke(this, ...values);
}

/**
Expand All @@ -175,7 +169,7 @@ class Config {
* @returns {Config}
*/
merge(...values) {
return executeCommand(this, this.mergeCommand, ...values);
return new ConfigCommandInvoker(this.mergeCommand).invoke(this, ...values);
}

/**
Expand Down Expand Up @@ -222,7 +216,7 @@ class Config {
* @returns {Config}
*/
extend(...values) {
return executeCommand(this, this.extendCommand, ...values);
return new ConfigCommandInvoker(this.extendCommand).invoke(this, ...values);
}

/**
Expand All @@ -241,7 +235,11 @@ class Config {
* @returns {Config}
*/
clone() {
return new Config(this.factory, this.defaultsCommand, this.mergeCommand, this.extendCommand).merge(this.toObject());
const config = new Config(this.factory, this.defaultsCommand, this.mergeCommand, this.extendCommand);

config.dependencyTree = new ConfigDependency(config, this.dependencyTree.children);

return config.merge(this.toObject());
}

/**
Expand All @@ -268,8 +266,6 @@ class Config {
}
}

delete properties[DEPENDENCY_TREE];

return properties;
}

Expand Down
41 changes: 41 additions & 0 deletions src/ConfigCommandInvoker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @private
* @type {WeakMap}
*/
const COMMAND = new WeakMap();

/**
* @class
*/
class ConfigCommandInvoker {
/**
* @constructor
* @param {ConfigCommand} command
*/
constructor(command) {
COMMAND.set(this, command);
}

/**
* @readonly
* @type {ConfigCommand}
*/
get command() {
return COMMAND.get(this);
}

/**
* @param {Config} config
* @param {...*} values
* @returns {Config}
*/
invoke(config, ...values) {
for (const value of values) {
this.command.execute(config, value);
}

return config;
}
}

export default ConfigCommandInvoker;
14 changes: 10 additions & 4 deletions src/ConfigFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ class ConfigFactory {

/**
* @private
* @param {Object} options
* @param {Object|Config} value
* @returns {Config}
*/
initWith(options) {
const config = this.container.resolve(Config);
initWith(value) {
let config;

if (value instanceof Config) {
config = value.clone();
} else {
config = this.container.resolve(Config).merge(value);
}

return config.merge(options);
return config;
}

/**
Expand Down

0 comments on commit 141bd46

Please sign in to comment.