Skip to content

Commit d8e9bd1

Browse files
committed
feat: use wasm-pack and add shims
1 parent f85fcbe commit d8e9bd1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+60789
-115292
lines changed

build.ts

+26-93
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as dnt from "https://deno.land/x/[email protected]/mod.ts";
2-
import * as esbuild from "https://deno.land/x/[email protected]/mod.js";
2+
import { copySync } from "https://deno.land/[email protected]/fs/copy.ts";
3+
import { ensureDirSync } from "https://deno.land/[email protected]/fs/ensure_dir.ts";
4+
// import { build } from "https://deno.land/x/[email protected]/mod.js";
35
import packageInfo from "./package.json" assert { type: "json" };
46

57
await dnt.emptyDir("./dist");
@@ -18,7 +20,11 @@ await dnt.build({
1820
scriptModule: false,
1921
typeCheck: false,
2022
skipSourceOutput: true,
21-
shims: {},
23+
shims: {
24+
webSocket: true,
25+
undici: true,
26+
crypto: true,
27+
},
2228
package: {
2329
...packageInfo
2430
},
@@ -27,101 +33,28 @@ await dnt.build({
2733
Deno.copyFileSync("LICENSE", "dist/LICENSE");
2834
Deno.copyFileSync("README.md", "dist/README.md");
2935

30-
// Copy WebAssembly
31-
32-
Deno.copyFileSync(
33-
"src/core/libs/cardano_multiplatform_lib/cardano_multiplatform_lib_bg.wasm",
34-
"dist/esm/src/core/libs/cardano_multiplatform_lib/cardano_multiplatform_lib_bg.wasm",
35-
);
36-
Deno.copyFileSync(
37-
"src/core/libs/cardano_message_signing/cardano_message_signing_bg.wasm",
38-
"dist/esm/src/core/libs/cardano_message_signing/cardano_message_signing_bg.wasm",
39-
);
40-
41-
//** Web ES Module */
36+
// Copy WASM Pack files
4237

43-
const importPathPlugin = {
44-
name: "core-import-path",
45-
setup(build: any) {
46-
build.onResolve({
47-
filter:
48-
/^\.\/libs\/cardano_multiplatform_lib\/cardano_multiplatform_lib.generated.js$/,
49-
}, (args: any) => {
50-
return {
51-
path:
52-
"../esm/src/core/libs/cardano_multiplatform_lib/cardano_multiplatform_lib.generated.js",
53-
external: true,
54-
};
55-
});
56-
build.onResolve({
57-
filter:
58-
/^\.\/libs\/cardano_message_signing\/cardano_message_signing.generated.js$/,
59-
}, (args: any) => {
60-
return {
61-
path:
62-
"../esm/src/core/libs/cardano_message_signing/cardano_message_signing.generated.js",
63-
external: true,
64-
};
65-
});
66-
},
67-
};
68-
69-
// await esbuild.build({
70-
// bundle: true,
71-
// format: "esm",
72-
// entryPoints: ["./dist/esm/mod.js"],
73-
// outfile: "./dist/web/mod.js",
74-
// minify: true,
75-
// plugins: [
76-
// importPathPlugin,
77-
// ],
78-
// });
79-
// esbuild.stop();
38+
for (const pkg of ["cardano_message_signing", "cardano_multiplatform_lib"]) {
39+
const files = Deno.readDirSync(`src/core/libs/${pkg}/pkg`);
8040

81-
// /** Add necessary global import statements to NPM ES Module. */
82-
const coreFile = `
83-
const C = await (async () => {
84-
try {
85-
return await import(
86-
/* webpackIgnore: true */ "./libs/cardano_multiplatform_lib/nodejs/cardano_multiplatform_lib.generated.js"
87-
);
88-
} catch (_e) {
89-
// This only ever happens during SSR rendering
90-
return null;
91-
}
92-
})();
93-
const M = await (async () => {
94-
try {
95-
return await import(
96-
/* webpackIgnore: true */ "./libs/cardano_message_signing/nodejs/cardano_message_signing.generated.js"
97-
);
98-
} catch (_e) {
99-
// This only ever happens during SSR rendering
100-
return null;
41+
for (const file of files) {
42+
if (file.isFile && !file.name.endsWith(".js")) {
43+
copySync(
44+
`src/core/libs/${pkg}/pkg/${file.name}`,
45+
`dist/esm/src/core/libs/${pkg}/pkg/${file.name}`,
46+
{ overwrite: true }
47+
);
48+
}
10149
}
102-
})();
103-
export { C, M };
104-
`;
105-
Deno.writeTextFileSync("dist/esm/src/core/core.js", coreFile);
10650

107-
Deno.mkdirSync("dist/esm/src/core/libs/cardano_message_signing/nodejs");
108-
Deno.mkdirSync("dist/esm/src/core/libs/cardano_multiplatform_lib/nodejs");
51+
const packageJson = JSON.parse(Deno.readTextFileSync(`src/core/libs/${pkg}/pkg/package.json`));
52+
packageJson.type = "commonjs";
10953

110-
Deno.copyFileSync(
111-
"src/core/libs/cardano_message_signing/nodejs/cardano_message_signing.generated.js",
112-
"dist/esm/src/core/libs/cardano_message_signing/nodejs/cardano_message_signing.generated.js",
113-
);
54+
Deno.writeTextFile(
55+
`dist/esm/src/core/libs/${pkg}/pkg/package.json`,
56+
JSON.stringify(packageJson, null, 2),
57+
);
58+
}
11459

115-
Deno.copyFileSync(
116-
"src/core/libs/cardano_multiplatform_lib/nodejs/cardano_multiplatform_lib.generated.js",
117-
"dist/esm/src/core/libs/cardano_multiplatform_lib/nodejs/cardano_multiplatform_lib.generated.js",
118-
);
11960

120-
Deno.writeTextFile(
121-
"dist/esm/src/core/libs/cardano_message_signing/nodejs/package.json",
122-
JSON.stringify({ type: "commonjs" }),
123-
);
124-
Deno.writeTextFile(
125-
"dist/esm/src/core/libs/cardano_multiplatform_lib/nodejs/package.json",
126-
JSON.stringify({ type: "commonjs" }),
127-
);

deno.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"tasks": {
3-
"build": "deno run --allow-env --allow-write --allow-read --allow-net --allow-run build.ts",
4-
"build:core": "cd src/core/libs/cardano_multiplatform_lib && deno run -A https://raw.githubusercontent.com/spacebudz/wasmbuild/a0fec73985b0f0a9120b5077e292963d513795d4/main.ts --out ./; cd ../cardano_message_signing && deno run -A https://raw.githubusercontent.com/spacebudz/wasmbuild/a0fec73985b0f0a9120b5077e292963d513795d4/main.ts --out ./",
3+
"build": "NODE_ENV=production deno run --allow-env --allow-write --allow-read --allow-net --allow-run build.ts",
4+
"build:core": "npm run pack -- src/core/libs/cardano_multiplatform_lib; npm run pack -- src/core/libs/cardano_message_signing",
55
"publish": "deno task build && deno task test && npm publish dist",
66
"test": "deno test --allow-env --allow-read --allow-net tests",
77
"test:core": "cd src/core/libs/cardano_multiplatform_lib && cargo test; cd ../cardano_message_signing && cargo test"

deno.lock

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/esm/_dnt.shims.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export { crypto, type Crypto, type SubtleCrypto, type AlgorithmIdentifier, type Algorithm, type RsaOaepParams, type BufferSource, type AesCtrParams, type AesCbcParams, type AesGcmParams, type CryptoKey, type KeyAlgorithm, type KeyType, type KeyUsage, type EcdhKeyDeriveParams, type HkdfParams, type HashAlgorithmIdentifier, type Pbkdf2Params, type AesDerivedKeyParams, type HmacImportParams, type JsonWebKey, type RsaOtherPrimesInfo, type KeyFormat, type RsaHashedKeyGenParams, type RsaKeyGenParams, type BigInteger, type EcKeyGenParams, type NamedCurve, type CryptoKeyPair, type AesKeyGenParams, type HmacKeyGenParams, type RsaHashedImportParams, type EcKeyImportParams, type AesKeyAlgorithm, type RsaPssParams, type EcdsaParams } from "@deno/shim-crypto";
2+
export { fetch, File, FormData, Headers, Request, Response, type BodyInit, type HeadersInit, type RequestInit, type ResponseInit } from "undici";
3+
export { default as WebSocket } from "ws";
4+
export declare const dntGlobalThis: Omit<typeof globalThis, "crypto" | "fetch" | "File" | "FormData" | "Headers" | "Request" | "Response" | "WebSocket"> & {
5+
crypto: any;
6+
fetch: any;
7+
File: any;
8+
FormData: any;
9+
Headers: any;
10+
Request: any;
11+
Response: any;
12+
WebSocket: any;
13+
};

0 commit comments

Comments
 (0)