1 | const parse = require("tns-core-modules/css").parse;
|
2 | const nl = "\n";
|
3 |
|
4 | module.exports = function (content, map) {
|
5 | const ast = parse(content);
|
6 | const dependencies = getImportsFrom(ast)
|
7 | .map(mapURI)
|
8 | .reduce((dependencies, { uri, requireURI }) =>
|
9 | dependencies + `global.registerModule(${uri}, () => require(${requireURI}));${nl}`, "");
|
10 |
|
11 | const str = JSON.stringify(ast, (k, v) => k === "position" ? undefined : v);
|
12 | this.callback(null, `${dependencies}module.exports = ${str};`, map);
|
13 | }
|
14 |
|
15 | function getImportsFrom(ast) {
|
16 | if (!ast || ast.type !== "stylesheet" || !ast.stylesheet) {
|
17 | return [];
|
18 | }
|
19 | return ast.stylesheet.rules
|
20 | .filter(rule => rule.type === "import")
|
21 | .map(importRule => importRule.import.replace(/[\'\"]/gm, ""));
|
22 | }
|
23 |
|
24 | function mapURI(uri) {
|
25 | return {
|
26 | uri: JSON.stringify(uri),
|
27 | requireURI: JSON.stringify(uri[0] === "~" && uri[1] !== "/" ? uri.substr(1) : uri)
|
28 | };
|
29 | }
|