Skip to content

Commit

Permalink
chore: handle cjs and esm
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard committed Aug 10, 2023
1 parent c7370c6 commit ec11cf3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
6 changes: 4 additions & 2 deletions components/clarinet-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obscurity-sdk",
"version": "0.2.0",
"version": "0.3.0",
"description": "clarity testing lib",
"repository": {
"type": "git",
Expand All @@ -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": {
Expand Down
29 changes: 19 additions & 10 deletions components/clarinet-sdk/src-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,9 +21,7 @@ type GetMapEntry = (contract: string, mapName: string, mapKey: ClarityValue) =>
type GetAssetsMap = () => Map<string, Map<string, bigint>>;
type GetAccounts = () => Map<string, string>;

// 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
Expand All @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-sdk/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions components/clarinet-sdk/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

0 comments on commit ec11cf3

Please sign in to comment.