Improve tree-shaking

Abierto
#217 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
55/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
javascript, node.js, rollup

Línea de trabajo

Read the package build configuration and inspect how it produces dist/d3-shape.mjs; the issue identifies Rollup's output.preserveModules as a possible direction. Run the provided esbuild reproduction to record bundle sizes, then check whether publishing per-module files restores the 3.2.0 behavior while keeping the dist/ layout and bundled CDN builds.

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

Descripción

Since 3.2.1, the package's default entry points to one bundled file, dist/d3-shape.mjs, instead of separate source files. sideEffects: false used to let bundlers drop unused files. With one file, importing anything keeps that module, and bundlers then have to prove each top-level statement is safe to remove.

Most of them can't be removed, so importing one curve now pulls in nearly all the others.

Reproduction
npm i [email protected]
echo "export { curveLinear } from 'd3-shape'" \
  | npx [email protected] --bundle --minify --format=esm | gzip -c | wc -c
Import 3.2.0 (gzip) 3.2.1 (gzip)
curveLinear 265 B 2,509 B
arc 2,478 B 4,871 B

Other toolchains are affected too, though less.

Cause

The file has top-level statements that bundlers treat as side effects:

  • 17 prototype assignments, such as Linear.prototype = { … }
  • calls such as var catmullRom = (function custom(alpha) { … })(0.5) and const sqrt3 = sqrt(3)

The prototype assignments matter most. Adding /*#__PURE__*/ to all 18 top-level calls only reduced the curveLinear case to about 2.3 KB, and those annotations can't be applied to assignments.

Suggested fix

Publish one file per module again, for example with Rollup's output.preserveModules, while keeping the dist/ layout and the bundled CDN builds. This restores the behavior of 3.2.0 without changing the source.

The same build change shipped in d3-array 3.2.5 and d3-time 3.1.1, which grew in the same way under esbuild: ticks + tickIncrement from 420 B to about 1.3 KB, and timeDay + utcDay from 597 B to about 2.7 KB. I haven't looked at the cause in those packages.

Lenguaje dominante
JavaScript
Estrellas
2.5k
Forks
307
Merge medio
4 h 16 min
PR fusionados (30 d)
1

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

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 d3/d3-shape

Todos los issues de d3/d3-shape

Issues similares

Más issues de JavaScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.