使用 Zustand 5.0.0 以上版本开发模式下打包报错 taro.react_production_min.create is not a function
相关平台
微信小程序
小程序基础库: 3.7.7 使用框架: React
复现步骤
使用 zustand 构建 store 后正常页面内引入,使用 dev:weapp 打包出错:taro.react_production_min.create is not a function,但是使用 build:weapp 则不会出错,zustand 版本5.0.0 以上可复现错误,旧版本正常
期望结果
正常使用 zustand
实际结果
报错 taro.react_production_min.create is not a function
环境信息
Taro CLI 4.0.9 environment info:
System:
OS: macOS 12.7
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 20.18.0 - ~/.nvm/versions/node/v20.18.0/bin/node
npm: 10.8.2 - ~/.nvm/versions/node/v20.18.0/bin/npm
npmPackages:
@tarojs/cli: 4.0.9 => 4.0.9
@tarojs/components: 4.0.9 => 4.0.9
@tarojs/helper: 4.0.9 => 4.0.9
@tarojs/plugin-framework-react: 4.0.9 => 4.0.9
@tarojs/plugin-platform-alipay: 4.0.9 => 4.0.9
@tarojs/plugin-platform-h5: 4.0.9 => 4.0.9
@tarojs/plugin-platform-harmony-hybrid: 4.0.9 => 4.0.9
@tarojs/plugin-platform-jd: 4.0.9 => 4.0.9
@tarojs/plugin-platform-qq: 4.0.9 => 4.0.9
@tarojs/plugin-platform-swan: 4.0.9 => 4.0.9
@tarojs/plugin-platform-tt: 4.0.9 => 4.0.9
@tarojs/plugin-platform-weapp: 4.0.9 => 4.0.9
@tarojs/react: 4.0.9 => 4.0.9
@tarojs/runtime: 4.0.9 => 4.0.9
@tarojs/shared: 4.0.9 => 4.0.9
@tarojs/taro: 4.0.9 => 4.0.9
@tarojs/vite-runner: 4.0.9 => 4.0.9
babel-preset-taro: 4.0.9 => 4.0.9
eslint-config-taro: 4.0.9 => 4.0.9
react: ^18.0.0 => 18.3.1
可以尝试关闭一下prebundle
我这边复现了这个问题,并且通过在config/dev.ts中添加如下设置临时解决了该问题
参考原理:https://docs.taro.zone/docs/config-detail#minidebugreact
看了下配置,是使用的vite,webpack5下没有复现该问题 CC @tutuxxx
一样的问题,debugReact: true 可以暂时解决。
👽 Taro v4.1.4
Taro CLI 4.1.4 environment info:
System:
OS: macOS 15.3.2
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.16.0 - ~/.local/state/fnm_multishells/9488_1753670871795/bin/node
Yarn: 1.22.22 - ~/.npm_global/bin/yarn
npm: 10.9.2 - ~/.local/state/fnm_multishells/9488_1753670871795/bin/npm
npmPackages:
@tarojs/cli: 4.1.4 => 4.1.4
@tarojs/components: 4.1.4 => 4.1.4
@tarojs/helper: 4.1.4 => 4.1.4
@tarojs/plugin-framework-react: 4.1.4 => 4.1.4
@tarojs/plugin-platform-alipay: 4.1.4 => 4.1.4
@tarojs/plugin-platform-h5: 4.1.4 => 4.1.4
@tarojs/plugin-platform-harmony-hybrid: 4.1.4 => 4.1.4
@tarojs/plugin-platform-jd: 4.1.4 => 4.1.4
@tarojs/plugin-platform-qq: 4.1.4 => 4.1.4
@tarojs/plugin-platform-swan: 4.1.4 => 4.1.4
@tarojs/plugin-platform-tt: 4.1.4 => 4.1.4
@tarojs/plugin-platform-weapp: 4.1.4 => 4.1.4
@tarojs/react: 4.1.4 => 4.1.4
@tarojs/runtime: 4.1.4 => 4.1.4
@tarojs/shared: 4.1.4 => 4.1.4
@tarojs/taro: 4.1.4 => 4.1.4
@tarojs/vite-runner: 4.1.4 => 4.1.4
babel-preset-taro: 4.1.4 => 4.1.4
react: ^18.0.0 => 18.3.1
npmGlobalPackages:
typescript: 5.7.2
这个问题的原因是官方的taro内部实现有BUG, 就看官方是否愿意恢复了。
packages/taro-framework-react/src/vite.mini.ts 这个文件L52行,会对出现import from react字样的地方查找替换react的模块,而且是prod mode下,这也是为什么开了debug不会出现这个问题。
而恰好,zustand库中,有这个同名的文件/模块,导致替换错误。zustand/src/react.ts zustand中同名模块
查找替换的逻辑需要更严谨,而不是通用的react字样。
我应该at谁合适?
- zustand: ^5.0.8
- @tarojs/taro: 4.1.8
中编译小程序启动时也出现了该问题,按照上述所说的在
config/dev.ts中添加debugReact: true可以解决(注意修改了后需要重新运行才会生效)