@@ -5,12 +5,13 @@ import {
55 has
66} from 'lodash' ;
77import 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 */
3738const 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
0 commit comments