question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Color spaces are not tree-shakeable

See original GitHub issue

(Remaining part of #159)

While the new procedural API is tree-shakeable wrt functions, since color spaces register themselves onto ColorSpace.registry, this means they are not tree-shakeable.

To make them tree-shakeable, we’d need to eliminate side effects from the color space modules. Then, the importing code would register them.

Something like:

import ColorSpace from "COLORJS_ROOT/src/space.js";
import oklch from "COLORJS_ROOT/src/spaces/oklch.js";
ColorSpace.register(oklch);

We could make this less painful by offering pre-generated indices that do this for all existing color spaces, but I’m still struggling with the idea of requiring more plumbing to use Color.js, which makes it easier to make mistakes. E.g. someone could easily forget to register their color space and do something like:

import oklch from "COLORJS_ROOT/src/spaces/oklch.js";
let color = new Color("oklch", [.5, .2, 340]); // TypeError: No color space found with id = "oklch"

Note that registration is needed only to use string ids instead of color space objects. The API is perfectly usable with just passing identifiers around and never registering any color space, like so:

import srgb from "COLORJS_ROOT/src/spaces/srgb.js";
import oklch from "COLORJS_ROOT/src/spaces/oklch.js";
let color = new Color(oklch, [.5, .2, 340]);
color.to(srgb);

Thoughts?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lydellcommented, Aug 15, 2022

Sure thing! Opened a new issue: https://github.com/LeaVerou/color.js/issues/204

0reactions
LeaVeroucommented, Aug 15, 2022

Can you open a new issue then since this is closed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimize bundle size with tree-shaking - culori
However, the way color spaces are initialized prevents the library from being tree-shaken, so when you use a bundler such as Webpack or...
Read more >
Releasing Color.js: A library that takes color seriously
Completely color space agnostic, each Color object just has a reference to a color space, a list of coordinates,, and optionally an alpha....
Read more >
Color.js: Let's get serious about color
Modules for a wide variety of color spaces, including Lab/LCh, OKLab/OKLCh, ... Procedural, tree-shakeable API available for performance sensitive tasks and ...
Read more >
Why are "providers" in components or modules not tree ...
As Angular explained, providedIn root leads to providing a service on application level, but still angular application can tree shake the ...
Read more >
stonecypher/hsluv.ts - GitHub Pages
This package primarily exists to implement two color spaces: HSLuv and HPLuv . ... es6 native tree-shakeable for most work, es6 commonjs bundled...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found