Skip to content

关于在生产构建中使用Top-Level Await的问题 #19670

Open
@Duo-Huang

Description

@Duo-Huang

Describe the bug

问题描述:
使用react-swc模版创建的项目,vite build target调整为es2022以支持Top-Level Await特性,然后在模块中通过一下方式使用

import _ from 'lodash'

const useStore = {
    getState: () => {
        return {
            count: 0,
        }
    },
}



console.log('loading lazyModule', _)
const module = await import('@/utils/lazyModule');

console.log('module value is :', module.default)

export default useStore 

本地运行没有任何问题,但是build之后preview运行代码会在await这一行直接静默结束,network可以看到chunkfile已经被懒加载成功,但是await永远不会resolve也不会reject没有任何报错,在src/utils/lazyModule.ts第一行打断点也不会执行。

这里有一个复现的代码库你可以拉取到本地测试: [email protected]:Duo-Huang/test-tla.git
代码是一个最小可复现的demo, 主要代码文件src/store/index.ts, src/utils/lazyModule.ts
代码中有详细的操作过程和现象描述

Reproduction

https://github.com/Duo-Huang/test-tla

Steps to reproduce

  1. clone my demo code: git clone [email protected]:Duo-Huang/test-tla.git
  2. pnpm i
  3. pnpm build
  4. pnpm preview
  5. bug is reproduced

System Info

System:
    OS: macOS 15.3.2
    CPU: (10) arm64 Apple M1 Pro
    Memory: 70.55 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - ~/.asdf/installs/nodejs/v22.14.0/bin/node
    Yarn: 1.22.22 - ~/.asdf/installs/nodejs/v22.14.0/bin/yarn
    npm: 10.9.2 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 10.4.1 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 134.0.6998.89
    Safari: 18.3.1
  npmPackages:
    @vitejs/plugin-react-swc: ^3.8.0 => 3.8.0 
    vite: ^6.2.2 => 6.2.2

Used Package Manager

pnpm

Logs

你可以观察chrome console中的log, 显示异常的执行, 没有报错

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug: upstreamBug in a dependency of Vitep3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions