source: trip-planner-front/node_modules/postcss-env-function/index.cjs.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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