diff --git a/components/clarinet-sdk/package.json b/components/clarinet-sdk/package.json index e0ca31853..8f882ed62 100644 --- a/components/clarinet-sdk/package.json +++ b/components/clarinet-sdk/package.json @@ -1,6 +1,6 @@ { "name": "obscurity-sdk", - "version": "0.2.0", + "version": "0.3.0", "description": "clarity testing lib", "repository": { "type": "git", @@ -9,10 +9,12 @@ "files": [ "dist" ], + "main": "dist/cjs/index.js", "module": "dist/esm/index.mjs", "exports": { ".": { - "import": "./dist/esm/index.mjs" + "import": "./dist/esm/index.mjs", + "require": "./dist/cjs/index.js" } }, "scripts": { diff --git a/components/clarinet-sdk/src-ts/index.ts b/components/clarinet-sdk/src-ts/index.ts index 897ca8c48..0c99dcade 100644 --- a/components/clarinet-sdk/src-ts/index.ts +++ b/components/clarinet-sdk/src-ts/index.ts @@ -2,9 +2,10 @@ import { Cl, ClarityValue } from "@stacks/transactions"; import { vfs } from "./vfs"; import type { ContractInterface } from "./contractInterface"; -import { SDK, CallContractArgs } from "./sdk"; -const rustSDK = import("./sdk"); +import type { SDK } from "./sdk"; +type WASMModule = typeof import("./sdk"); +const wasmModule = import("./sdk"); type CallFn = ( contract: string, @@ -20,9 +21,7 @@ type GetMapEntry = (contract: string, mapName: string, mapKey: ClarityValue) => type GetAssetsMap = () => Map>; type GetAccounts = () => Map; -// because we use a proxy around the test session, -// the type need to be hardcoded -// is there a better way than this nested ternary? +// because the session is wrapped in a proxy the types need to be hardcoded export type ClarityVM = { [K in keyof SDK]: K extends "callReadOnlyFn" | "callPublicFn" ? CallFn @@ -39,7 +38,7 @@ export type ClarityVM = { : SDK[K]; }; -const sessionProxy = { +const getSessionProxy = (wasm: WASMModule) => ({ get(session: SDK, prop: keyof SDK, receiver: any) { // some of the WASM methods are proxied here to: // - serialize clarity values input argument @@ -48,7 +47,7 @@ const sessionProxy = { if (prop === "callReadOnlyFn" || prop === "callPublicFn") { const callFn: CallFn = (contract, method, args, sender) => { const response = session[prop]( - new CallContractArgs( + new wasm.CallContractArgs( contract, method, args.map((a) => Cl.serialize(a)) @@ -90,16 +89,26 @@ const sessionProxy = { return Reflect.get(session, prop, receiver); }, -}; +}); // load wasm only once and memoize it function memoizedInit() { let vm: ClarityVM | null = null; return async (manifestPath = "./Clarinet.toml") => { - await rustSDK; + const module = await wasmModule; + + // handle both CJS and ESM context + // in CJS: `module.default` is a promise resolving + // in ESM: `module` is directly the + // @ts-ignore + let wasm: WASMModule = + typeof module.default === "undefined" + ? (module as unknown as WASMModule) + : await module.default; + if (!vm) { console.log("init clarity vm"); - vm = new Proxy(new SDK(vfs), sessionProxy) as unknown as ClarityVM; + vm = new Proxy(new wasm.SDK(vfs), getSessionProxy(wasm)) as unknown as ClarityVM; } // start a new session await vm.initSession(process.cwd(), manifestPath); diff --git a/components/clarinet-sdk/tsconfig.base.json b/components/clarinet-sdk/tsconfig.base.json index 353784170..9b26c41c8 100644 --- a/components/clarinet-sdk/tsconfig.base.json +++ b/components/clarinet-sdk/tsconfig.base.json @@ -2,7 +2,7 @@ "compilerOptions": { // outdir, module and target are defined in esm and cjs configs - "lib": ["es6"], + "lib": ["es2022"], "moduleResolution": "Node", "rootDir": "src-ts", diff --git a/components/clarinet-sdk/webpack.config.js b/components/clarinet-sdk/webpack.config.js index 6e514fbcf..222360e09 100644 --- a/components/clarinet-sdk/webpack.config.js +++ b/components/clarinet-sdk/webpack.config.js @@ -75,11 +75,10 @@ const configCJS = { plugins: [ new WasmPackPlugin({ crateDirectory: path.resolve(__dirname, "./"), - extraArgs: "--release --target=web", + extraArgs: "--release --target=bundler", outDir: path.resolve(__dirname, "./src-ts/sdk"), }), ], }; -module.exports = [configESM]; -// module.exports = [configESM, configCJS]; +module.exports = [configESM, configCJS];