Subpath bundles duplicate PostContext, and the root import can't be tree-shaken
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 需要澄清
- 活跃度
- 活跃
- 技术栈
- react, rollup, typescript, webpack
- 领域
- build-system
调研方向
The build is configured in webpack.config.js and package.json; read those to see how entries are bundled and why no sideEffects field exists. Inspect dist/components/post-context/index.js and dist/components/post-title/index.js to confirm each creates its own context. Done means either an ESM build with sideEffects added, or cross-entry imports externalized so React context is shared across subpaths.
由索引模型根据 Issue 内容生成。
描述
Follow-up to #97. The subpath exports it added help a lot, but two problems remain in 1.23.0. Together they force a choice between bundle size and correct behaviour.
1. The package root can't be tree-shaken
dist/index.js is one CommonJS webpack bundle, and package.json has no sideEffects field. A consumer that imports a single component from @10up/block-components gets the whole library and all of its dependencies (@tanstack/react-query, @dnd-kit/*, react-window, @leeoniya/ufuzzy, @emotion/* and so on).
On a large client site with about 60 custom blocks, each block's editor script that imported anything from the root was about 125 KB gzipped larger. Switching those imports to @10up/block-components/components/* and /hooks/* cut the editor JS from 4.8 MB to 2.5 MB gzipped, and most block scripts dropped from about 130 KB to 1–16 KB.
2. Subpath bundles each carry their own copy of shared internals
webpack.config.js builds every api/, components/, hooks/ and stores/ folder as a separate entry, and only @wordpress/* is externalized. Shared modules are therefore inlined into every entry that uses them. React context is where this breaks things. In dist/ (1.23.0):
$ grep -c createContext dist/components/post-context/index.js dist/components/post-title/index.js
dist/components/post-context/index.js:1
dist/components/post-title/index.js:1
post-title, post-featured-image, post-author, post-meta, hooks/use-post and hooks/use-primary-term each create their own copy of the post context. So this:
import { PostContext } from '@10up/block-components/components/post-context';
import { PostTitle } from '@10up/block-components/components/post-title';
<PostContext postId={ 123 } postType="post">
<PostTitle />
</PostContext>
wires PostTitle to a different context object than the provider. It never sees postId: 123, so it falls back to the context default. To stay correct, anything using PostContext has to import the whole family from the root, which brings back the full bundle from problem 1. In our case that meant 9 files (and their blocks) still ship the full library.
The icon store is fine: every copy runs select( 'tenup/icons' ) || register( store ), so all copies share one store by name. Module-level state that isn't keyed by a global name isn't shared.
Possible fixes
- Ship an unbundled ESM build (for example
tscoutput, or Rollup withpreserveModules) alongside the CommonJS one. Declare"sideEffects"limited to the store registration modules. Then root imports tree-shake, shared modules exist once, and the subpath exports keep working. - Or, keeping the current webpack build: externalize cross-entry imports so an entry
requires another entry instead of inlining it. For example,components/post-titlewould import@10up/block-components/components/post-contextat runtime. At minimum this matters for modules that hold React context.
Happy to help test either approach.
- 主要语言
- TypeScript
- 星标
- 330
- 派生
- 54
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
10up/block-components 的其他 Issue
-
Repeater with allowReordering logs React key and isDragging warnings可能已有人在做 @s3rgiosan 于 5 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 80/100
10up/block-components#419 · 1 条评论 ·
-
ContentPicker SortableList logs a useSelect unstable value warning可能已有人在做 @s3rgiosan 于 5 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 42/100
10up/block-components#421 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
10up/block-components#413 ·
-
难度 5/5 一周以上 新手友好度 25/100
10up/block-components#412 ·
-
ContentPicker: invalid html可能已有人在做 @s3rgiosan 于 5 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 45/100
10up/block-components#400 ·
查看 10up/block-components 的全部 Issue
相似的 Issue
-
难度 1/5 1-3 小时 新手友好度 87/100
维护者通常 1 天内回复
-
🛠️ dette technique
难度 2/5 1-3 小时 新手友好度 61/100
betagouv/mon-entreprise#4752 · 1 条评论 ·
维护者通常 5 天内回复
-
type: bug
难度 2/5 1-3 小时 新手友好度 67/100
franciscoBSalgueiro/en-croissant#1111 ·
维护者通常 2 天内回复
-
area/providers type/feature version/v2
难度 2/5 1-3 小时 新手友好度 68/100
anomalyco/opencode#64845 · 1 条评论 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复