UNPKG

1.31 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = wrapInClass;
5
6var _commonTags = require("common-tags");
7
8function wrapInClass(text) {
9 const imports = [];
10 const composes = [];
11 let match;
12 const rImports = /@import.*?(?:$|;)/g;
13 const rComposes = /\b(?:composes\s*?:\s*(.*?)(?:from\s(.+?))?(?=[}\n\r]))/gim; // eslint-disable-next-line no-cond-assign
14
15 while (match = rImports.exec(text)) {
16 imports.push(match[0]);
17 }
18
19 match = null; // eslint-disable-next-line no-cond-assign
20
21 while (match = rComposes.exec(text)) {
22 composes.push(match[0]);
23 }
24
25 text = text.replace(rImports, '').replace(rComposes, ''); // comment prevents Sass from removing the empty class
26
27 let val = _commonTags.stripIndents`
28 .cls1 {
29 ${text.trim() || '/*!*/'}
30 }`; // Components need two css classes, the first being the single root
31 // class with styles and the second is the exported name used in the Styled helper
32 // We need both so that that interpolations have a class that is _only_
33 // the single class, e.g. no additional classes composed in so that it can be used
34 // as a selecto
35
36 if (composes.length) {
37 composes.unshift(`composes: cls1;`);
38 val += `\n.cls2 {\n${composes.join('\n')}\n}`;
39 }
40
41 if (imports.length) val = `${imports.join('\n')}\n${val}`;
42 return val;
43}
\No newline at end of file