react-native-svg icon indicating copy to clipboard operation
react-native-svg copied to clipboard

Possible Breaking Change on Web in 15.5.0

Open jspizziri opened this issue 2 years ago • 13 comments

Description

This commit which was released on v15.5.0 introduces a dependency on @react-native/assets-registry which previously didn't exist.

@react-native/assets-registry is a transitive dependency of react-native but is NOT a transitive dependency of react-native-web. Therefore, in web contexts, an error is thrown due to the missing dependency.

Steps to reproduce

Setup a bare react-native-web project that depends on [email protected] and does NOT install react-native. This seems to only happen in non-expo contexts.

Snack or a link to a repository

working on it

SVG version

15.5.0

React Native version

0.74.5

Platforms

Web

JavaScript runtime

None

Workflow

None

Architecture

None

Build type

None

Device

None

Device model

No response

Acknowledgements

Yes

jspizziri avatar Aug 20 '24 19:08 jspizziri

Can reproduce this issue on RNSVG 15.6.0

furyswordxd avatar Aug 23 '24 00:08 furyswordxd

Can reproduce.

I'm using this in a nextjs project that worked before. Now its throwing errors about reading flowtypes.

Example here: https://github.com/shawnmclean/sovoli/actions/runs/10548468653/job/29222414744#step:7:64

shawnmclean avatar Aug 25 '24 18:08 shawnmclean

I've gotta say, seems like we could have a test added to make sure web support isn't broken on release right?

jonstuebe avatar Aug 27 '24 17:08 jonstuebe

Hey! šŸ‘‹

The issue doesn't seem to contain a minimal reproduction.

Could you provide a snack or a link to a GitHub repository under your username that reproduces the problem?

github-actions[bot] avatar Aug 27 '24 17:08 github-actions[bot]

@jonstuebe , I'm sure the maintainers would welcome a PR from you adding such a test šŸ˜„

jspizziri avatar Aug 27 '24 18:08 jspizziri

I've gotta say, seems like we could have a test added to make sure web support isn't broken on release right?

It seems like the same problem as here. The best solution for that problem now is to:

Install this dependency. If you are using Next.js, you need to transpile @react-native/assets-registry/registry. I guess you can configure it here.

bohdanprog avatar Aug 28 '24 13:08 bohdanprog

@shawnmclean If you have any questions let us know ;)

bohdanprog avatar Aug 28 '24 17:08 bohdanprog

FYI: I tried transpiling via next.js, but that doesn't seem to work. I think it's because it's a flow file with a .js extension and not typescript.

I ended up patching registry.js instead.

johnf avatar Sep 08 '24 22:09 johnf

FYI: I tried transpiling via next.js, but that doesn't seem to work. I think it's because it's a flow file with a .js extension and not typescript.

I ended up patching registry.js instead.

Can you share an example of how you tried transpiling via next.js?

bohdanprog avatar Sep 09 '24 06:09 bohdanprog

I added the package to transpilePackages in next.config.mjs (I already have other entire in there so I know it's working). I tried a couple of options

  • @react-native/assets-registry
  • @react-native/assets-registry/registry
  • @react-native/assets-registry/registry.js

None of these made any difference.

johnf avatar Sep 09 '24 06:09 johnf

Hello @johnf, sorry it took a while. I found the solution to your problem. here is my configuration for the next.js project.

/** @type {import('next').NextConfig} */

const path = require("path");

const nextConfig = {
  experimental: {
    turbo: {
      resolveAlias: {
        "react-native": "react-native-web",
      },
      resolveExtensions: [
        ".web.js",
        ".web.jsx",
        ".web.ts",
        ".web.tsx",
        ".mdx",
        ".tsx",
        ".ts",
        ".jsx",
        ".js",
        ".mjs",
        ".json",
      ],
    },
  },
  webpack: (config) => {
    config.module.rules.push({
      test: /\.(js|jsx|ts|tsx)$/,
      include: [
        path.resolve(__dirname, "src"),
        path.resolve(
          __dirname,
          "node_modules/@react-native/assets-registry/registry"
        ),
      ],
      use: {
        loader: "babel-loader",
        options: {
          cacheDirectory: true,
          presets: ["module:metro-react-native-babel-preset"],
          plugins: ["react-native-web"],
        },
      },
    });
    config.resolve.alias = {
      ...(config.resolve.alias || {}),
      // Transform all direct `react-native` imports to `react-native-web`
      "react-native$": "react-native-web",
    };
    config.resolve.extensions = [
      ".web.js",
      ".web.jsx",
      ".web.ts",
      ".web.tsx",
      ...config.resolve.extensions,
    ];
    return config;
  },
};

module.exports = nextConfig;

bohdanprog avatar Sep 15 '24 14:09 bohdanprog

@bohdanprog Thank you for posting your fix. However, even with the next.config.js you recommended, I'm still running into this issue. I was wondering if you could share the versions of all of the relevant packages you are using, like react-native-web, react-native, nextjs, etc.

Thank you!

stevenwchien avatar Jan 13 '25 17:01 stevenwchien

I’m having this issue on web since upgrading to RN 0.87. The Webpack fix:

    // RN 0.87 dropped the @react-native/assets-registry package. react-native-svg
    // still imports it; on web the same registry lives in react-native-web.
    webpackConfig.resolve.alias["@react-native/assets-registry/registry"] =
      "react-native-web/dist/modules/AssetRegistry";

Freddy03h avatar Oct 07 '26 10:10 Freddy03h