UNPKG

2.81 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
8
9function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { 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; } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }
10
11var _postcss = require('postcss');
12
13var _postcss2 = _interopRequireDefault(_postcss);
14
15var declWhitelist = ['composes'],
16 declFilter = new RegExp('^(' + declWhitelist.join('|') + ')$'),
17 matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)')$/;
18
19var processor = _postcss2['default'].plugin('modules-extract-imports', function (options) {
20 return function (css) {
21 var imports = {},
22 importIndex = 0,
23 createImportedName = options && options.createImportedName || function (importName /*, path*/) {
24 return 'i__imported_' + importName.replace(/\W/g, '_') + '_' + importIndex++;
25 };
26
27 // Find any declaration that supports imports
28 css.eachDecl(declFilter, function (decl) {
29 var matches = decl.value.match(matchImports);
30 if (matches) {
31 (function () {
32 var _matches = _slicedToArray(matches, 4);
33
34 var symbols = _matches[1];
35 var doubleQuotePath = _matches[2];
36 var singleQuotePath = _matches[3];
37
38 var path = doubleQuotePath || singleQuotePath;
39 imports[path] = imports[path] || {};
40 var tmpSymbols = symbols.split(/\s+/).map(function (s) {
41 if (!imports[path][s]) {
42 imports[path][s] = createImportedName(s, path);
43 }
44 return imports[path][s];
45 });
46 decl.value = tmpSymbols.join(' ');
47 })();
48 }
49 });
50
51 // If we've found any imports, insert :import rules
52 Object.keys(imports).reverse().forEach(function (path) {
53 var pathImports = imports[path];
54 css.prepend(_postcss2['default'].rule({
55 selector: ':import("' + path + '")',
56 after: '\n',
57 nodes: Object.keys(pathImports).map(function (importedSymbol) {
58 return _postcss2['default'].decl({
59 value: importedSymbol,
60 prop: pathImports[importedSymbol],
61 before: '\n ',
62 _autoprefixerDisabled: true
63 });
64 })
65 }));
66 });
67 };
68});
69
70exports['default'] = processor;
71module.exports = exports['default'];
72/*match*/
\No newline at end of file