Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit ec3f9cb

Browse files
committed
Implement CSS loader that parses the CSS to JSON, Implement platform suffix file resolver plugin
1 parent d17ea4d commit ec3f9cb

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

Diff for: PlatformSuffixPlugin.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const parseFile = require("path").parse;
2+
3+
function PlatformSuffixPlugin(platform, platforms) {
4+
this.platform = platform;
5+
this.platforms = platforms || ["ios", "android"];
6+
}
7+
module.exports = PlatformSuffixPlugin;
8+
9+
PlatformSuffixPlugin.prototype.apply = function(resolver) {
10+
var platform = this.platform;
11+
var platforms = this.platforms;
12+
13+
resolver.plugin("file", function(request, callback) {
14+
const fs = this.fileSystem;
15+
const file = this.join(request.path, request.request);
16+
const query = request.query;
17+
const pFile = parseFile(file);
18+
const platformFile = this.join(pFile.dir, pFile.name + ("." + platform) + pFile.ext);
19+
fs.stat(platformFile, (err, stat) => {
20+
if (!err && stat && stat.isFile()) {
21+
const err = undefined;
22+
const path = platformFile;
23+
callback(err, { file: true, path, query });
24+
} else {
25+
fs.stat(file, (err, stat) => {
26+
if (!err && stat && stat.isFile()) {
27+
const err = undefined;
28+
const path = file;
29+
callback(err, { file: true, path, query });
30+
} else {
31+
callback();
32+
}
33+
});
34+
}
35+
});
36+
});
37+
}

Diff for: css2json-loader.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
console.log("Here: " + __dirname);
2+
3+
const parse = require("tns-core-modules/css").parse;
4+
5+
const nl = "\n";
6+
7+
function escape(string) {
8+
return JSON.stringify(string);
9+
}
10+
11+
function importsFrom(ast) {
12+
if (!ast || ast.type !== "stylesheet" || !ast.stylesheet) {
13+
return [];
14+
}
15+
return ast.stylesheet.rules
16+
.filter(rule => rule.type === "import")
17+
.map(importRule => importRule.import.replace(/[\'\"]/gm, ""));
18+
}
19+
20+
// Identity loader
21+
module.exports = function(content) {
22+
this.cacheable && this.cacheable();
23+
this.value = content;
24+
const ast = parse(content);
25+
26+
let dependencies = "";
27+
28+
importsFrom(ast).forEach(uri => {
29+
if (uri[0] === "~" && uri[1] !== "/") {
30+
// Require form node modules from `~nativescript-theme` like imports.
31+
dependencies += `global.registerModule(${escape(uri)}, () => require(${escape(uri.substr(1))}));${nl}`;
32+
} else {
33+
dependencies += `global.registerModule(${escape(uri)}, () => require(${escape(uri)});${nl}`;
34+
}
35+
});
36+
37+
const str = JSON.stringify(ast, (k, v) => k === "position" ? undefined : v);
38+
return `${dependencies}module.exports = ${str};`;
39+
}
40+
module.exports.seperable = true;
41+

0 commit comments

Comments
 (0)