@@ -13,6 +13,8 @@ var shallowCloneNode = require("./lib/shallow-clone-node");
13
13
var resolveValue = require ( "./lib/resolve-value" ) ;
14
14
var resolveDecl = require ( "./lib/resolve-decl" ) ;
15
15
16
+ const fs = require ( 'fs' ) ;
17
+
16
18
// A custom property is any property whose name starts with two dashes (U+002D HYPHEN-MINUS)
17
19
// `--foo`
18
20
// See: http://dev.w3.org/csswg/css-variables/#custom-property
@@ -61,12 +63,18 @@ var defaults = {
61
63
preserveInjectedVariables : true ,
62
64
// Will write media queries in the same order as in the original file.
63
65
// Currently defaulted to false for legacy behavior. We can update to `true` in a major version
64
- preserveAtRulesOrder : false
66
+ preserveAtRulesOrder : false ,
67
+ // Export all var statement usages to a file.
68
+ exportVarUsagesTo : null ,
65
69
} ;
66
70
67
71
module . exports = postcss . plugin ( "postcss-css-variables" , function ( options ) {
68
72
var opts = extend ( { } , defaults , options ) ;
69
73
74
+ if ( ! [ 'object' ] . includes ( typeof opts . exportVarUsagesTo ) ) {
75
+ throw new Error ( '`exportVarUsagesTo` must be a valid location.' )
76
+ }
77
+
70
78
// Work with opts here
71
79
72
80
return function ( css , result ) {
@@ -217,26 +225,29 @@ module.exports = postcss.plugin("postcss-css-variables", function(options) {
217
225
if ( rule . nodes === undefined ) return ;
218
226
219
227
var doesRuleUseVariables = rule . nodes . some ( function ( node ) {
220
- if ( node . type === "decl" ) {
221
- var decl = node ;
222
- // If it uses variables
223
- // and is not a variable declarations that we may be preserving from earlier
224
- if (
225
- resolveValue . RE_VAR_FUNC . test ( decl . value ) &&
226
- ! RE_VAR_PROP . test ( decl . prop )
227
- ) {
228
- return true ;
229
- }
230
- }
231
-
232
- return false ;
228
+ // If it uses variables
229
+ // and is not a variable declarations that we may be preserving from earlier
230
+ return node . type === "decl"
231
+ && resolveValue . RE_VAR_FUNC . test ( node . value )
232
+ && ! RE_VAR_PROP . test ( node . prop )
233
233
} ) ;
234
234
235
235
if ( doesRuleUseVariables ) {
236
236
rulesThatHaveDeclarationsWithVariablesList . push ( rule ) ;
237
237
}
238
238
} ) ;
239
239
240
+ const allVars = { }
241
+
242
+ const shouldExport = ( ) => ! ! opts . exportVarUsagesTo
243
+
244
+ const collectVar = ! shouldExport ( ) ? null : ( cssVar ) => {
245
+ if ( ! allVars [ cssVar . name ] ) {
246
+ allVars [ cssVar . name ] = { usages : [ ] } ;
247
+ }
248
+ allVars [ cssVar . name ] . usages . push ( cssVar . usage ) ;
249
+ }
250
+
240
251
rulesThatHaveDeclarationsWithVariablesList . forEach ( function ( rule ) {
241
252
var rulesToWorkOn = [ ] . concat ( rule ) ;
242
253
// Split out the rule into each comma separated selector piece
@@ -256,16 +267,20 @@ module.exports = postcss.plugin("postcss-css-variables", function(options) {
256
267
// Resolve the declarations
257
268
rulesToWorkOn . forEach ( function ( ruleToWorkOn ) {
258
269
ruleToWorkOn . nodes . slice ( 0 ) . forEach ( function ( node ) {
259
- if ( node . type === "decl" ) {
260
- var decl = node ;
261
- resolveDecl (
262
- decl ,
263
- map ,
264
- opts . preserve ,
265
- opts . preserveAtRulesOrder ,
266
- logResolveValueResult
267
- ) ;
270
+ if ( node . type !== "decl" ) {
271
+ return ;
268
272
}
273
+ let preserveAtRulesOrder ;
274
+ resolveDecl (
275
+ node ,
276
+ map ,
277
+ {
278
+ shouldPreserve : opts . preserve ,
279
+ preserveAtRulesOrder : opts . preserveAtRulesOrder ,
280
+ logResolveValueResult,
281
+ collectVar,
282
+ } ,
283
+ ) ;
269
284
} ) ;
270
285
} ) ;
271
286
} ) ;
@@ -279,6 +294,19 @@ module.exports = postcss.plugin("postcss-css-variables", function(options) {
279
294
injectedDecl . remove ( ) ;
280
295
} ) ;
281
296
297
+ // Merge variables with the ones that are already in the passed object.
298
+ if ( opts . exportVarUsagesTo && allVars ) {
299
+ Object . keys ( allVars ) . forEach ( varName => {
300
+ if ( 'undefined' === typeof opts . exportVarUsagesTo [ varName ] ) {
301
+ opts . exportVarUsagesTo [ varName ] = {
302
+ name : varName ,
303
+ usages : [ ] ,
304
+ } ;
305
+ }
306
+ opts . exportVarUsagesTo [ varName ] . usages . push ( ...allVars [ varName ] . usages ) ;
307
+ } ) ;
308
+ }
309
+
282
310
//console.log('map', map);
283
311
284
312
/* * /
0 commit comments