Skip to content

Conversation

@yvonneyx
Copy link
Collaborator

@yvonneyx yvonneyx commented Nov 4, 2025

在使用 tsc 打包 ESM 时,worker 文件没有被正确定位,Web Worker 处于不可用状态。UMD 用的 webpack 打包,功能正常可用。

分析下来,tsc 仅做类型编译和文件转换,不会处理 import.meta.url 或内联的 Worker 资源路径,因此无法生成独立的 worker bundle。

这里把 ESM 的打包方式也改成 webpack。

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @yvonneyx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决在使用 tsc 打包 ESM 模块时,Web Worker 文件无法被正确识别导致功能不可用的问题。通过将 ESM 的构建流程从 tsc 迁移到 webpack,确保了 import.meta.url 和内联 Worker 资源路径能够被正确处理,从而使 Web Worker 在 ESM 环境下也能正常工作,并与现有的 UMD 构建流程保持一致。

Highlights

  • 构建流程更新: 将 packages/layout/package.json 中 ESM 模块的构建命令从 tsc 切换为 webpack,以解决 worker 文件在 ESM 打包时无法正确识别的问题。
  • 新增 Webpack 配置: 引入了新的 packages/layout/webpack.esm.config.js 文件,专门用于 ESM 模块的 Webpack 打包配置,确保 worker 资源路径的正确处理。
  • TypeScript 配置调整: 在 packages/layout/tsconfig.json 中添加了 "types": [] 配置项,以优化 TypeScript 编译行为。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@yvonneyx yvonneyx changed the title chore: update build process and add ESM webpack configuration chore(build): switch to Webpack for ESM build Nov 4, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @yvonneyx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决 ESM 构建中 Web Worker 文件定位不正确的问题。通过将 ESM 模块的打包方式从 tsc 迁移到 webpack,与现有的 UMD 构建方式保持一致,确保了 import.meta.url 和内联 Worker 资源路径的正确处理,从而使 Web Worker 在 ESM 构建中能够正常工作。

Highlights

  • 构建工具切换: 将 ESM 模块的打包工具从 tsc 切换为 webpack,以解决 tsc 在处理 Web Worker 文件定位时的不足。
  • 解决 Web Worker 问题: 此更改旨在修复在使用 tsc 打包 ESM 时,Web Worker 文件无法被正确识别和定位,导致 Web Worker 不可用的问题。
  • 新增 Webpack 配置: 引入了一个新的 webpack.esm.config.js 配置文件,专门用于 ESM 模块的打包,确保了模块输出、TypeScript 处理和源映射的正确配置。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

你好,感谢你的贡献。这个 PR 将 ESM 的打包方式从 tsc 改为 webpack,以解决 Web Worker 资源路径的问题,这个方向是正确的。

但在实施中存在一些问题,可能会导致构建失败或产物不完整。我主要有以下几点反馈:

  1. CI 脚本移除package.jsonci 脚本被移除了,这可能会影响 CI/CD 流程,需要确认是否是预期行为。
  2. 类型声明文件缺失:新的 build:esm 脚本使用 Webpack,但没有配置生成 TypeScript 声明文件(.d.ts),这将导致库的用户无法获得类型提示。
  3. TypeScript 配置问题tsconfig.json 的修改存在一些问题:
    • "types": [] 的设置会阻止 TypeScript 自动加载 @types 包,可能导致依赖的类型(如 d3-force)丢失。
    • 为了正确处理 import.meta.url,TypeScript 的编译目标 target 需要设置为 es2020 或更高版本,当前的 es6 会导致编译失败。
  4. 构建配置优化:为了让不同的构建目标(如 UMD 和 ESM)可以有不同的编译选项,建议为 ESM 构建创建一个独立的 tsconfig.esm.json 文件。

具体的修改建议请看我的评论。

"build": "npm run clean && run-p build:*",
"ci": "npm run build && npm run test",
"build:esm": "tsc",
"build:esm": "webpack --config webpack.esm.config.js --mode production",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

你好,将 build:esmtsc 切换到 webpack 会导致 TypeScript 的声明文件(.d.ts)无法生成。ts-loader 在 Webpack 中默认不会生成声明文件,即使 tsconfig.json 中设置了 "declaration": true

这将导致 package.json 中的 "types": "lib/index.d.ts" 字段指向一个不存在的文件,从而破坏使用这个包的 TypeScript 项目的类型推断。

为了解决这个问题,你需要一个额外的步骤来生成声明文件。一个常见的做法是修改 build:esm 脚本,在 Webpack 打包之后再运行一次 tsc 来专门生成声明文件。

Suggested change
"build:esm": "webpack --config webpack.esm.config.js --mode production",
"build:esm": "webpack --config webpack.esm.config.js --mode production && tsc --project tsconfig.json --emitDeclarationOnly",

"resolveJsonModule": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],
"types": [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

你好,这里的 "types": [] 配置会阻止 TypeScript 自动加载 node_modules/@types 中的类型定义。这可能会导致项目中依赖的类型(例如 @types/d3-force, @types/dagre 等)无法被识别,从而引发编译错误。建议移除这一行,让 TypeScript 自动发现类型。如果确实需要精确控制,你应该在这里列出所有需要的类型包。

另外,还有一个与新 Webpack 构建相关的重要问题:为了让 Webpack 正确处理 import.meta.url(这是本次修改的核心目的),TypeScript 的编译目标 compilerOptions.target 需要设置为 es2020 或更高版本。当前 tsconfig.json 中的 "target": "es6" 不支持 import.meta 语法,会导致 ts-loader 编译失败。

关于如何修改 target 而不影响其他构建,请参考我对 webpack.esm.config.js 文件的评论,其中建议为 ESM 构建创建一个专用的 tsconfig 文件。

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这个 PR 将 ESM 的打包方式从 tsc 切换到了 Webpack,以解决 Web Worker 文件无法正确定位的问题。这是一个合理的改动,因为 Webpack 在处理资源依赖(如 Worker)方面比 tsc 更强大。新的 webpack.esm.config.js 配置看起来是正确的,它使用了 Webpack 5 的功能来输出 ES 模块。tsconfig.json 中添加 "types": [] 也是一个良好的实践,可以避免全局类型污染。

我发现了一个潜在问题:package.json 中移除了 ci 脚本。这可能会影响持续集成流程,建议确认这是否是预期行为。

"build": "npm run clean && run-p build:*",
"ci": "npm run build && npm run test",
"build:esm": "tsc",
"build:esm": "webpack --config webpack.esm.config.js --mode production",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

在更新 build:esm 脚本的同时,ci 脚本似乎被意外移除了。这个脚本 (npm run build && npm run test) 对于在持续集成环境中验证构建和测试至关重要。如果移除不是故意的,建议通过下面的建议恢复它。

Suggested change
"build:esm": "webpack --config webpack.esm.config.js --mode production",
"ci": "npm run build && npm run test",
"build:esm": "webpack --config webpack.esm.config.js --mode production",

@hustcc
Copy link
Member

hustcc commented Nov 11, 2025

可以试试 utoo,不过 1122 开源,应该有 beta 版本。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

3 participants