Expo导入npm包中'use dom'组件时出现导出相关打包报错的问题求助
Expo导入npm包中'use dom'组件时出现导出相关打包报错的问题求助
大家好,我最近在试Expo新的DOM组件,目前用下来挺喜欢的,但现在想测试把几个DOM组件放到npm包中,再导入到Expo app里用的时候遇到了问题,想求助一下大家。
Setup
测试用npm包
我为了测试从零创建了这个包,依赖配置如下:
{ "expo": "^52.0.27", "react": "^19.0.0", "react-native": "^0.77.0", "react-native-webview": "13.12.5", "styled-components": "^6.1.14" }
我想导出的组件代码如下:
'use dom' const Test = () => { return ( <div> <h1>Hello</h1> </div> ) } export default Test
通过项目根目录的index.js文件导出,只有一行代码:
export {default as Test} from './components/Test/Test.native'
我用Babel编译这个包,本地测试用的是yalc来模拟npm包的安装使用。
Expo应用端
在我的Expo应用中,我这样使用这个包的组件:
import { Test } from 'my-test-package' export default function ProfileScreen() { return ( <Test /> ) }
Issue
当Expo尝试渲染使用了这个导入的DOM组件的页面时,抛出了如下错误:
iOS Bundling failed: node_modules/my-test-package/dist/components/Test/Test.native.js: <...>/my-expo-app/node_modules/my-test-package/dist/components/Test/Test.native.js: The "use dom" directive requires a default export to be present in the file.
我本来以为是导出/导入的配置哪里搞错了,但我在Expo应用本地做了完全一样的配置(也就是通过相同结构的index文件导出Test.native.js组件,所有文件都放在同一个Expo项目里),那样是完全可以正常运行的。
我在想会不会是Babel编译的问题,或者是编译后自动加上的'use strict'指令影响的?有没有大佬能给点思路?
提前谢谢大家了!
备注:内容来源于stack exchange,提问作者Pinkney
相关产品推荐
相关产品推荐

