fix(console): emit maplibre's sibling worker beside its chunk (#3297) - #3304
Merged
Conversation
maplibre-gl resolves its worker as a sibling of its own script URL
(`new URL('./maplibre-gl-worker.mjs', import.meta.url)`) instead of
importing it, so no bundler can see the edge. Vite bundled maplibre into
`assets/maplibre-gl-<hash>.js` and emitted nothing beside it, leaving
`/_console/assets/maplibre-gl-worker.mjs` a 404 in every deployment.
Copy the worker into `assetsDir` at build time — plus the sibling module
the worker itself imports (`maplibre-gl-shared.mjs`), without which the
404 would only move one hop deeper — and serve the same files in dev,
where `optimizeDeps` puts the identical hole under `.vite/deps/`.
The copy is guarded so it cannot drift silently, in the spirit of
`scripts/build-console.sh`'s CONSOLE_BUNDLE_CANARY: the build fails if
maplibre's dist no longer holds the files, if the bundle asks for a
worker name the build does not emit, or if the emitted files are missing
from disk after the write.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NVPjPzmmAJ2Ngtvgg5MSRa
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Contributor
✅ Console Performance Budget
📦 Bundle Size Report
Size Limits
|
… plugin The comment described the earlier `this.resolve` approach. The actual reason is plugin ordering: once Vite's core resolver answers a specifier, later plugins' resolveId hooks never see it. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVPjPzmmAJ2Ngtvgg5MSRa
Contributor
✅ Console Performance Budget
📦 Bundle Size Report
Size Limits
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3297
问题
maplibre-gl把 worker 当作自身脚本 URL 的兄弟文件来解析,而不是 import 它,所以打包器看不到这条边(issue 中已给出构建产物摘录):vite 把 maplibre 打成
assets/maplibre-gl-[hash].js后旁边什么都不产出,于是运行时算出的/_console/assets/maplibre-gl-worker.mjs在所有部署里都是 404。改动
新增
scripts/vite-maplibre-worker.ts(放在scripts/里,与既有的vite-crypto-stub.ts同一处),在
apps/console/vite.config.ts里接入。1. issue 只说了一半 —— worker 自己还有一个兄弟依赖。 maplibre 的 worker 是 module worker,
第一行就
import "./maplibre-gl-shared.mjs"(479 KB)。只拷 worker 只会把 404 往里挪一跳,所以本 PR 拷的是 worker 的兄弟闭包:读 worker 源码,跟着它的相对 import 递归收集,
一并 emit 进
assetsDir。maplibre 以后拆出更多兄弟文件也会自动跟上。2. 落点。 文件名不可哈希、不可换目录 —— 运行时算的是字面量兄弟路径。
源文件目录取自 rollup 本次真的打进去的那个模块 id(
transform钩子),所以拷出来的 worker 与主线程代码天然同版本,不存在二次解析选到另一份 hoist 副本的可能。
3. 构建断言(参照
scripts/build-console.sh的CONSOLE_BUNDLE_CANARY姿势)。三道,全部让构建红而不是运行时 404:
dist/assetsdev 模式(本单要求核对的第三点)
结论:dev 原本也是坏的,同一机制、隔壁目录,已一并修。
optimizeDeps会把 maplibre 预打包到node_modules/.vite/deps/maplibre-gl-[hash].js,import.meta.url未被改写,于是兄弟 URL 变成/node_modules/.vite/deps/maplibre-gl-worker.mjs—— 同样不存在。实测(修复前,console dev server):
因此插件同时挂了一个 dev 中间件,把这些请求用 maplibre 真实 dist 里的文件应答。
它只会应答确实存在于 maplibre dist 里的文件名,其余一律
next()。修复后:验证
构建产物(
pnpm --filter @object-ui/console build,绿):按 maplibre 自己的逻辑复算 URL(从 preview 实际下发的 chunk 里正则取出那个三目表达式再算一遍):
issue 里那条一模一样的 URL(
vite preview --base /_console/):sabotage(三道断言逐条验红,均 EXIT=1):
maplibre dist file 'maplibre-gl-worker-v7.mjs' does not exist (required by maplibre's runtime worker URL).Built chunks request maplibre worker(s) this build does not emit: maplibre-gl-worker.mjs. Emitted: maplibre-gl-shared.mjs.maplibre worker asset(s) missing from the build output: maplibre-gl-worker.mjs, maplibre-gl-shared.mjs.测试 / 类型 / lint:
新增的 20 条单测钉住了漂移审计的各个分支,并且会读真实安装的 maplibre:升级后
worker 改名或不再是兄弟约定,
pnpm test直接红,不用等到浏览器里才发现。demotiles.maplibre.org打不通),容器内也没有可用浏览器(playwright 装了但没下载 browser),所以无法真的把地图画出来。
验证到的是「产物存在 + 运行时算出的 URL 200 + MIME 正确 + worker 自身依赖也 200」。
还是彻底不出图。有外网环境时值得复看一眼。不过这不影响本 PR 的正确性 ——
第一方资产在自家产物里缺失,无论如何都该补。
changeset
未加,依据两条:AGENTS.md「纯 bug 修复不需要 changeset」;以及仓库现行做法 ——
近期
fix(console):三个 PR 均未附 changeset,而改动发布库包的fix(components)/fix(fields)/fix(app-shell)都附了。console 进部署走的是 framework 侧.objectui-shapin +scripts/build-console.sh重新构建,不依赖 npm 版本号。若维护者希望本次走版本发布,我再补一个(按仓规标
minor,绝不标major)。Generated by Claude Code