UNPKG

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