@react-native/[email protected] assigns module.exports inside an ES module, so non-Metro web bundles crash
維護者通常 2 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 74/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- javascript, react-native, webpack
研究方向
issue 指出 @react-native/assets-registry/registry.js 是發生錯誤的檔案,並提供了要替換的具名匯出。閱讀該檔案並檢查其 package 宣告;接著執行 webpack 5 或 Rspack 的 Web bundle,匯入 react-native-svg,並將 react-native 別名為 react-native-web。當 bundle 載入時沒有 ES 模組錯誤,且具名匯入都能解析,就算完成。
由索引模型根據 Issue 內容生成。
描述
Description
@react-native/[email protected] registry.js contains an import and then assigns module.exports. Rspack and webpack 5 treat a file with import as an ES module and throw when it assigns module.exports. The web bundle never evaluates past that file.
react-native-svg imports the named export, so any web bundle that includes it hits this on startup:
import { getAssetByID } from "@react-native/assets-registry/registry";
Metro accepts the mix, so native builds and Metro web builds do not throw.
This is not #48349. That issue was Flow syntax (export type) in the 0.76 file, fixed by #48458, which moved the types into comments and kept module.exports. The 0.88 file is a later rewrite: it imports AssetRegistry from react-native and still assigns module.exports. The published registry.d.ts already declares registerAsset and getAssetByID as named exports.
Published registry.js:
"use strict";
import { AssetRegistry } from "react-native";
/*::
export type {AssetDestPathResolver, PackagerAsset} from 'react-native';
*/
// eslint-disable-next-line @react-native/monorepo/no-commonjs-exports
module.exports = {
registerAsset: AssetRegistry.registerAsset,
getAssetByID: AssetRegistry.getAssetByID,
};
Replacing the assignment with named exports matches the .d.ts and lets the bundle load:
export const registerAsset = AssetRegistry.registerAsset;
export const getAssetByID = AssetRegistry.getAssetByID;
Steps to reproduce
- Install
[email protected],react-native-web, and[email protected]. - Bundle a web entry that imports
react-native-svgwith Rspack or webpack 5, aliasingreact-nativetoreact-native-web. - Load the bundle in a browser.
React Native Version
0.88.1
Affected Platforms
Runtime - Web
Output of npx @react-native-community/cli info
Not a stock React Native app. The failure is in the published @react-native/[email protected] package, loaded through [email protected] by an Rspack web build. react-native is 0.88.1 and react-native-web is 0.21.4.
Stacktrace or Logs
Uncaught (in promise) Error: ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: .../node_modules/@react-native/assets-registry/registry.js
Rspack also reports, before the throw:
ESModulesLinkingWarning: export 'getAssetByID' (imported as 'getAssetByID') was not found in '@react-native/assets-registry/registry' (module has no exports)
from react-native-svg/lib/module/lib/resolveAssetUri.js.
Reproducer
The three accepted reproducer forms all bundle with Metro, and Metro does not throw on this file:
- A pull request that edits
RNTesterPlayground.jsruns inside RNTester, which uses Metro. - An Expo Snack uses Metro.
- The reproducer template is a Community CLI app, which also uses Metro.
The crash happens only when Rspack or webpack 5 bundles @react-native/assets-registry/registry.js. A project that reproduces it is a small web build that installs [email protected], react-native-web, and [email protected], aliases react-native to react-native-web, and bundles an entry that imports react-native-svg. Loading that bundle throws ES Modules may not assign module.exports from registry.js.
There is no public repository for that build yet.
- 主要語言
- C++
- 星號
- 127k
- 分支
- 25.5k
- 平均合併
- 3 天 22 小時
- 30 天內合併 PR
- 258
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
react/react-native 的其他 Issue
-
Needs: Triage :mag:
難度 2/5 1-3 小時 新手友好度 70/100
react/react-native#60000 ·
維護者通常 2 天內回覆
-
`yarn test-ios` exits before building RNTester: `objc-test.sh` launches the WebSocket server script #59433 deleted可能已有人在做 @lazerg 於 5 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 76/100
react/react-native#59939 ·
維護者通常 2 天內回覆
-
iOS Fabric pointer-handler reset leaves the next lone touch non-primary可能已有人在做 @alleneubank 於 7 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 68/100
react/react-native#59919 ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 70/100
react/react-native#59722 ·
維護者通常 2 天內回覆
-
Android: new WebSocket() crashes the app for a URL that OkHttp or java.net.URI rejects可能已有人在做 @sorinc03 於 15 天前認領。 未關閉🌐Networking Needs: Author Feedback Needs: Repro Platform: Android
難度 2/5 1-3 小時 新手友好度 72/100
react/react-native#59704 · 1 則留言 ·
維護者通常 2 天內回覆
查看 react/react-native 的全部 Issue
相似的 Issue
-
ai_p2
難度 2/5 1-3 小時 新手友好度 80/100
ClickHouse/ClickHouse#132519 ·
維護者通常 1 天內回覆
-
patch_advisor_json_integration fails when the build tree is reached through a symbolic link可能已有人在做 @DerKarussellbremser 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 88/100
boykopovar/AnyPS5#22581 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
juce-framework/JUCE#1810 ·
-
難度 1/5 1 小時以內 新手友好度 82/100
petercorke/robotics-toolbox-python#952 ·
維護者通常 1 天內回覆