diff --git a/src/lib/web-shims/codegenNativeComponent.js b/src/lib/web-shims/codegenNativeComponent.js new file mode 100644 index 0000000000..4c50f5cd87 --- /dev/null +++ b/src/lib/web-shims/codegenNativeComponent.js @@ -0,0 +1,17 @@ +/* + * Web stub for `react-native/Libraries/Utilities/codegenNativeComponent`. + * + * Deep `react-native/*` imports bypass the `react-native$ -> react-native-web` + * alias and drag RN core into the web bundle, where its platform-extension + * modules (e.g. Libraries/Utilities/Platform.js) resolve to themselves and + * throw a TDZ error at startup. Packages that import codegen specs + * unconditionally (react-native-uitextview) guard the native component behind + * a `Platform.OS === 'ios'` check, so nothing ever renders this on web. + */ +module.exports = function codegenNativeComponent(componentName) { + return function UnimplementedNativeComponent() { + throw new Error( + `Native component "${componentName}" was rendered on web. It has no web implementation.`, + ) + } +} diff --git a/webpack.config.js b/webpack.config.js index 889a2cb83f..69a7adaab1 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -62,6 +62,16 @@ module.exports = async function (env, argv) { } config = withAlias(config, { 'react-native$': 'react-native-web', + /* + * Codegen specs import this via a deep `react-native/*` path, which skips + * the exact-match alias above and pulls RN core into the web bundle. RN + * core's platform-extension modules have no `.web` variant, so they + * resolve to themselves and blow up with a TDZ error on startup. + */ + 'react-native/Libraries/Utilities/codegenNativeComponent$': path.join( + __dirname, + 'src/lib/web-shims/codegenNativeComponent.js', + ), 'react-native-webview': 'react-native-web-webview', 'react-native-gesture-handler': false, // RNGH should not be used on web, so let's cause a build error if it sneaks in '@sentry-internal/replay': false, // not used, ~300kb of dead weight