Skip to content

Visualize Your Ideas With Code

fix: prevent Color tree shaking (#666)

Jacobe5028e3c176d
diff --git a/packages/core/package.json b/packages/core/package.json
index 612ccf4e..824a3c0d 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -12,9 +12,7 @@
"watch": "ttsc -p tsconfig.build.json -w",
"test": "vitest"
},
- "sideEffects": [
- "./lib/types/Color.js"
- ],
+ "sideEffects": false,
"repository": {
"type": "git",
"url": "https://github.com/motion-canvas/motion-canvas.git"
diff --git a/packages/core/src/types/Color.ts b/packages/core/src/types/Color.ts
index 151eb849..8ab367fb 100644
--- a/packages/core/src/types/Color.ts
+++ b/packages/core/src/types/Color.ts
@@ -44,68 +44,76 @@ declare module 'chroma-js' {
}
}
-Color.symbol = Color.prototype.symbol = Symbol.for(
- '@motion-canvas/core/types/Color',
-);
-
-Color.lerp = Color.prototype.lerp = (
- from: Color | string | null,
- to: Color | string | null,
- value: number,
- colorSpace: InterpolationMode = 'lch',
-) => {
- if (typeof from === 'string') {
- from = new Color(from);
- }
- if (typeof to === 'string') {
- to = new Color(to);
- }
+// iife prevents tree shaking from stripping our methods.
+type ExtendedColor = Color;
+const ExtendedColor: typeof Color = (() => {
+ Color.symbol = Color.prototype.symbol = Symbol.for(
+ '@motion-canvas/core/types/Color',
+ );
- const fromIsColor = from instanceof Color;
- const toIsColor = to instanceof Color;
+ Color.lerp = Color.prototype.lerp = (
+ from: Color | string | null,
+ to: Color | string | null,
+ value: number,
+ colorSpace: InterpolationMode = 'lch',
+ ) => {
+ if (typeof from === 'string') {
+ from = new Color(from);
+ }
+ if (typeof to === 'string') {
+ to = new Color(to);
+ }
- if (!fromIsColor) {
- from = toIsColor ? (to as Color).alpha(0) : new Color('rgba(0, 0, 0, 0)');
- }
- if (!toIsColor) {
- to = fromIsColor ? (from as Color).alpha(0) : new Color('rgba(0, 0, 0, 0)');
- }
+ const fromIsColor = from instanceof Color;
+ const toIsColor = to instanceof Color;
+
+ if (!fromIsColor) {
+ from = toIsColor ? (to as Color).alpha(0) : new Color('rgba(0, 0, 0, 0)');
+ }
+ if (!toIsColor) {
+ to = fromIsColor
+ ? (from as Color).alpha(0)
+ : new Color('rgba(0, 0, 0, 0)');
+ }
+
+ return mix(from as Color, to as Color, value, colorSpace);
+ };
+
+ Color.createLerp = Color.prototype.createLerp =
+ (colorSpace: InterpolationMode) =>
+ (from: Color | string | null, to: Color | string | null, value: number) =>
+ Color.lerp(from, to, value, colorSpace);
+
+ Color.createSignal = (
+ initial?: SignalValue<PossibleColor>,
+ interpolation: InterpolationFunction<Color> = Color.lerp,
+ ): ColorSignal<void> => {
+ return new SignalContext(
+ initial,
+ interpolation,
+ undefined,
+ value => new Color(value),
+ ).toSignal();
+ };
+
+ Color.prototype.toSymbol = () => {
+ return Color.symbol;
+ };
+
+ Color.prototype.serialize = function (this: Color): SerializedColor {
+ return this.css();
+ };
+
+ Color.prototype.lerp = function (
+ this: Color,
+ to: Color,
+ value: number,
+ colorSpace?: ColorSpace,
+ ) {
+ return Color.lerp(this, to, value, colorSpace);
+ };
+
+ return Color;
+})();
- return mix(from as Color, to as Color, value, colorSpace);
-};
-
-Color.createLerp = Color.prototype.createLerp =
- (colorSpace: InterpolationMode) =>
- (from: Color | string | null, to: Color | string | null, value: number) =>
- Color.lerp(from, to, value, colorSpace);
-
-Color.createSignal = (
- initial?: SignalValue<PossibleColor>,
- interpolation: InterpolationFunction<Color> = Color.lerp,
-): ColorSignal<void> => {
- return new SignalContext(
- initial,
- interpolation,
- undefined,
- value => new Color(value),
- ).toSignal();
-};
-
-Color.prototype.toSymbol = () => {
- return Color.symbol;
-};
-
-Color.prototype.serialize = function (this: Color): SerializedColor {
- return this.css();
-};
-
-Color.prototype.lerp = function (
- this: Color,
- to: Color,
- value: number,
- colorSpace?: ColorSpace,
-) {
- return Color.lerp(this, to, value, colorSpace);
-};
-
-export {Color};
+export {ExtendedColor as Color};
diff --git a/packages/docs/docs/getting-started/tweening.mdx b/packages/docs/docs/getting-started/tweening.mdx
index a093c031..8c37d3ba 100644
--- a/packages/docs/docs/getting-started/tweening.mdx
+++ b/packages/docs/docs/getting-started/tweening.mdx
@@ -104,8 +104,7 @@ yield *
});
```
-[`Color.lerp`](/api/core/types/Color#lerp) is a static function that
-interpolates between two colors:
+`Color.lerp` is a static function that interpolates between two colors:
<AnimationPlayer small name="tweening-color" />