【问题标题】:expo monorepo with expo-yarn-workspaces: how to customize the metro.config.js file带有 expo-yarn-workspaces 的 expo monorepo:如何自定义 metro.config.js 文件
【发布时间】:2020-11-12 11:04:18
【问题描述】:

我想用monorepo yarn workspaces 设置一个 react-native expo(空白打字稿模板)项目。我对谷歌的研究指出我使用这个库expo-yarn-workspaces。在他们网站上的教程的一个步骤中,提到您必须“创建”metro.config.js 并添加以下行:

const { createMetroConfiguration } = require('expo-yarn-workspaces');
module.exports = createMetroConfiguration(__dirname);

一般来说,在我之前的 react-native expo 项目中,我曾经在我的 metro.config.js 中添加一些自定义配置,以将我的应用程序导入 svg 文件作为 react components。这是我的自定义配置:

// metro.config.js

const { getDefaultConfig } = require("metro-config");
module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("react-native-svg-transformer")
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== "svg"),
      sourceExts: [...sourceExts, "svg"]
    }
  };
})();

我的问题是:如何进一步自定义metro.config.js 文件与自定义配置(如上)?

【问题讨论】:

    标签: expo monorepo expo-yarn-workspaces


    【解决方案1】:

    您可以将 expo-yarn-workspacesmetro-config(或 @expo/metro-config,如果您的 expo SDK 版本是 v40.0.0 或更高版本)组合在一起来配置 React Native 打包器。关键是 expo-yarn-workspaces 及其解析器的配置不会导出 sourceExts,您应该使用 metro-config 中的另一个配置。

    Expo SDK v40.0.0 或更新版本:

    // metro.config.js
    const { createMetroConfiguration } = require('expo-yarn-workspaces');
    const { getDefaultConfig } = require('@expo/metro-config');
    
    const configuration = createMetroConfiguration(__dirname);
    
    module.exports = (async () => {
      const {
        resolver: { sourceExts }
      } = await getDefaultConfig();
    
      return {
        transformer: {
          babelTransformerPath: require.resolve("react-native-svg-transformer")
        },
        resolver: {
          ...configuration.resolver,
          assetExts: configuration.resolver.assetExts.filter(ext => ext !== 'svg'),
          sourceExts: [...sourceExts, 'svg'],
          // NOTE: using native entrypoint because bug in metro https://github.com/facebook/metro/issues/485
          resolverMainFields: ['native', 'module', 'browser', 'main'],
        }
      };
    })();
    

    您使用 Expo SDK

    // metro.config.js
    const { createMetroConfiguration } = require('expo-yarn-workspaces');
    const { getDefaultConfig } = require("metro-config");
    
    const configuration = createMetroConfiguration(__dirname);
    
    module.exports = (async () => {
      const {
        resolver: { sourceExts }
      } = await getDefaultConfig();
    
      return {
        transformer: {
          babelTransformerPath: require.resolve("react-native-svg-transformer")
        },
        resolver: {
          ...configuration.resolver,
          assetExts: configuration.resolver.assetExts.filter(ext => ext !== 'svg'),
          sourceExts: [...sourceExts, 'svg'],
          // NOTE: using native entrypoint because bug in metro https://github.com/facebook/metro/issues/485
          resolverMainFields: ['native', 'module', 'browser', 'main'],
        }
      };
    })();
    

    【讨论】:

    • 感谢您的回复。我现在正在做一个不同的项目,当我回到这个项目时会检查你的回复。
    • 如何根据工作空间包的变化重启expo?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多