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

Commit 141bd46

Browse files
authored
feat(webpack): now it is compatible with webpack 2
1 parent f0674a4 commit 141bd46

File tree

3 files changed

+75
-32
lines changed

3 files changed

+75
-32
lines changed

src/Config.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import {
55
has
66
} from 'lodash';
77
import ConfigDependency from './ConfigDependency';
8+
import ConfigCommandInvoker from './ConfigCommandInvoker';
89

910
/**
1011
* @private
11-
* @type {String}
12+
* @type {WeakMap}
1213
*/
13-
const DEPENDENCY_TREE = 'DEPENDENCY_TREE';
14+
const DEPENDENCY_TREE = new WeakMap();
1415

1516
/**
1617
* @private
@@ -36,21 +37,6 @@ const MERGE_COMMAND = new WeakMap();
3637
*/
3738
const EXTEND_COMMAND = new WeakMap();
3839

39-
/**
40-
* @private
41-
* @param {Config} config
42-
* @param {ConfigCommand} command
43-
* @param {...*} values
44-
* @returns {Config}
45-
*/
46-
const executeCommand = (config, command, ...values) => {
47-
for (const value of values) {
48-
command.execute(config, value);
49-
}
50-
51-
return config;
52-
};
53-
5440
/**
5541
* @class
5642
*/
@@ -109,24 +95,32 @@ class Config {
10995
*
11096
* config.extend('./test/fixtures/webpack.1.config.js');
11197
*
112-
* for (let {node} of config.dependencyTree) {
98+
* for (const {node} of config.dependencyTree) {
11399
* console.log(node.root.filename);
114100
* }
115101
* // ./test/fixtures/webpack.1.config.js
116102
* // ./test/fixtures/webpack.2.config.js
117103
* // ./test/fixtures/webpack.3.config.js
118104
* // ./test/fixtures/webpack.5.config.js
119105
* // ./test/fixtures/webpack.4.config.js
120-
* @description Keeps information about configs which have been loaded via {@link Config#extend}
106+
* @description Holds information about [included]{@link Config#extend} configs
121107
* @readonly
122108
* @type {ConfigDependency}
123109
*/
124110
get dependencyTree() {
125-
if (!this[DEPENDENCY_TREE]) {
126-
this[DEPENDENCY_TREE] = new ConfigDependency(this);
111+
if (!DEPENDENCY_TREE.has(this)) {
112+
DEPENDENCY_TREE.set(this, new ConfigDependency(this));
127113
}
128114

129-
return this[DEPENDENCY_TREE];
115+
return DEPENDENCY_TREE.get(this);
116+
}
117+
118+
/**
119+
* @private
120+
* @param {ConfigDependency} value
121+
*/
122+
set dependencyTree(value) {
123+
DEPENDENCY_TREE.set(this, value);
130124
}
131125

132126
/**
@@ -150,7 +144,7 @@ class Config {
150144
* @returns {Config}
151145
*/
152146
defaults(...values) {
153-
return executeCommand(this, this.defaultsCommand, ...values);
147+
return new ConfigCommandInvoker(this.defaultsCommand).invoke(this, ...values);
154148
}
155149

156150
/**
@@ -175,7 +169,7 @@ class Config {
175169
* @returns {Config}
176170
*/
177171
merge(...values) {
178-
return executeCommand(this, this.mergeCommand, ...values);
172+
return new ConfigCommandInvoker(this.mergeCommand).invoke(this, ...values);
179173
}
180174

181175
/**
@@ -222,7 +216,7 @@ class Config {
222216
* @returns {Config}
223217
*/
224218
extend(...values) {
225-
return executeCommand(this, this.extendCommand, ...values);
219+
return new ConfigCommandInvoker(this.extendCommand).invoke(this, ...values);
226220
}
227221

228222
/**
@@ -241,7 +235,11 @@ class Config {
241235
* @returns {Config}
242236
*/
243237
clone() {
244-
return new Config(this.factory, this.defaultsCommand, this.mergeCommand, this.extendCommand).merge(this.toObject());
238+
const config = new Config(this.factory, this.defaultsCommand, this.mergeCommand, this.extendCommand);
239+
240+
config.dependencyTree = new ConfigDependency(config, this.dependencyTree.children);
241+
242+
return config.merge(this.toObject());
245243
}
246244

247245
/**
@@ -268,8 +266,6 @@ class Config {
268266
}
269267
}
270268

271-
delete properties[DEPENDENCY_TREE];
272-
273269
return properties;
274270
}
275271

src/ConfigCommandInvoker.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @private
3+
* @type {WeakMap}
4+
*/
5+
const COMMAND = new WeakMap();
6+
7+
/**
8+
* @class
9+
*/
10+
class ConfigCommandInvoker {
11+
/**
12+
* @constructor
13+
* @param {ConfigCommand} command
14+
*/
15+
constructor(command) {
16+
COMMAND.set(this, command);
17+
}
18+
19+
/**
20+
* @readonly
21+
* @type {ConfigCommand}
22+
*/
23+
get command() {
24+
return COMMAND.get(this);
25+
}
26+
27+
/**
28+
* @param {Config} config
29+
* @param {...*} values
30+
* @returns {Config}
31+
*/
32+
invoke(config, ...values) {
33+
for (const value of values) {
34+
this.command.execute(config, value);
35+
}
36+
37+
return config;
38+
}
39+
}
40+
41+
export default ConfigCommandInvoker;

src/ConfigFactory.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ class ConfigFactory {
3333

3434
/**
3535
* @private
36-
* @param {Object} options
36+
* @param {Object|Config} value
3737
* @returns {Config}
3838
*/
39-
initWith(options) {
40-
const config = this.container.resolve(Config);
39+
initWith(value) {
40+
let config;
41+
42+
if (value instanceof Config) {
43+
config = value.clone();
44+
} else {
45+
config = this.container.resolve(Config).merge(value);
46+
}
4147

42-
return config.merge(options);
48+
return config;
4349
}
4450

4551
/**

0 commit comments

Comments
 (0)