forked from NativeScript/nativescript-dev-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
css2json-loader.js
29 lines (25 loc) · 943 Bytes
/
css2json-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const parse = require("tns-core-modules/css").parse;
const nl = "\n";
module.exports = function(content) {
const ast = parse(content);
const dependencies = getImportsFrom(ast)
.map(mapURI)
.reduce((dependencies, {uri, requireURI}) =>
dependencies + `global.registerModule(${uri}, () => require(${requireURI}));${nl}`, "");
const str = JSON.stringify(ast, (k, v) => k === "position" ? undefined : v);
return `${dependencies}module.exports = ${str};`;
}
function getImportsFrom(ast) {
if (!ast || ast.type !== "stylesheet" || !ast.stylesheet) {
return [];
}
return ast.stylesheet.rules
.filter(rule => rule.type === "import")
.map(importRule => importRule.import.replace(/[\'\"]/gm, ""));
}
function mapURI(uri) {
return {
uri: JSON.stringify(uri),
requireURI: JSON.stringify(uri[0] === "~" && uri[1] !== "/" ? uri.substr(1) : uri)
};
}