Skip to content

fix: 修复支付宝小程序开发预览报错问题 - #16715

Merged
koppthe merged 3 commits into
mainfrom
fix/issues
Oct 16, 2024
Merged

fix: 修复支付宝小程序开发预览报错问题#16715
koppthe merged 3 commits into
mainfrom
fix/issues

Conversation

@koppthe

@koppthe koppthe commented Oct 15, 2024

Copy link
Copy Markdown
Contributor

这个 PR 做了什么? (简要描述所做更改)

问题描述

Taro 4.x 构建支付宝小程序,在开发工具上预览会报如下错误:

For the selected environment is no default script chunk format available:
JSONP Array push can be chosen when 'document' or 'importScripts' is available.
CommonJs exports can be chosen when 'require' or node builtins are available.
Make sure that your 'browserslist' includes only platforms that support these features or select an appropriate 'target' to allow selecting a chunk format by default. Alternatively specify the 'output.chunkFormat' directly.

Dev server exited unexpectedly with code 1

Please try to restart by clicking 'Restart'

问题原因

Taro 4.x 后,更新了 browserslist 的配置,默认仅支持 ES6 的导出
e87d6ab#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R196

解决办法

问题一:

开发工具预览报错,无法启动:

Make sure that your 'browserslist' includes only platforms that support these features or select an appropriate 'target' to allow selecting a chunk format by default. Alternatively specify the 'output.chunkFormat' directly.

解决办法:
由于支付宝小程序开发工具会读取项目中的 browserslist 配置,以提前暴露问题,所以为了禁用这个逻辑,可以在 dist 目录中增加 .browserslistrc 配置,并设置内容为:

defaults and fully supports es6-module

问题二:

Taro 在 dev 模式下,构建支付宝小程序产出的是 ES6 的代码,用户在使用支付宝小程序开发者工具时,默认不会转换 node_modules 的代码,会产生以下报错:

TypeError: Constructor Map requires 'new'

解决办法:
开启开发者工具的 transpile 配置,即在 mini.project.json 或者 project.alipay.json 配置中添加如下配置:

{
  "format": 2,
  "miniprogramRoot": "dist",
  "compileOptions": {
    "transpile": {}
  }
}

文档:https://opendocs.alipay.com/mini/03dbc3?pathHash=e876dc50#transpile

这个 PR 是什么类型? (至少选择一个)

  • 错误修复(Bugfix) issue: fix #
  • 新功能(Feature)
  • 代码重构(Refactor)
  • TypeScript 类型定义修改(Typings)
  • 文档修改(Docs)
  • 代码风格更新(Code style update)
  • 其他,请描述(Other, please describe):

这个 PR 涉及以下平台:

  • 所有小程序
  • 微信小程序
  • 支付宝小程序
  • 百度小程序
  • 字节跳动小程序
  • QQ 轻应用
  • 京东小程序
  • 快应用平台(QuickApp)
  • Web 平台(H5)
  • 移动端(React-Native)
  • 鸿蒙(harmony)

* main:
  fix: 修复solid的onShow,onHide不生效的bug (#16569)
  fix(taro-cli): 修正tsconfig-paths-webpack-plugin插件的导入条件
  fix(taro-cli): 使TS条件下生成的模板,import与export间保持一行空隙,以避免ESLint报错
@codecov

codecov Bot commented Oct 15, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 58.51%. Comparing base (5148dd6) to head (f622c1f).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##             main   #16715    +/-   ##
========================================
  Coverage   58.51%   58.51%            
========================================
  Files         466      466            
  Lines       27115    27115            
  Branches     5894     5712   -182     
========================================
  Hits        15866    15866            
+ Misses       9895     9801    -94     
- Partials     1354     1448    +94     
Flag Coverage Δ
taro-cli 73.67% <ø> (ø)
taro-runtime 59.83% <ø> (ø)
taro-web 52.70% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

see 32 files with indirect coverage changes

@koppthe
koppthe merged commit e1aa8a8 into main Oct 16, 2024
@koppthe
koppthe deleted the fix/issues branch October 16, 2024 02:16
@ZEJIA-LIU ZEJIA-LIU added this to the 4.0.7 milestone Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants