lokesh / color-thief

Grab the color palette from an image using just Javascript. Works in the browser and in Node.
https://lokeshdhakar.com/projects/color-thief/
MIT License
12.49k stars 1.31k forks source link

webpack and target node takes the wrong dist file #186

Closed ghost closed 3 years ago

ghost commented 4 years ago

I use colorthief in node environment and I use webpack.

But webpack seems to take the wrong dist file (browser version color-thief.mjs instead node version color-thief.js ?), I get errors like TypeError: colorthief__WEBPACK_IMPORTED_MODULE_0__.default.getColor is not a function or ReferenceError: document is not defined (Node and browser version seems to differ in export)

I can fix with import colorThief from "colorthief/dist/color-thief.js"; in my code

But I have the same issue with the quantize module (TypeError: quantize is not a function) using in colorthief (takes browser version index.mjs instead node version index.js ?)

I patch this like follow for me (dist only), but it will not works anymore in the current and common use case of colorthief, I think ...

diff --git a/node_modules/colorthief/dist/color-thief.js b/node_modules/colorthief/dist/color-thief.js
index 2833cec..9c4d318 100644
--- a/node_modules/colorthief/dist/color-thief.js
+++ b/node_modules/colorthief/dist/color-thief.js
@@ -1,5 +1,5 @@
 const getPixels = require('get-pixels');
-const quantize = require('quantize');
+const quantize = require('quantize/dist/index.js');

 function createPixelArray(imgData, pixelCount, quality) {
     const pixels = imgData;