feat: add metro transformer#1999
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
size-limit report 📦
|
7365339 to
43c8e2b
Compare
5cd3e1c to
276128c
Compare
9cc5041 to
3da57f3
Compare
|
@vonovak thank you! Looks very nice to me, just left a small comment in the I was thinking about including it in the v5 release but realized that there is no reason to postpone it, and it would probably be better to ship it in v4 so that users can already use it. I hope it will not cause a lot of conflicts when merging |
@andrii-bodnar I don't mind merging into |
|
@vonovak I plan to release 4.12.0 soon, it would be great to include this PR in it. By the way, I was looking for some topics to improve the Lingui blog. This seems to be a great one. Would you like to write a little blog post about the Metro transformer? P.S. I can share the background that I use for covers if needed. |
|
Hey guys, how to configure this in case there's another const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;
metroConfig = (() => {
return {
transformer: {
babelTransformerPath: require.resolve('react-native-svg-transformer/react-native')
},
resolver: {
assetExts: [...assetExts.filter((ext) => ext !== 'svg'), 'bin', 'lottie'],
sourceExts: [...sourceExts, 'svg']
}
};
})();
module.exports = mergeConfig(defaultConfig, metroConfig); |
|
nvm, figured it out: const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;
metroConfig = (() => {
return {
transformer: {
babelTransformerPath: require.resolve('@lingui/metro-transformer/react-native')
},
resolver: {
assetExts: [...assetExts.filter((ext) => ext !== 'svg'), 'bin', 'lottie'],
sourceExts: [...sourceExts, 'svg']
}
};
})();
linguiConfig = (() => {
return {
transformer: {
babelTransformerPath: require.resolve('@lingui/metro-transformer/react-native')
},
resolver: {
sourceExts: [...sourceExts, 'po', 'pot']
}
};
})();
module.exports = mergeConfig(defaultConfig, metroConfig, linguiConfig);I think this should go into the docs, could save some time for the fellow devs. |
|
Hello, I'd recommend this: https://stackoverflow.com/a/57660231/2070942 edit: also see the comment "I tried to use mergeConfig from the metro docs, but since it doesn't deeply merge the objects, the transformers always get overwritten by whichever config you merge in last." |
|
@vonovak thanks for the heads up, I’ve already tried the split version based on file extensions but a few errors showed up. Let me try again and try to fix it since it seems to be the only solution. Will leave a comment with my findings. |
|
@vonovak I ended up with this and works as a charm for both svg imports and lingui po(t).
const upstreamTransformer = require('metro-babel-transformer'); // no need to npm install this pkg, it's already included in metro
const linguiTransformer = require('@lingui/metro-transformer/react-native');
const svgTransformer = require('react-native-svg-transformer');
module.exports.transform = function ({ src, filename, options }) {
if (filename.endsWith('.po' || filename.endsWith('.pot'))) {
return linguiTransformer.transform({ src, filename, options });
} else if (filename.endsWith('.svg')) {
return svgTransformer.transform({ src, filename, options });
} else {
return upstreamTransformer.transform({ src, filename, options });
}
};
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;
metroConfig = (() => {
return {
transformer: {
babelTransformerPath: require.resolve('./customTransformer.js')
},
resolver: {
assetExts: [...assetExts.filter((ext) => ext !== 'svg')],
sourceExts: [...sourceExts, 'svg', 'po', 'pot']
}
};
})();
module.exports = mergeConfig(defaultConfig, metroConfig); |
Description
This adds the equivalent of webpack loader for metro bundler used in react native apps.
I'm targeting the main branch but could easily switch to
nextif desirable.Types of changes
Fixes # (issue)
Checklist