Skip to content

Commit

Permalink
feat: support custom urls for wasm and js file (#7)
Browse files Browse the repository at this point in the history
* feat: support custom urls for wasm and js file
  • Loading branch information
gz65555 authored Dec 10, 2024
1 parent f6d2d53 commit 55e41d4
Show file tree
Hide file tree
Showing 20 changed files with 926 additions and 607 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.js
4 changes: 2 additions & 2 deletions example/page/DownloadComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import jszip from "jszip";
import { PreEncodeFile } from "./type";
import { useState } from "react";
import { downloadBlob } from "./utils";
import { encodeToKTX2 } from "../../src";
import { encodeToKTX2 } from "../../src/web";

export const DownloadComponent: React.FC<{ list: PreEncodeFile[] }> = ({ list }) => {
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -19,7 +19,7 @@ export const DownloadComponent: React.FC<{ list: PreEncodeFile[] }> = ({ list })
const promises = list.map((item) => {
return item.file.arrayBuffer().then(async (buffer) => {
return encodeToKTX2(new Uint8Array(buffer), {
isUASTC: false,
isUASTC: true,
enableDebug: false,
qualityLevel: 230,
generateMipmap: true
Expand Down
24 changes: 11 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>KTX2 Encoder</title>
</head>

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>KTX2 Encoder</title>
</head>

<body>
<div id="root"></div>
<script type="module" src="/example/page/main.tsx"></script>
</body>

</html>
<body>
<div id="root"></div>
<script type="module" src="/example/page/main.tsx"></script>
</body>
</html>
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"scripts": {
"dev": "vite --host",
"docs": "typedoc ./src/web/index.ts --plugin typedoc-plugin-markdown --out ./docs",
"build": "tsc -p tsconfig.json && cp -R src/node/basis dist/node",
"build": "tsc -p tsconfig.json && cp -R src/basis dist",
"test": "npm run test:node && npm run test:web",
"test:node": "vitest run ./test/node.test.ts",
"test:web": "vitest run --browser.name=chrome ./test/web.test.ts",
Expand All @@ -51,22 +51,22 @@
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/browser": "^1.3.1",
"@vitest/browser": "^2.1.8",
"@webgpu/types": "^0.1.40",
"antd": "^5.8.2",
"bumpp": "^9.4.0",
"jszip": "^3.10.1",
"prettier": "^2.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sharp": "^0.33.2",
"sharp": "^0.33.5",
"tsc-alias": "^1.8.8",
"tsconfig-paths": "^4.2.0",
"typedoc": "^0.26.3",
"typedoc-plugin-markdown": "^4.1.1",
"typescript": "^5",
"vite": "^5.1.5",
"vitest": "^1.3.1",
"vitest": "^2.1.8",
"webdriverio": "^8.32.4"
},
"dependencies": {
Expand Down
1,353 changes: 859 additions & 494 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

Binary file added public/basis_encoder.wasm
Binary file not shown.
Binary file modified public/tests/DuckCM-etc1s.ktx2
Binary file not shown.
Binary file modified public/tests/DuckCM-uastc.ktx2
Binary file not shown.
4 changes: 4 additions & 0 deletions src/basis/basis_encoder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { IBasisModule } from "../../type";

declare const BASIS: () => IBasisModule;
export default BASIS;
20 changes: 20 additions & 0 deletions src/basis/basis_encoder.js

Large diffs are not rendered by default.

Binary file added src/basis/basis_encoder.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/node/NodeBasisEncoder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SourceType } from "../enum.js";
import { IBasisModule, IEncodeOptions } from "../type.js";
import { applyInputOptions } from "../utils.js";
import BASIS from "./basis/basis_encoder.cjs";
import BASIS from "../basis/basis_encoder.js";

let promise: Promise<IBasisModule> | null = null;

Expand Down
24 changes: 0 additions & 24 deletions src/node/basis/basis_encoder.cjs

This file was deleted.

2 changes: 0 additions & 2 deletions src/node/basis/basis_encoder.d.ts

This file was deleted.

Binary file removed src/node/basis/basis_encoder.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,12 @@ export interface IEncodeOptions {
* @returns
*/
imageDecoder?: (buffer: Uint8Array) => Promise<{ width: number; height: number; data: Uint8Array }>;
/**
* js url
*/
jsUrl?: string;
/**
* wasm url
*/
wasmUrl?: string;
}
64 changes: 13 additions & 51 deletions src/web/BrowserBasisEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,23 @@ import { BasisTextureType, SourceType } from "../enum.js";
declare function importScripts(...args: any): any;

let promise: Promise<IBasisModule> | null = null;
const isInWorker = typeof document === "undefined";

class BrowserBasisEncoder {
async init() {
async init(options?: { jsUrl?: string; wasmUrl?: string }) {
if (!promise) {
function _init(): Promise<IBasisModule> {
return new Promise((resolve, reject) => {
if (!isInWorker) {
Promise.all([
fetch(
"https://mdn.alipayobjects.com/rms/afts/file/A*SrRkQarYYl4AAAAAAAAAAAAAARQnAQ/basis_encoder.js"
).then((res) => res.text()),
fetch(
"https://mdn.alipayobjects.com/rms/afts/file/A*qFWbTrA0hZYAAAAAAAAAAAAAARQnAQ/basis_encoder.wasm"
).then((res) => res.arrayBuffer())
])
.then(([basisEncoderCode, wasmBinary]) => {
const script = document.createElement("script");
script.onload = () => {
const basisModule = {
wasmBinary
};
// @ts-ignore
BASIS(basisModule)
.then((Module: any) => {
Module.initializeBasis();
resolve(Module);
})
.catch(reject);
};
script.src = URL.createObjectURL(new Blob([basisEncoderCode]));
script.onerror = reject;
document.body.appendChild(script);
})
.catch(reject);
} else {
importScripts(
"https://mdn.alipayobjects.com/rms/afts/file/A*SrRkQarYYl4AAAAAAAAAAAAAARQnAQ/basis_encoder.js"
);
fetch("https://mdn.alipayobjects.com/rms/afts/file/A*qFWbTrA0hZYAAAAAAAAAAAAAARQnAQ/basis_encoder.wasm")
.then((res) => res.arrayBuffer())
.then((wasmBinary) => {
const basisModule = {
wasmBinary
};
// @ts-ignore
BASIS(basisModule)
.then((Module: any) => {
Module.initializeBasis();
resolve(Module);
})
.catch(reject);
})
.catch(reject);
}
Promise.all([
options?.jsUrl ? import(/* @vite-ignore */ options.jsUrl) : import("../basis/basis_encoder.js"),
options?.wasmUrl ? fetch(options.wasmUrl).then((res) => res.arrayBuffer()) : undefined
])
.then(([{ default: BASIS }, wasmBinary]) => {
return BASIS({ wasmBinary }).then((Module: IBasisModule) => {
Module.initializeBasis();
resolve(Module);
});
})
.catch(reject);
});
}
promise = _init();
Expand All @@ -69,7 +31,7 @@ class BrowserBasisEncoder {
}

encode(imageBuffer: Uint8Array, options: Partial<IEncodeOptions> = {}) {
return this.init().then((basisModule) => {
return this.init(options).then((basisModule) => {
const encoder = new basisModule.BasisEncoder();
applyInputOptions(options, encoder);
encoder.setTexType(BasisTextureType.cBASISTexType2D);
Expand Down
3 changes: 2 additions & 1 deletion test/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ test("uastc", async () => {
isUASTC: true,
enableDebug: false,
qualityLevel: 230,
generateMipmap: true
generateMipmap: true,
wasmUrl: "/basis_encoder.wasm"
});

const resultBuffer = await fetch("/tests/DuckCM-uastc.ktx2").then((res) => res.arrayBuffer());
Expand Down
2 changes: 0 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
"module": "NodeNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "NodeNext",
"allowImportingTsExtensions": false,
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",

/* Linting */
"strict": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
Expand Down
14 changes: 1 addition & 13 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,5 @@ export default defineConfig({
}
}
},
test: {
deps: {
interopDefault: true,
optimizer: {
web: { include: ["./src/node/basis/basis_encoder.cjs"] }
}
},
server: {
deps: {
fallbackCJS: true
}
}
}
test: {}
});

0 comments on commit 55e41d4

Please sign in to comment.