-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmetro.transformer.js
More file actions
73 lines (63 loc) · 2.13 KB
/
metro.transformer.js
File metadata and controls
73 lines (63 loc) · 2.13 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const upstreamTransformer = require("@expo/metro-config/babel-transformer");
const { createTransformer } = require("@bacons/mdx/metro-transformer");
const remarkFrontmatter = require("remark-frontmatter").default;
const remarkMdxFrontmatter = require("remark-mdx-frontmatter").default;
// Create MDX transformer with frontmatter support
const mdxTransformer = createTransformer({
remarkPlugins: [remarkFrontmatter, remarkMdxFrontmatter],
});
async function convertSvgModule(projectRoot, src, options) {
const { resolveConfig, transform } = require("@svgr/core");
const isNotNative = !options.platform || options.platform === "web";
const defaultSVGRConfig = {
native: !isNotNative,
plugins: ["@svgr/plugin-svgo", "@svgr/plugin-jsx"],
svgoConfig: {
// TODO: Maybe there's a better config for web?
plugins: [
{
name: "preset-default",
params: {
overrides: {
inlineStyles: {
onlyMatchedOnce: false,
},
removeViewBox: false,
removeUnknownsAndDefaults: false,
convertColors: false,
},
},
},
],
},
};
const svgUserConfig = await resolveConfig(projectRoot);
const svgrConfig = svgUserConfig
? { ...defaultSVGRConfig, ...svgUserConfig }
: defaultSVGRConfig;
const output = await transform(
src,
// @ts-expect-error
svgrConfig
);
if (isNotNative) {
// If the SVG is not native, we need to add a wrapper to make it work
return output;
}
// RNSVG doesn't support RSC yet.
return '"use client";\n' + output;
}
module.exports.transform = async ({ src, filename, options }) => {
// Handle MDX/MD files
if (filename.endsWith(".mdx") || filename.endsWith(".md")) {
src = (await mdxTransformer.transform({ src, filename, options })).src;
}
// Handle SVG files
if (filename.endsWith(".svg")) {
src = await convertSvgModule(options.projectRoot, src, {
platform: options.platform,
});
}
// Pass the source through the upstream Expo transformer.
return upstreamTransformer.transform({ src, filename, options });
};