forked from kristerkari/react-native-stylus-transformer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
42 lines (36 loc) · 1.37 KB
/
index.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
30
31
32
33
34
35
36
37
38
39
40
41
42
var semver = require("semver");
var css2rn = require("css-to-react-native-transform").default;
var stylus = require("stylus");
var upstreamTransformer = null;
var reactNativeVersionString = require("react-native/package.json").version;
var reactNativeMinorVersion = semver(reactNativeVersionString).minor;
if (reactNativeMinorVersion >= 52) {
upstreamTransformer = require("metro/src/transformer");
} else if (reactNativeMinorVersion >= 47) {
upstreamTransformer = require("metro-bundler/src/transformer");
} else if (reactNativeMinorVersion === 46) {
upstreamTransformer = require("metro-bundler/build/transformer");
} else {
// handle RN <= 0.45
var oldUpstreamTransformer = require("react-native/packager/transformer");
upstreamTransformer = {
transform({ src, filename, options }) {
return oldUpstreamTransformer.transform(src, filename, options);
}
};
}
module.exports.transform = function(src, filename, options) {
if (typeof src === "object") {
// handle RN >= 0.46
({ src, filename, options } = src);
}
if (filename.endsWith(".styl")) {
var cssObject = css2rn(stylus.render(src, {filename}), { parseMediaQueries: true });
return upstreamTransformer.transform({
src: "module.exports = " + JSON.stringify(cssObject),
filename,
options
});
}
return upstreamTransformer.transform({ src, filename, options });
};