Skip to content

Commit 21fbcdd

Browse files
authored
Merge pull request #139 from jehillert/jehillert-patch-1
Update README re multiple metro.config.js transformers
2 parents cf2ac3c + 864aaa2 commit 21fbcdd

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,48 @@ module.exports = {
8787
};
8888
```
8989

90+
##### Projects with Multiple Transformers
91+
If your project at some point requires a metro configuration with additional transformers, consider making a separate `customTransformer.js` file in the project root with logic for delegating files types to the appropriate transformer, and modifying `metro.config.js` file to reference the customer transformer file. For example, if you are using `react-native-svg-transformer`, this would be your custom transformer file:
92+
```js
93+
// root/customTransformer.js
94+
var reactNativeReactBridgeTransformer = require('react-native-react-bridge/lib/plugin');
95+
var svgTransformer = require('react-native-svg-transformer');
96+
97+
module.exports.transform = function ({ src, filename, options }) {
98+
if (filename.endsWith('.svg')) {
99+
return svgTransformer.transform({ src, filename, options });
100+
} else {
101+
return reactNativeReactBridgeTransformer.transform({
102+
src,
103+
filename,
104+
options,
105+
});
106+
}
107+
};
108+
```
109+
110+
And this would be your metro config:
111+
```js
112+
// root/metro.config.js
113+
const { getDefaultConfig } = require('metro-config');
114+
115+
module.exports = (async () => {
116+
const {
117+
resolver: { sourceExts, assetExts },
118+
} = await getDefaultConfig();
119+
return {
120+
transformer: {
121+
babelTransformerPath: require.resolve('./customTransformer.js'),
122+
},
123+
resolver: {
124+
assetExts: assetExts.filter(ext => ext !== 'svg'),
125+
sourceExts: [...sourceExts, 'svg'],
126+
},
127+
};
128+
})();
129+
130+
```
131+
90132
#### Expo
91133

92134
```javascript

0 commit comments

Comments
 (0)