UNPKG

4.96 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6
7var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })();
8
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
10
11var _postcss = require('postcss');
12
13var _postcss2 = _interopRequireDefault(_postcss);
14
15var _icssReplaceSymbols = require('icss-replace-symbols');
16
17var _icssReplaceSymbols2 = _interopRequireDefault(_icssReplaceSymbols);
18
19var matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
20var matchValueDefinition = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|\w+\([^\)]+\)|[^,]+)\s?/g;
21var matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/;
22var options = {};
23var importIndex = 0;
24var createImportedName = options && options.createImportedName || function (importName /*, path*/) {
25 return 'i__const_' + importName.replace(/\W/g, '_') + '_' + importIndex++;
26};
27
28exports['default'] = function (css, result) {
29 var importAliases = [];
30 var definitions = {};
31
32 var addDefinition = function addDefinition(atRule) {
33 var matches = undefined;
34 while (matches = matchValueDefinition.exec(atRule.params)) {
35 var _matches = matches;
36
37 var _matches2 = _slicedToArray(_matches, 3);
38
39 var /*match*/key = _matches2[1];
40 var value = _matches2[2];
41
42 // Add to the definitions, knowing that values can refer to each other
43 definitions[key] = (0, _icssReplaceSymbols.replaceAll)(definitions, value);
44 atRule.remove();
45 }
46 };
47
48 var addImport = function addImport(atRule) {
49 var matches = matchImports.exec(atRule.params);
50 if (matches) {
51 var _matches3 = _slicedToArray(matches, 3);
52
53 var /*match*/aliases = _matches3[1];
54 var path = _matches3[2];
55
56 // We can use constants for path names
57 if (definitions[path]) path = definitions[path];
58 var imports = aliases.split(/\s*,\s*/).map(function (alias) {
59 var tokens = matchImport.exec(alias);
60 if (tokens) {
61 var _tokens = _slicedToArray(tokens, 3);
62
63 var /*match*/theirName = _tokens[1];
64 var _tokens$2 = _tokens[2];
65 var myName = _tokens$2 === undefined ? theirName : _tokens$2;
66
67 var importedName = createImportedName(myName);
68 definitions[myName] = importedName;
69 return { theirName: theirName, importedName: importedName };
70 } else {
71 throw new Error('@import statement "' + alias + '" is invalid!');
72 }
73 });
74 importAliases.push({ path: path, imports: imports });
75 atRule.remove();
76 }
77 };
78
79 /* Look at all the @value statements and treat them as locals or as imports */
80 css.walkAtRules('value', function (atRule) {
81 if (matchImports.exec(atRule.params)) {
82 addImport(atRule);
83 } else {
84 if (atRule.params.indexOf('@value') !== -1) {
85 result.warn('Invalid value definition: ' + atRule.params);
86 }
87
88 addDefinition(atRule);
89 }
90 });
91
92 /* We want to export anything defined by now, but don't add it to the CSS yet or
93 it well get picked up by the replacement stuff */
94 var exportDeclarations = Object.keys(definitions).map(function (key) {
95 return _postcss2['default'].decl({
96 value: definitions[key],
97 prop: key,
98 raws: { before: "\n " },
99 _autoprefixerDisabled: true
100 });
101 });
102
103 /* If we have no definitions, don't continue */
104 if (!Object.keys(definitions).length) return;
105
106 /* Perform replacements */
107 (0, _icssReplaceSymbols2['default'])(css, definitions);
108
109 /* Add export rules if any */
110 if (exportDeclarations.length > 0) {
111 css.prepend(_postcss2['default'].rule({
112 selector: ':export',
113 raws: { after: "\n" },
114 nodes: exportDeclarations
115 }));
116 }
117
118 /* Add import rules */
119 importAliases.reverse().forEach(function (_ref) {
120 var path = _ref.path;
121 var imports = _ref.imports;
122
123 css.prepend(_postcss2['default'].rule({
124 selector: ':import(' + path + ')',
125 raws: { after: "\n" },
126 nodes: imports.map(function (_ref2) {
127 var theirName = _ref2.theirName;
128 var importedName = _ref2.importedName;
129 return _postcss2['default'].decl({
130 value: theirName,
131 prop: importedName,
132 raws: { before: "\n " },
133 _autoprefixerDisabled: true
134 });
135 })
136 }));
137 });
138};
139
140module.exports = exports['default'];
\No newline at end of file