color-js / color.js

Color conversion & manipulation library by the editors of the CSS Color specifications
https://colorjs.io
MIT License
1.93k stars 81 forks source link

Bundle compatibility: .contrast is not a function, .deltaE is not a function #235

Closed ambar closed 2 years ago

ambar commented 2 years ago

Bug

Using vite@3.2.2 or esbuild@0.15.12

Reproduce

input.js

import Color from 'colorjs.io'

const red = new Color('red')
const blue = new Color('blue')

// ERROR: .contrast is not a function
red.contrast(blue, 'WCAG21')
// ERROR: .deltaE is not a function
red.deltaE(blue, '2000')

// PASSED
// red.contrastWCAG21(blue)
// red.deltaE2000(blue)

bundle:

esbuild --bundle --minify input.js --outfile=bundle.js && node bundle.js
jgerigmeyer commented 2 years ago

@ambar Thanks for reporting -- I can reproduce this error using esbuild@0.15.12 (though I can't reproduce it using vite@3.2.2). During minification, esbuild by default renames function names, and colorjs.io relies upon function names in its default package export. You can disable the esbuild feature by adding --keep-names to your command:

esbuild --bundle --minify --keep-names input.js --outfile=bundle.js && node bundle.js
ambar commented 2 years ago

@jgerigmeyer You should run vite build to verify bundled result.