vite icon indicating copy to clipboard operation
vite copied to clipboard

fix: 修复现代浏览器不支持catch optional

Open ykant opened this issue 1 year ago • 5 comments

Description

支持module,但是不支持 catch optional的低版本浏览器会导致程序不能正常运行

在浏览器判断处新增catch optional场景

Additional context


What is the purpose of this pull request?

  • [x] Bug fix
  • [ ] New Feature
  • [ ] Documentation update
  • [ ] Other

Before submitting the PR, please make sure you do the following

  • [x] Read the Contributing Guidelines.
  • [x] Read the Pull Request Guidelines and follow the Commit Convention.
  • [x] Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • [ ] Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • [x] Ideally, include relevant tests that fail without this PR but pass with it.

ykant avatar Sep 09 '22 11:09 ykant

I don't think this is needed. Optional catch binding is supported by all default target browsers.

https://github.com/vitejs/vite/blob/e66cf69cc27b8a8900f14af746ed6925c8af8fdc/packages/vite/src/node/constants.ts#L17-L23 https://caniuse.com/mdn-javascript_statements_try_catch_optional_catch_binding

sapphi-red avatar Sep 09 '22 11:09 sapphi-red

I fixed the problem in my project just a few hours ago(Android 9 Webview), but my solution is ugly. `

`

ykant avatar Sep 09 '22 12:09 ykant

OK. I know understand the problem. Our default target browsers are not the same with the browsers supporting ESM + dynamic import + import.meta. This is different from what we document.

Current default target browsers (A): es2020, edge88, firefox78, chrome87, safari13 browsers supporting ESM + dynamic import + import.meta (B): es2020, edge79, firefox67, chrome64, safari11.1

So features supported in A but not in B will be used in modern chunks and will break users using B. Optional catch binding is supported from Chrome 66 and that is one of the affected features.

It seems the difference comes from https://github.com/vitejs/vite/pull/2976.

sapphi-red avatar Sep 09 '22 14:09 sapphi-red

I think there's three options here.

  1. Change build.target to B
  2. Change build.target to B by plugin-legacy
  3. Check every feature supported between A and B, and detect whether it is supported in detectModernBrowserCode

sapphi-red avatar Sep 09 '22 14:09 sapphi-red

Thank you for the answer :)

ykant avatar Sep 10 '22 02:09 ykant