forked from kristerkari/react-native-svg-transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (43 loc) · 1.23 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
43
44
45
46
const svgr = require("@svgr/core").transform;
const resolveConfig = require("@svgr/core").resolveConfig;
const resolveConfigDir = require("path-dirname");
const upstreamTransformer = require("@react-native/metro-babel-transformer");
const defaultSvgrConfig = {
native: true,
plugins: ["@svgr/plugin-svgo", "@svgr/plugin-jsx"],
svgoConfig: {
plugins: [
{
name: "preset-default",
params: {
overrides: {
removeViewBox: false,
removeUnknownsAndDefaults: false,
convertColors: false,
inlineStyles: {
onlyMatchedOnce: false
}
}
}
}
]
}
};
function transform({ src, filename, options }) {
if (filename.endsWith(".svg")) {
const config = resolveConfig.sync(resolveConfigDir(filename));
const svgrConfig =
config != null ? { ...defaultSvgrConfig, ...config } : defaultSvgrConfig;
const jsCode = svgr.sync(src, svgrConfig, { filePath: filename });
return upstreamTransformer.transform({
src: jsCode,
filename,
options
});
}
return upstreamTransformer.transform({ src, filename, options });
}
module.exports = {
transform,
getCacheKey: upstreamTransformer.getCacheKey
};