@think-grid-labs/opti-assets 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ export interface UseImageOptimizerResult {
2
+ optimizedUrl: string | null;
3
+ loading: boolean;
4
+ error: string | null;
5
+ }
6
+ export declare const useImageOptimizer: (src: string | Blob, quality?: number) => UseImageOptimizerResult;
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ import { useState, useEffect } from 'react';
2
+ // Dynamic import for the Wasm module
3
+ const loadWasm = async () => {
4
+ // We import from the local pkg directory (built via wasm-pack)
5
+ return import('../pkg/opti_assets_browser.js');
6
+ };
7
+ export const useImageOptimizer = (src, quality = 80) => {
8
+ const [state, setState] = useState({
9
+ optimizedUrl: null,
10
+ loading: false,
11
+ error: null,
12
+ });
13
+ useEffect(() => {
14
+ let mounted = true;
15
+ const process = async () => {
16
+ if (!src)
17
+ return;
18
+ setState(prev => ({ ...prev, loading: true, error: null }));
19
+ try {
20
+ // 1. Get Blob
21
+ let blob;
22
+ if (typeof src === 'string') {
23
+ const resp = await fetch(src);
24
+ if (!resp.ok)
25
+ throw new Error(`Failed to fetch image: ${resp.statusText}`);
26
+ blob = await resp.blob();
27
+ }
28
+ else {
29
+ blob = src;
30
+ }
31
+ // 2. Load bytes
32
+ const buffer = await blob.arrayBuffer();
33
+ const bytes = new Uint8Array(buffer);
34
+ // 3. Wasm Optimize
35
+ const wasm = await loadWasm();
36
+ const optimizedBytes = wasm.optimize_image_sync(bytes, quality);
37
+ // 4. Create Object URL
38
+ const optimizedBlob = new Blob([optimizedBytes], { type: 'image/webp' });
39
+ const url = URL.createObjectURL(optimizedBlob);
40
+ if (mounted) {
41
+ setState({
42
+ optimizedUrl: url,
43
+ loading: false,
44
+ error: null
45
+ });
46
+ }
47
+ }
48
+ catch (err) {
49
+ if (mounted) {
50
+ setState(prev => ({
51
+ ...prev,
52
+ loading: false,
53
+ error: err.message || 'Unknown error'
54
+ }));
55
+ }
56
+ }
57
+ };
58
+ process();
59
+ return () => {
60
+ mounted = false;
61
+ if (state.optimizedUrl) {
62
+ URL.revokeObjectURL(state.optimizedUrl);
63
+ }
64
+ };
65
+ }, [src, quality]);
66
+ return state;
67
+ };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@think-grid-labs/opti-assets",
3
+ "version": "0.1.0",
4
+ "description": "Unified image optimization toolkit for the browser and React",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "pkg"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "./browser": {
19
+ "types": "./pkg/opti_assets_browser.d.ts",
20
+ "import": "./pkg/opti_assets_browser.js",
21
+ "default": "./pkg/opti_assets_browser.js"
22
+ }
23
+ },
24
+ "scripts": {
25
+ "build": "npm run build:wasm && npm run build:ts",
26
+ "build:wasm": "wasm-pack build --target web --out-dir pkg --release",
27
+ "build:ts": "tsc",
28
+ "test": "vitest"
29
+ },
30
+ "keywords": [
31
+ "image-optimization",
32
+ "wasm",
33
+ "react",
34
+ "rust",
35
+ "webp"
36
+ ],
37
+ "author": "DennisP <dennis@thinkgrid-labs.com>",
38
+ "license": "MIT",
39
+ "peerDependencies": {
40
+ "react": ">=16.8.0"
41
+ },
42
+ "devDependencies": {
43
+ "typescript": "^5.0.0",
44
+ "@types/react": "^18.0.0",
45
+ "concurrently": "^8.0.0",
46
+ "vitest": "^0.34.0",
47
+ "@testing-library/react": "^14.0.0",
48
+ "jsdom": "^22.0.0",
49
+ "@types/node": "^20.0.0"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public"
53
+ }
54
+ }