Subpath bundles duplicate PostContext, and the root import can't be tree-shaken

Abierto
#417 0 comentarios 0 reacciones 0 asignados Ver en GitHub

@fabiankaegy ya está trabajando en esto.

Desde el 30/10/2026.

  • #418 de @fabiankaegy — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Necesita aclaración
Estado de actividad
Activo
Stack tecnológico
react, rollup, typescript, webpack
Área
build-system

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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 tsc output, or Rollup with preserveModules) 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-title would import @10up/block-components/components/post-context at runtime. At minimum this matters for modules that hold React context.

Happy to help test either approach.

Lenguaje dominante
TypeScript
Estrellas
330
Forks
54
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de 10up/block-components

Todos los issues de 10up/block-components

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.