Tree-shakable `parse()` doesn't seem to work
See original GitHub issueHiya 👋,
I tried to follow the documentation but I can’t make the tree-shakable API work.
The parse
function returns the following error:
Uncaught TypeError: Could not parse red as a color. Missing a plugin?
Here’s a reproduction code (and it’s not ESM, the same code doesn’t work in a Vue app with Vite):
<!DOCTYPE html>
<html lang="en">
<script type="module">
import { parse } from 'https://esm.run/colorjs.io/fn'
console.log(parse('red'))
</script>
</html>
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Tree shaking issue in 2.17 · Issue #2207 · date-fns ... - GitHub
So, long story short I created a vanilla project, and by importing like this, tree-shaking works correctly. import { parse, format } from...
Read more >Reduce JavaScript payloads with tree shaking - web.dev
In dev builds, this doesn't change anything, as the entire module gets imported regardless. In production builds, webpack can be configured to "shake"...
Read more >date-fns 2 - can't get tree-shaking to work - Stack Overflow
the test contains 2 files, one acting as the "library", the other acting as the code to run, and it is as simple...
Read more >Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module...
Read more >Tree Shaking - How to Clean up Your JavaScript - KeyCDN
Tree shaking is a strategy web developers use to create leaner JavaScript bundles by getting rid of unused code.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ah, narrowed it down. The thing is, with the tree-shakable API you need to do everything manually, including registering color spaces you want Color.js to know about:
Once you do that, parsing sRGB colors works. Obviously you’d need to do it separately for other color spaces as well.
You can still use an unregistered color space in many places by just passing around its object, but for parsing its formats (or serializing to them) it absolutely needs to be registered.
Perhaps we should include a way to register all color spaces automatically, though do note this would make them non tree-shakable (this was the original design of the tree-shakable API, see #163 ) since you’d have a reference to them whether they are used or not. Basically, once you register a color space, it’s not tree-shakable anymore. If you have any ideas about this, they are welcome!
Demo: https://codepen.io/leaverou/pen/MWXqgXX?editors=0010
Absolutely.