Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions fix-plugin-types.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env node
/* eslint-env node */
/**
* Post-build script to fix plugin .d.ts files for Node16/NodeNext module resolution.
* Converts `export default function` to `declare function` + `export =`
*/

import { readFileSync, readdirSync, writeFileSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const pluginsDir = join(__dirname, "dist", "plugins");
const pluginFiles = readdirSync(pluginsDir).filter((f) => f.endsWith(".d.ts"));

for (const file of pluginFiles) {
const filePath = join(pluginsDir, file);
let content = readFileSync(filePath, "utf-8");

// Normalize line endings to LF
content = content.replace(/\r\n/g, "\n");

// Replace "export default function name" with "declare function name" + "export ="
content = content.replace(
/export default function (\w+)(\([^)]*\):\s*void);/g,
"declare function $1$2;\nexport = $1;"
);

// Remove empty export statements
content = content.replace(/\nexport \{\};\n/g, "\n");
content = content.replace(/\nexport \{\};$/g, "");
content = content.replace(/^export \{\};\n/g, "");

// Remove trailing blank lines and ensure single newline at end
content = content.replace(/\n+$/g, "\n");

writeFileSync(filePath, content);
console.log("Fixed " + file);
}

console.log("All plugin types fixed for Node16/NodeNext module resolution.");
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"check-types": "tsc --noEmit true",
"test": "jest tests --coverage",
"benchmark": "tsc --outDir bench --skipLibCheck --esModuleInterop ./tests/benchmark.ts && node ./bench/tests/benchmark.js && rm -rf ./bench",
"build": "rm -rf ./dist/* && rollup --config",
"build": "rm -rf ./dist/* && rollup --config && node fix-plugin-types.mjs",
"release": "npm run build && cp *.json dist && cp *.md dist && npm publish dist",
"check-release": "npm run release -- --dry-run"
},
Expand Down
10 changes: 4 additions & 6 deletions src/plugins/a11y.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AnyColor } from "../types";
import { Plugin } from "../extend";
import { AnyColor, Parsers } from "../types";
import { Colord } from "../colord";
import { getContrast } from "../get/getContrast";
import { getLuminance } from "../get/getLuminance";
import { round, floor } from "../helpers";
Expand Down Expand Up @@ -41,7 +41,7 @@ declare module "../colord" {
* Follows Web Content Accessibility Guidelines 2.0.
* https://www.w3.org/TR/WCAG20/
*/
const a11yPlugin: Plugin = (ColordClass): void => {
export default function a11yPlugin(ColordClass: typeof Colord, _parsers: Parsers): void {
/**
* Returns WCAG text color contrast requirement.
* Read explanation here https://webaim.org/resources/contrastchecker/
Expand All @@ -64,6 +64,4 @@ const a11yPlugin: Plugin = (ColordClass): void => {
ColordClass.prototype.isReadable = function (color2 = "#FFF", options = {}) {
return this.contrast(color2) >= getMinimalContrast(options);
};
};

export default a11yPlugin;
}
10 changes: 4 additions & 6 deletions src/plugins/cmyk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CmykaColor } from "../types";
import { Plugin } from "../extend";
import { CmykaColor, Parsers } from "../types";
import { Colord } from "../colord";
import { parseCmyka, roundCmyka, rgbaToCmyka } from "../colorModels/cmyk";
import { parseCmykaString, rgbaToCmykaString } from "../colorModels/cmykString";

Expand All @@ -24,7 +24,7 @@ declare module "../colord" {
* https://lea.verou.me/2009/03/cmyk-colors-in-css-useful-or-useless/
* https://en.wikipedia.org/wiki/CMYK_color_model
*/
const cmykPlugin: Plugin = (ColordClass, parsers): void => {
export default function cmykPlugin(ColordClass: typeof Colord, parsers: Parsers): void {
ColordClass.prototype.toCmyk = function () {
return roundCmyka(rgbaToCmyka(this.rgba));
};
Expand All @@ -35,6 +35,4 @@ const cmykPlugin: Plugin = (ColordClass, parsers): void => {

parsers.object.push([parseCmyka, "cmyk"]);
parsers.string.push([parseCmykaString, "cmyk"]);
};

export default cmykPlugin;
}
9 changes: 4 additions & 5 deletions src/plugins/harmonies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Plugin } from "../extend";
import { Parsers } from "../types";
import { Colord } from "../colord";

export type HarmonyType =
| "analogous"
Expand All @@ -22,7 +23,7 @@ declare module "../colord" {
* A plugin adding functionality to generate harmony colors.
* https://en.wikipedia.org/wiki/Harmony_(color)
*/
const harmoniesPlugin: Plugin = (ColordClass): void => {
export default function harmoniesPlugin(ColordClass: typeof Colord, _parsers: Parsers): void {
/**
* Harmony colors are colors with particular hue shift of the original color.
*/
Expand All @@ -39,6 +40,4 @@ const harmoniesPlugin: Plugin = (ColordClass): void => {
ColordClass.prototype.harmonies = function (type = "complementary") {
return hueShifts[type].map((shift) => this.rotate(shift));
};
};

export default harmoniesPlugin;
}
10 changes: 4 additions & 6 deletions src/plugins/hwb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HwbaColor } from "../types";
import { Plugin } from "../extend";
import { HwbaColor, Parsers } from "../types";
import { Colord } from "../colord";
import { parseHwba, rgbaToHwba, roundHwba } from "../colorModels/hwb";
import { parseHwbaString, rgbaToHwbaString } from "../colorModels/hwbString";

Expand All @@ -23,7 +23,7 @@ declare module "../colord" {
* https://en.wikipedia.org/wiki/HWB_color_model
* https://www.w3.org/TR/css-color-4/#the-hwb-notation
*/
const hwbPlugin: Plugin = (ColordClass, parsers): void => {
export default function hwbPlugin(ColordClass: typeof Colord, parsers: Parsers): void {
ColordClass.prototype.toHwb = function () {
return roundHwba(rgbaToHwba(this.rgba));
};
Expand All @@ -34,6 +34,4 @@ const hwbPlugin: Plugin = (ColordClass, parsers): void => {

parsers.string.push([parseHwbaString, "hwb"]);
parsers.object.push([parseHwba, "hwb"]);
};

export default hwbPlugin;
}
10 changes: 4 additions & 6 deletions src/plugins/lab.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LabaColor, AnyColor } from "../types";
import { Plugin } from "../extend";
import { LabaColor, AnyColor, Parsers } from "../types";
import { Colord } from "../colord";
import { parseLaba, roundLaba, rgbaToLaba } from "../colorModels/lab";
import { getDeltaE00 } from "../get/getPerceivedDifference";
import { clamp, round } from "../helpers";
Expand All @@ -25,7 +25,7 @@ declare module "../colord" {
* A plugin adding support for CIELAB color space.
* https://en.wikipedia.org/wiki/CIELAB_color_space
*/
const labPlugin: Plugin = (ColordClass, parsers): void => {
export default function labPlugin(ColordClass: typeof Colord, parsers: Parsers): void {
ColordClass.prototype.toLab = function () {
return roundLaba(rgbaToLaba(this.rgba));
};
Expand All @@ -37,6 +37,4 @@ const labPlugin: Plugin = (ColordClass, parsers): void => {
};

parsers.object.push([parseLaba, "lab"]);
};

export default labPlugin;
}
10 changes: 4 additions & 6 deletions src/plugins/lch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LchaColor } from "../types";
import { Plugin } from "../extend";
import { LchaColor, Parsers } from "../types";
import { Colord } from "../colord";
import { parseLcha, roundLcha, rgbaToLcha } from "../colorModels/lch";
import { parseLchaString, rgbaToLchaString } from "../colorModels/lchString";

Expand All @@ -24,7 +24,7 @@ declare module "../colord" {
* https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/
* https://en.wikipedia.org/wiki/CIELAB_color_space#Cylindrical_model
*/
const lchPlugin: Plugin = (ColordClass, parsers): void => {
export default function lchPlugin(ColordClass: typeof Colord, parsers: Parsers): void {
ColordClass.prototype.toLch = function () {
return roundLcha(rgbaToLcha(this.rgba));
};
Expand All @@ -35,6 +35,4 @@ const lchPlugin: Plugin = (ColordClass, parsers): void => {

parsers.string.push([parseLchaString, "lch"]);
parsers.object.push([parseLcha, "lch"]);
};

export default lchPlugin;
}
8 changes: 3 additions & 5 deletions src/plugins/minify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Colord } from "../colord";
import { Plugin } from "../extend";
import { Parsers } from "../types";
import { round } from "../helpers";

interface MinificationOptions {
Expand All @@ -21,7 +21,7 @@ declare module "../colord" {
/**
* A plugin adding a color minification utilities.
*/
const minifyPlugin: Plugin = (ColordClass): void => {
export default function minifyPlugin(ColordClass: typeof Colord, _parsers: Parsers): void {
// Finds the shortest hex representation
const minifyHex = (instance: Colord): string | null => {
const hex = instance.toHex();
Expand Down Expand Up @@ -113,6 +113,4 @@ const minifyPlugin: Plugin = (ColordClass): void => {

return findShortestString(variants);
};
};

export default minifyPlugin;
}
9 changes: 3 additions & 6 deletions src/plugins/mix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AnyColor } from "../types";
import { Plugin } from "../extend";
import { AnyColor, Parsers } from "../types";
import { mix } from "../manipulate/mix";
import { Colord } from "../colord";

Expand Down Expand Up @@ -30,7 +29,7 @@ declare module "../colord" {
/**
* A plugin adding a color mixing utilities.
*/
const mixPlugin: Plugin = (ColordClass): void => {
export default function mixPlugin(ColordClass: typeof Colord, _parsers: Parsers): void {
ColordClass.prototype.mix = function (color2, ratio = 0.5) {
const instance2 = color2 instanceof ColordClass ? color2 : new ColordClass(color2);

Expand Down Expand Up @@ -61,6 +60,4 @@ const mixPlugin: Plugin = (ColordClass): void => {
ColordClass.prototype.tones = function (count) {
return mixPalette(this, "#808080", count);
};
};

export default mixPlugin;
}
10 changes: 4 additions & 6 deletions src/plugins/names.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ParseFunction, RgbaColor } from "../types";
import { Plugin } from "../extend";
import { ParseFunction, RgbaColor, Parsers } from "../types";
import { Colord } from "../colord";

interface ConvertOptions {
closest?: boolean;
Expand All @@ -19,7 +19,7 @@ declare module "../colord" {
* Supports 'transparent' string as defined in
* https://drafts.csswg.org/css-color/#transparent-color
*/
const namesPlugin: Plugin = (ColordClass, parsers): void => {
export default function namesPlugin(ColordClass: typeof Colord, parsers: Parsers): void {
// The default CSS color names dictionary
// The properties order is optimized for better compression
const NAME_HEX_STORE: Record<string, string> = {
Expand Down Expand Up @@ -235,6 +235,4 @@ const namesPlugin: Plugin = (ColordClass, parsers): void => {
};

parsers.string.push([parseColorName, "name"]);
};

export default namesPlugin;
}
10 changes: 4 additions & 6 deletions src/plugins/xyz.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { XyzaColor } from "../types";
import { Plugin } from "../extend";
import { XyzaColor, Parsers } from "../types";
import { Colord } from "../colord";
import { parseXyza, rgbaToXyza, roundXyza } from "../colorModels/xyz";

declare module "../colord" {
Expand All @@ -13,12 +13,10 @@ declare module "../colord" {
* Wikipedia: https://en.wikipedia.org/wiki/CIE_1931_color_space
* Helpful article: https://www.sttmedia.com/colormodel-xyz
*/
const xyzPlugin: Plugin = (ColordClass, parsers): void => {
export default function xyzPlugin(ColordClass: typeof Colord, parsers: Parsers): void {
ColordClass.prototype.toXyz = function () {
return roundXyza(rgbaToXyza(this.rgba));
};

parsers.object.push([parseXyza, "xyz"]);
};

export default xyzPlugin;
}