Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支付宝小程序test环境构建产物作为子包运行报错TypeError: Constructor Map requires 'new' #12005

Closed
Dewyzee opened this issue Jun 28, 2022 · 2 comments
Labels
F-react Framework - React T-alipay Target - 编译到支付宝小程序 V-3 Version - 3.x walk-around 绕过问题

Comments

@Dewyzee
Copy link

Dewyzee commented Jun 28, 2022

相关平台

支付宝小程序

小程序基础库: 2.7.15
使用框架: React

复现步骤

1.使用以下命令构建cross-env NODE_ENV=test taro build --type alipay
2.获取构建产物打开调试工具
3.控制台报错
TypeError: Constructor Map requires 'new'
at ze.Map ()
at new ze (taro.js:2)
at Object. (taro.js:2)
at Object._ (taro.js:2)
at l (runtime.js:1)
at Module._ (app.js:2)
at l (runtime.js:1)
at t (runtime.js:1)
at Array.r [as push] (runtime.js:1)
at Object.GfhN (app.js:2)

期望结果

能正常运行

实际结果

taro运行时代码报错,导致小程序页面注册失败,无法打开页面

环境信息

 Taro CLI 3.4.3 environment info:
    System:
      OS: macOS 11.4
      Shell: 5.8 - /bin/zsh
    Binaries:
      Node: 12.22.12 - ~/.nvm/versions/node/v12.22.12/bin/node
      Yarn: 1.22.18 - ~/.nvm/versions/node/v12.22.12/bin/yarn
      npm: 6.14.16 - ~/.nvm/versions/node/v12.22.12/bin/npm
    npmPackages:
      @tarojs/cli: 3.4.3 => 3.4.3 
      @tarojs/components: 3.4.3 => 3.4.3 
      @tarojs/mini-runner: 3.4.3 => 3.4.3 
      @tarojs/react: 3.4.3 => 3.4.3 
      @tarojs/runtime: 3.4.3 => 3.4.3 
      @tarojs/taro: 3.4.3 => 3.4.3 
      @tarojs/webpack-runner: 3.4.3 => 3.4.3 
      babel-preset-taro: 3.1.4 => 3.1.4 
      eslint-config-taro: 3.1.4 => 3.1.4 
      react: ^17.0.0 => 17.0.2 

补充信息

初步排查,在Taro运行时代码里有一段event-source的逻辑,这里对Map进行了继承扩展并创建实例暴露出去。在测试环境打包构建压缩的时候,这里的实例化的动作括号丢失了,导致最终产物中这里没有进行实例化创建
// 源码
class EventSource extends Map {
removeNode (child: TaroNode) {
const { sid, uid } = child
this.delete(sid)
if (uid !== sid && uid) this.delete(uid)
}

removeNodeTree (child: TaroNode) {
this.removeNode(child)
const { childNodes } = child
childNodes.forEach(node => this.removeNodeTree(node))
}
}

export const eventSource: IEventSource = new EventSource()
// 构建产物
class ze extends Map {
removeNode(e) {
const {sid: t, uid: n} = e;
this.delete(t),
n !== t && n && this.delete(n)
}
removeNodeTree(e) {
this.removeNode(e);
const {childNodes: t} = e;
t.forEach(e=>this.removeNodeTree(e))
}
}
const He = new ze;

@taro-bot2 taro-bot2 bot added F-react Framework - React T-alipay Target - 编译到支付宝小程序 V-3 Version - 3.x labels Jun 28, 2022
@wotermelon
Copy link

这个问题在 3.5.10 中依然存在,一模一样,复现也是一样

小程序基础库: 2.7.15
使用框架: React

image

@Chen-jj Chen-jj added the walk-around 绕过问题 label Mar 28, 2023
@Chen-jj
Copy link
Contributor

Chen-jj commented Mar 28, 2023

@Dewyzee @wotermelon Taro 在 NODE_ENV === 'test' 的情况下会做一些特别的事情,这的确不规范,但随着项目迭代,对 test 环境的判断语句已经散落多处,不太好改了。因此建议可以换一个环境变量名来绕过的。

针对本 issue,就是在 test 环境时把 @babel/preset-env 的 targets 改写为最新的语法,所以支付宝小程序中会因为编译产物中还留有 es6 的语法而报错。

} else if (ignoreBrowserslistConfig) {
targets = { node: 'current' }

@Chen-jj Chen-jj closed this as completed Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React T-alipay Target - 编译到支付宝小程序 V-3 Version - 3.x walk-around 绕过问题
Projects
None yet
Development

No branches or pull requests

3 participants