function hasVar(string) {
  return string.includes('var(');
}

function resolveValue(value, maps) {
  return hasVar(value) ? value.replace(/var\(--.*?\)/g, match => maps[match.slice(4, -1)] || match) : value;
}

function circularReference(maps) {
  return Object.keys(maps).reduce((previousMaps, property) => {
    previousMaps[property] = resolveValue(maps[property], maps);
    return previousMaps;
  }, maps);
}

module.exports = (options = {}) => {
  options = {
    atRules: [...new Set(['for', 'if', 'else', 'each', 'mixin', 'custom-media', ...options.atRules || ''])],
    variables: {...options.variables}
  };

  return {
    postcssPlugin: 'postcss-at-rules-variables',
    prepare() {
      let variables = {};
      return {
        Declaration(node) {
          if (node.variable) {
            variables[node.prop] = node.value;
          }
        },
        Once() {
          variables = circularReference(Object.assign(variables, options.variables));
        },
        AtRule(rule) {
          if (options.atRules.includes(rule.name)) {
            rule.params = resolveValue(rule.params, variables);
          }
        }
      };
    }
  };
};

module.exports.postcss = true;

@@ -409,16 +409,318 @@
 ps);%0A%7D%0A%0A
+function getProperty(nodes) %7B%0A  let propertys = %7B%7D;%0A%0A  nodes.walkRules(rule =%3E %7B%0A    if (rule.selector !== ':root') %7B%0A      return;%0A    %7D%0A%0A    rule.each((%7Btype, prop: property, value%7D) =%3E %7B%0A      if (type === 'decl') %7B%0A        propertys%5Bproperty%5D = value;%0A      %7D%0A    %7D);%0A  %7D);%0A%0A  return propertys;%0A%7D%0A%0A
 module.e
@@ -908,16 +908,75 @@
 riables%7D
+,%0A    declarationByWalk: options.declarationByWalk ?? false
 %0A  %7D;%0A%0A 
@@ -1233,20 +1233,182 @@
    Once(
+root
 ) %7B%0A
+          let declarativeVariables = variables;%0A          if (options.declarationByWalk) %7B%0A            declarativeVariables = getProperty(root);%0A          %7D%0A%0A
         
@@ -1453,17 +1453,28 @@
 .assign(
-v
+declarativeV
 ariables

add declarationByWalk option, close issue #209