Skip to content

Commit

Permalink
fix: move away from default imports
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Dec 17, 2024
1 parent 1acc2af commit addea19
Show file tree
Hide file tree
Showing 24 changed files with 42 additions and 56 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Breaking Changes

- You now must import from `node-vibrant/browser`, `node-vibrant/node`, or `node-vibrant/worker` to get the correct environment-specific implementation
- `Vibrant` class is now a default export
- `Vibrant` class is now a named export
- Node 18+ is now required
- ES5 support is dropped

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ $ npm install node-vibrant

```typescript
// Node
import Vibrant from "node-vibrant/node";
import { Vibrant } from "node-vibrant/node";
// Browser
import Vibrant from "node-vibrant/browser";
import { Vibrant } from "node-vibrant/browser";
// Web Worker
import Vibrant from "node-vibrant/worker";
import { Vibrant } from "node-vibrant/worker";

// Using builder
Vibrant.from("path/to/image").getPalette((err, palette) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/node-vibrant/__tests__/vibrant.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { commands } from "@vitest/browser/context";
import { afterAll, beforeAll, describe, it } from "vitest";

import Vibrant from "../src/worker";
import { Vibrant } from "../src/worker";
import { testVibrant, testVibrantAsPromised } from "./common/helper";

import type { TestSample } from "../../../fixtures/sample/loader";
Expand Down
2 changes: 1 addition & 1 deletion packages/node-vibrant/__tests__/vibrant.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { loadTestSamples } from "../../../fixtures/sample/loader";

import { createSampleServer } from "../../../fixtures/sample/server";

import Vibrant from "../src/node";
import { Vibrant } from "../src/node";
import { testVibrant, testVibrantAsPromised } from "./common/helper";
import type http from "node:http";

Expand Down
6 changes: 3 additions & 3 deletions packages/node-vibrant/src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Vibrant from "./configs/browser";
import pipeline from "./pipeline";
import { Vibrant } from "./configs/browser";
import { pipeline } from "./pipeline";

Vibrant.use(pipeline);

export default Vibrant;
export { Vibrant };
6 changes: 3 additions & 3 deletions packages/node-vibrant/src/configs/browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BrowserImage from "@vibrant/image-browser";
import Vibrant from "./config";
import { BrowserImage } from "@vibrant/image-browser";
import { Vibrant } from "./config";

Vibrant.DefaultOpts.ImageClass = BrowserImage;

export default Vibrant;
export { Vibrant };
4 changes: 2 additions & 2 deletions packages/node-vibrant/src/configs/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Vibrant from "@vibrant/core";
import { Vibrant } from "@vibrant/core";

Vibrant.DefaultOpts.quantizer = "mmcq";
Vibrant.DefaultOpts.generators = ["default"];
Vibrant.DefaultOpts.filters = ["default"];

export default Vibrant;
export { Vibrant };
8 changes: 4 additions & 4 deletions packages/node-vibrant/src/node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import NodeImage from "@vibrant/image-node";
import Vibrant from "./configs/config";
import pipeline from "./pipeline";
import { NodeImage } from "@vibrant/image-node";
import { Vibrant } from "./configs/config";
import { pipeline } from "./pipeline";

Vibrant.DefaultOpts.ImageClass = NodeImage;
Vibrant.use(pipeline);

export default Vibrant;
export { Vibrant };
8 changes: 3 additions & 5 deletions packages/node-vibrant/src/pipeline/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import MMCQ from "@vibrant/quantizer-mmcq";
import DefaultGenerator from "@vibrant/generator-default";
import { MMCQ } from "@vibrant/quantizer-mmcq";
import { DefaultGenerator } from "@vibrant/generator-default";

import { BasicPipeline } from "@vibrant/core";

const pipeline = new BasicPipeline().filter
export const pipeline = new BasicPipeline().filter
.register(
"default",
(r: number, g: number, b: number, a: number) =>
a >= 125 && !(r > 250 && g > 250 && b > 250),
)
.quantizer.register("mmcq", MMCQ)
.generator.register("default", DefaultGenerator);

export default pipeline;
2 changes: 1 addition & 1 deletion packages/node-vibrant/src/pipeline/index.worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runPipelineInWorker } from "@vibrant/core";
import pipeline from "./";
import { pipeline } from "./";

runPipelineInWorker(self, pipeline);
4 changes: 2 additions & 2 deletions packages/node-vibrant/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { WorkerPipeline } from "@vibrant/core";
import Vibrant from "./configs/browser";
import { Vibrant } from "./configs/browser";

import PipelineWorker from "./pipeline/index.worker?worker";

Vibrant.use(new WorkerPipeline(PipelineWorker as never));

export default Vibrant;
export { Vibrant };
2 changes: 1 addition & 1 deletion packages/vibrant-core/__tests__/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";

import Builder from "../src/builder";
import { Builder } from "../src/builder";

describe("builder", () => {
it("modifies Vibrant options", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/vibrant-core/src/builder.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Filter } from "@vibrant/color";
import { assignDeep } from "./utils";
import Vibrant from "./";
import { Vibrant } from "./";
import type { Callback } from "@vibrant/types";
import type { ImageClass, ImageSource } from "@vibrant/image";

import type { Palette } from "@vibrant/color";
import type { Options } from "./options";

export default class Builder {
export class Builder {
private _src: ImageSource;
private _opts: Partial<Options>;
constructor(src: ImageSource, opts: Partial<Options> = {}) {
Expand Down
4 changes: 2 additions & 2 deletions packages/vibrant-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { buildProcessOptions } from "./options";
import Builder from "./builder";
import { Builder } from "./builder";
import { assignDeep } from "./utils";
import type { Options } from "./options";
import type { Callback } from "@vibrant/types";
Expand All @@ -13,7 +13,7 @@ export interface VibrantStatic {
from(src: ImageSource): Builder;
}

export default class Vibrant {
export class Vibrant {
private _result: ProcessResult | undefined;
private static _pipeline: Pipeline;

Expand Down
2 changes: 1 addition & 1 deletion packages/vibrant-core/src/pipeline/worker/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WorkerManager from "@vibrant/worker";
import { WorkerManager } from "@vibrant/worker";
import { Swatch } from "@vibrant/color";
import { mapValues } from "../../utils";
import type { TaskWorkerClass } from "@vibrant/worker";
Expand Down
4 changes: 1 addition & 3 deletions packages/vibrant-generator-default/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ function _generateEmptySwatches(
}
}

const DefaultGenerator: Generator = ((
export const DefaultGenerator: Generator = ((
swatches: Array<Swatch>,
opts?: DefaultGeneratorOptions,
): Palette => {
Expand All @@ -305,5 +305,3 @@ const DefaultGenerator: Generator = ((

return palette;
}) as never;

export default DefaultGenerator;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { loadTestSamples } from "../../../fixtures/sample/loader";
import BrowserImage from "../src";
import { BrowserImage } from "../src";

const SAMPLES = loadTestSamples();

Expand Down
2 changes: 1 addition & 1 deletion packages/vibrant-image-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function isSameOrigin(a: string, b: string): boolean {
);
}

export default class BrowserImage extends ImageBase {
export class BrowserImage extends ImageBase {
image: HTMLImageElement | undefined;
private _canvas: HTMLCanvasElement | undefined;
private _context: CanvasRenderingContext2D | undefined;
Expand Down
10 changes: 1 addition & 9 deletions packages/vibrant-image-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ const Jimp = configure({
plugins: [resize],
});

interface ProtocalHandler {
get(url: string | any, cb?: (res: any) => void): any;
}

interface ProtocalHandlerMap {
[protocolName: string]: ProtocalHandler;
}

const URL_REGEX = /^(\w+):\/\/.*/i;

type NodeImageSource = string | Buffer;

export default class NodeImage extends ImageBase {
export class NodeImage extends ImageBase {
private _image: InstanceType<typeof Jimp> | undefined;

private _getImage() {
Expand Down
8 changes: 3 additions & 5 deletions packages/vibrant-quantizer-mmcq/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Quantizer } from "@vibrant/quantizer";
import { Filter, Swatch } from "@vibrant/color";
import VBox from "./vbox";
import PQueue from "./pqueue";
import { VBox } from "./vbox";
import { PQueue } from "./pqueue";
import type { Pixels } from "@vibrant/image";
import type { QuantizerOptions } from "@vibrant/quantizer";

Expand Down Expand Up @@ -32,7 +32,7 @@ function _splitBoxes(pq: PQueue<VBox>, target: number): void {
}
}

const MMCQ = (pixels: Pixels, opts: QuantizerOptions): Array<Swatch> => {
export const MMCQ = (pixels: Pixels, opts: QuantizerOptions): Array<Swatch> => {
if (pixels.length === 0 || opts.colorCount < 2 || opts.colorCount > 256) {
throw new Error("Wrong MMCQ parameters");
}
Expand Down Expand Up @@ -69,5 +69,3 @@ function generateSwatches(pq: PQueue<VBox>) {
}
return swatches;
}

export default MMCQ;
2 changes: 1 addition & 1 deletion packages/vibrant-quantizer-mmcq/src/pqueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ interface PQueueComparator<T> {
(a: T, b: T): number;
}

export default class PQueue<T> {
export class PQueue<T> {
contents: T[];
private _sorted: boolean;
private _comparator: PQueueComparator<T>;
Expand Down
2 changes: 1 addition & 1 deletion packages/vibrant-quantizer-mmcq/src/vbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Dimension {
const SIGBITS = 5;
const RSHIFT = 8 - SIGBITS;

export default class VBox {
export class VBox {
static build(pixels: Pixels): VBox {
const h = new Histogram(pixels, { sigBits: SIGBITS });
const { rmin, rmax, gmin, gmax, bmin, bmax } = h;
Expand Down
4 changes: 2 additions & 2 deletions packages/vibrant-worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import WorkerPool from "./pool";
import { WorkerPool } from "./pool";
import type { TaskWorkerClass } from "./common";

export default class WorkerManager {
export class WorkerManager {
private _pools: { [name: string]: WorkerPool } = {};

register(name: string, WorkerClass: TaskWorkerClass) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vibrant-worker/src/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Task<R> extends WorkerRequest {
// const WorkerClass: TaskWorkerClass = require('worker-loader?inline=true!./worker')

const MAX_WORKER_COUNT = 5;
export default class WorkerPool {
export class WorkerPool {
private _taskId = 0;

private _workers: TaskWorker[] = [];
Expand Down

0 comments on commit addea19

Please sign in to comment.