UNPKG

1.74 kBJavaScriptView Raw
1/**
2 * This script parses "global.css" for custom CSS properties and returns an object like
3 * {'--main-color': '#FFF'}, which required for postcss-css-properties plugin
4 *
5 * Inspired by https://github.com/jonathantneal/postcss-export-custom-variables
6 */
7
8// eslint-disable-next-line no-console
9console.warn(`
10 **** WARNING: Ring UI extract-css-vars.js is deprecated since 2.0 in favor of "importFrom" option of postcss-preset-env ****
11 **** Consider using the following way of including variables: ****
12 'postcss-preset-env': {
13 importFrom: require.resolve('@jetbrains/ring-ui/components/global/variables.css'),
14 features: {
15 'postcss-custom-properties': {
16 preserve: true
17 }
18 }
19 }
20 **** More details: https://github.com/postcss/postcss-custom-properties#importfrom, https://github.com/csstools/postcss-preset-env#importfrom ***
21`);
22
23const fs = require('fs');
24
25const postcss = require('postcss');
26
27const customPropertyMatch = /^--([_a-zA-Z]+[_a-zA-Z0-9-]*)$/;
28
29function isCustomProperty(node) {
30 return node.type === 'decl' && customPropertyMatch.test(node.prop);
31}
32
33const ExtractVariablesPlugin = postcss.
34 plugin('postcss-export-ring-variables', () => {
35 const variables = {};
36
37 return (root, result) => {
38 root.walk(node => {
39 if (isCustomProperty(node)) {
40 const [, property] = node.prop.match(customPropertyMatch);
41
42 variables[`--${property}`] = node.value;
43 }
44 });
45
46 result.parsedVariables = variables;
47 };
48 });
49
50const variablesSource = fs.
51 readFileSync(require.resolve('./components/global/variables.css'));
52
53const res = ExtractVariablesPlugin.
54 process(variablesSource.toString(), {}, {}).sync();
55
56module.exports = res.parsedVariables;