[6a3a178] | 1 | import parser from 'postcss-values-parser';
|
---|
| 2 | import fs from 'fs';
|
---|
| 3 | import path from 'path';
|
---|
| 4 | import postcss from 'postcss';
|
---|
| 5 |
|
---|
| 6 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
---|
| 7 | try {
|
---|
| 8 | var info = gen[key](arg);
|
---|
| 9 | var value = info.value;
|
---|
| 10 | } catch (error) {
|
---|
| 11 | reject(error);
|
---|
| 12 | return;
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | if (info.done) {
|
---|
| 16 | resolve(value);
|
---|
| 17 | } else {
|
---|
| 18 | Promise.resolve(value).then(_next, _throw);
|
---|
| 19 | }
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | function _asyncToGenerator(fn) {
|
---|
| 23 | return function () {
|
---|
| 24 | var self = this,
|
---|
| 25 | args = arguments;
|
---|
| 26 | return new Promise(function (resolve, reject) {
|
---|
| 27 | var gen = fn.apply(self, args);
|
---|
| 28 |
|
---|
| 29 | function _next(value) {
|
---|
| 30 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | function _throw(err) {
|
---|
| 34 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | _next(undefined);
|
---|
| 38 | });
|
---|
| 39 | };
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | const dashedMatch = /^--/; // returns the value of a css function as a string
|
---|
| 43 |
|
---|
| 44 | var getFnValue = (node => {
|
---|
| 45 | const value = String(node.nodes.slice(1, -1));
|
---|
| 46 | return dashedMatch.test(value) ? value : undefined;
|
---|
| 47 | });
|
---|
| 48 |
|
---|
| 49 | var updateEnvValue = ((node, variables) => {
|
---|
| 50 | // get the value of a css function as a string
|
---|
| 51 | const value = getFnValue(node);
|
---|
| 52 |
|
---|
| 53 | if (typeof value === 'string' && value in variables) {
|
---|
| 54 | node.replaceWith(...asClonedArrayWithBeforeSpacing(variables[value], node.raws.before));
|
---|
| 55 | }
|
---|
| 56 | }); // return an array with its nodes cloned, preserving the raw
|
---|
| 57 |
|
---|
| 58 | const asClonedArrayWithBeforeSpacing = (array, beforeSpacing) => {
|
---|
| 59 | const clonedArray = asClonedArray(array, null);
|
---|
| 60 |
|
---|
| 61 | if (clonedArray[0]) {
|
---|
| 62 | clonedArray[0].raws.before = beforeSpacing;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | return clonedArray;
|
---|
| 66 | }; // return an array with its nodes cloned
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | const asClonedArray = (array, parent) => array.map(node => asClonedNode(node, parent)); // return a cloned node
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | const asClonedNode = (node, parent) => {
|
---|
| 73 | const cloneNode = new node.constructor(node);
|
---|
| 74 |
|
---|
| 75 | for (const key in node) {
|
---|
| 76 | if (key === 'parent') {
|
---|
| 77 | cloneNode.parent = parent;
|
---|
| 78 | } else if (Object(node[key]).constructor === Array) {
|
---|
| 79 | cloneNode[key] = asClonedArray(node.nodes, cloneNode);
|
---|
| 80 | } else if (Object(node[key]).constructor === Object) {
|
---|
| 81 | cloneNode[key] = Object.assign({}, node[key]);
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | return cloneNode;
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | // returns whether a node is a css env() function
|
---|
| 89 | var isEnvFunc = (node => node && node.type === 'func' && node.value === 'env');
|
---|
| 90 |
|
---|
| 91 | function walk(node, fn) {
|
---|
| 92 | node.nodes.slice(0).forEach(childNode => {
|
---|
| 93 | if (childNode.nodes) {
|
---|
| 94 | walk(childNode, fn);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | if (isEnvFunc(childNode)) {
|
---|
| 98 | fn(childNode);
|
---|
| 99 | }
|
---|
| 100 | });
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | var getReplacedValue = ((originalValue, variables) => {
|
---|
| 104 | // get the ast of the original value
|
---|
| 105 | const ast = parser(originalValue).parse(); // walk all of the css env() functions
|
---|
| 106 |
|
---|
| 107 | walk(ast, node => {
|
---|
| 108 | // update the environment value for the css env() function
|
---|
| 109 | updateEnvValue(node, variables);
|
---|
| 110 | }); // return the stringified ast
|
---|
| 111 |
|
---|
| 112 | return String(ast);
|
---|
| 113 | });
|
---|
| 114 |
|
---|
| 115 | // returns whether a node is an at-rule
|
---|
| 116 | var isAtrule = (node => node && node.type === 'atrule');
|
---|
| 117 |
|
---|
| 118 | // returns whether a node is a declaration
|
---|
| 119 | var isDecl = (node => node && node.type === 'decl');
|
---|
| 120 |
|
---|
| 121 | var getSupportedValue = (node => isAtrule(node) && node.params || isDecl(node) && node.value);
|
---|
| 122 |
|
---|
| 123 | function setSupportedValue (node, value) {
|
---|
| 124 | if (isAtrule(node)) {
|
---|
| 125 | node.params = value;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | if (isDecl(node)) {
|
---|
| 129 | node.value = value;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /* Import Custom Properties from Object
|
---|
| 134 | /* ========================================================================== */
|
---|
| 135 |
|
---|
| 136 | function importEnvironmentVariablesFromObject(object) {
|
---|
| 137 | const environmentVariables = Object.assign({}, Object(object).environmentVariables || Object(object)['environment-variables']);
|
---|
| 138 |
|
---|
| 139 | for (const key in environmentVariables) {
|
---|
| 140 | environmentVariables[key] = parser(environmentVariables[key]).parse().nodes;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | return environmentVariables;
|
---|
| 144 | }
|
---|
| 145 | /* Import Custom Properties from JSON file
|
---|
| 146 | /* ========================================================================== */
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | function importEnvironmentVariablesFromJSONFile(_x) {
|
---|
| 150 | return _importEnvironmentVariablesFromJSONFile.apply(this, arguments);
|
---|
| 151 | }
|
---|
| 152 | /* Import Custom Properties from JS file
|
---|
| 153 | /* ========================================================================== */
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | function _importEnvironmentVariablesFromJSONFile() {
|
---|
| 157 | _importEnvironmentVariablesFromJSONFile = _asyncToGenerator(function* (from) {
|
---|
| 158 | const object = yield readJSON(path.resolve(from));
|
---|
| 159 | return importEnvironmentVariablesFromObject(object);
|
---|
| 160 | });
|
---|
| 161 | return _importEnvironmentVariablesFromJSONFile.apply(this, arguments);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | function importEnvironmentVariablesFromJSFile(_x2) {
|
---|
| 165 | return _importEnvironmentVariablesFromJSFile.apply(this, arguments);
|
---|
| 166 | }
|
---|
| 167 | /* Import Custom Properties from Sources
|
---|
| 168 | /* ========================================================================== */
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | function _importEnvironmentVariablesFromJSFile() {
|
---|
| 172 | _importEnvironmentVariablesFromJSFile = _asyncToGenerator(function* (from) {
|
---|
| 173 | const object = yield import(path.resolve(from));
|
---|
| 174 | return importEnvironmentVariablesFromObject(object);
|
---|
| 175 | });
|
---|
| 176 | return _importEnvironmentVariablesFromJSFile.apply(this, arguments);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | function importEnvironmentVariablesFromSources(sources) {
|
---|
| 180 | return sources.map(source => {
|
---|
| 181 | if (source instanceof Promise) {
|
---|
| 182 | return source;
|
---|
| 183 | } else if (source instanceof Function) {
|
---|
| 184 | return source();
|
---|
| 185 | } // read the source as an object
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | const opts = source === Object(source) ? source : {
|
---|
| 189 | from: String(source)
|
---|
| 190 | }; // skip objects with Custom Properties
|
---|
| 191 |
|
---|
| 192 | if (opts.environmentVariables || opts['environment-variables']) {
|
---|
| 193 | return opts;
|
---|
| 194 | } // source pathname
|
---|
| 195 |
|
---|
| 196 |
|
---|
| 197 | const from = String(opts.from || ''); // type of file being read from
|
---|
| 198 |
|
---|
| 199 | const type = (opts.type || path.extname(from).slice(1)).toLowerCase();
|
---|
| 200 | return {
|
---|
| 201 | type,
|
---|
| 202 | from
|
---|
| 203 | };
|
---|
| 204 | }).reduce(
|
---|
| 205 | /*#__PURE__*/
|
---|
| 206 | function () {
|
---|
| 207 | var _ref = _asyncToGenerator(function* (environmentVariables, source) {
|
---|
| 208 | const _ref2 = yield source,
|
---|
| 209 | type = _ref2.type,
|
---|
| 210 | from = _ref2.from;
|
---|
| 211 |
|
---|
| 212 | if (type === 'js') {
|
---|
| 213 | return Object.assign(environmentVariables, (yield importEnvironmentVariablesFromJSFile(from)));
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | if (type === 'json') {
|
---|
| 217 | return Object.assign(environmentVariables, (yield importEnvironmentVariablesFromJSONFile(from)));
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | return Object.assign(environmentVariables, importEnvironmentVariablesFromObject((yield source)));
|
---|
| 221 | });
|
---|
| 222 |
|
---|
| 223 | return function (_x3, _x4) {
|
---|
| 224 | return _ref.apply(this, arguments);
|
---|
| 225 | };
|
---|
| 226 | }(), {});
|
---|
| 227 | }
|
---|
| 228 | /* Helper utilities
|
---|
| 229 | /* ========================================================================== */
|
---|
| 230 |
|
---|
| 231 | const readFile = from => new Promise((resolve, reject) => {
|
---|
| 232 | fs.readFile(from, 'utf8', (error, result) => {
|
---|
| 233 | if (error) {
|
---|
| 234 | reject(error);
|
---|
| 235 | } else {
|
---|
| 236 | resolve(result);
|
---|
| 237 | }
|
---|
| 238 | });
|
---|
| 239 | });
|
---|
| 240 |
|
---|
| 241 | const readJSON =
|
---|
| 242 | /*#__PURE__*/
|
---|
| 243 | function () {
|
---|
| 244 | var _ref3 = _asyncToGenerator(function* (from) {
|
---|
| 245 | return JSON.parse((yield readFile(from)));
|
---|
| 246 | });
|
---|
| 247 |
|
---|
| 248 | return function readJSON(_x5) {
|
---|
| 249 | return _ref3.apply(this, arguments);
|
---|
| 250 | };
|
---|
| 251 | }();
|
---|
| 252 |
|
---|
| 253 | var index = postcss.plugin('postcss-env-fn', opts => {
|
---|
| 254 | // sources to import environment variables from
|
---|
| 255 | const importFrom = [].concat(Object(opts).importFrom || []); // promise any environment variables are imported
|
---|
| 256 |
|
---|
| 257 | const environmentVariablesPromise = importEnvironmentVariablesFromSources(importFrom);
|
---|
| 258 | return (
|
---|
| 259 | /*#__PURE__*/
|
---|
| 260 | function () {
|
---|
| 261 | var _ref = _asyncToGenerator(function* (root) {
|
---|
| 262 | const environmentVariables = yield environmentVariablesPromise;
|
---|
| 263 | root.walk(node => {
|
---|
| 264 | const supportedValue = getSupportedValue(node);
|
---|
| 265 |
|
---|
| 266 | if (supportedValue) {
|
---|
| 267 | const replacedValue = getReplacedValue(supportedValue, environmentVariables);
|
---|
| 268 |
|
---|
| 269 | if (replacedValue !== supportedValue) {
|
---|
| 270 | setSupportedValue(node, replacedValue);
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | });
|
---|
| 274 | });
|
---|
| 275 |
|
---|
| 276 | return function (_x) {
|
---|
| 277 | return _ref.apply(this, arguments);
|
---|
| 278 | };
|
---|
| 279 | }()
|
---|
| 280 | );
|
---|
| 281 | });
|
---|
| 282 |
|
---|
| 283 | export default index;
|
---|
| 284 | //# sourceMappingURL=index.es.mjs.map
|
---|