Skip to content

Commit

Permalink
feature: inline wasm in npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Sep 21, 2023
1 parent d49e98a commit c81a543
Show file tree
Hide file tree
Showing 12 changed files with 928 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions nucypher-core-wasm-bundler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
*log
675 changes: 675 additions & 0 deletions nucypher-core-wasm-bundler/LICENSE

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions nucypher-core-wasm-bundler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# nucypher-core-wasm-bundler

```bash
yarn install
yarn build
yarn build:minify
```
32 changes: 32 additions & 0 deletions nucypher-core-wasm-bundler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@nucypher/nucypher-core-wasm",
"license": "GPL-3.0-only",
"sideEffects": false,
"type": "module",
"main": "./dist/umd/index.js",
"module": "./dist/es/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/es/index.js",
"default": "./dist/cjs/index.cjs"
}
},
"files": [
"dist"
],
"scripts": {
"build": "wasm-pack build -t web --out-dir ../nucypher-core-wasm-bundler/src/pkg ../nucypher-core-wasm && rm -rf dist/ && rollup -c",
"build:minify": "npm run build && npx terser@latest --compress --mangle --output dist/cjs/index.cjs -- dist/cjs/index.cjs",
"format": "npx prettier@latest --write src/ tests/ package.json rollup.config.js tsconfig.json vite.config.ts cli.js"
},
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.3",
"@rollup/plugin-wasm": "^6.1.3",
"@types/node": "^20.5.6",
"rollup": "^3.29.2",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
}
}
63 changes: 63 additions & 0 deletions nucypher-core-wasm-bundler/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import typescript from "@rollup/plugin-typescript";
import { wasm } from "@rollup/plugin-wasm";
import path from "path";
import fs from "fs";

const outDir = (fmt, env) => {
if (env === "node") {
return `node`;
} else {
return `${fmt}${env === "slim" ? "-slim" : ""}`;
}
};

const rolls = (fmt, env) => ({
input: env !== "slim" ? "src/index.ts" : "src/index_slim.ts",
output: {
dir: `dist`,
format: fmt,
entryFileNames:
outDir(fmt, env) + `/[name].` + (fmt === "cjs" ? "cjs" : "js"),
name: "@nucypher/nucypher-core-wasm",
},
plugins: [
env !== "slim" &&
wasm(
env === "node"
? {
maxFileSize: 0,
targetEnv: "node",
publicPath: "../",
fileName: "[name][extname]",
}
: { targetEnv: "auto-inline" }
),
typescript({
target: fmt === "es" ? "ES2022" : "ES2017",
outDir: `dist/${outDir(fmt, env)}`,
rootDir: "src",
}),
{
name: "copy-pkg",
resolveImportMeta: () => `""`,
generateBundle() {
fs.mkdirSync(`./dist/types/pkg`, { recursive: true });
fs.copyFileSync(
path.resolve("./src/pkg/nucypher_core_wasm.d.ts"),
path.resolve(`./dist/types/pkg/nucypher_core_wasm.d.ts`)
);
},
},
],
});

export default [
rolls("umd", "fat"),
rolls("es", "fat"),
rolls("cjs", "fat"),
// TODO: No support a separate node build ATM
// rolls("cjs", "node"),
// TODO: Slim build is not supported ATM
// rolls("es", "slim"),
// rolls("cjs", "slim"),
];
4 changes: 4 additions & 0 deletions nucypher-core-wasm-bundler/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { initialize } from "./wasm";

export { initialize };
export * from "./index_core.js";
1 change: 1 addition & 0 deletions nucypher-core-wasm-bundler/src/index_core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./pkg/nucypher_core_wasm.js";
1 change: 1 addition & 0 deletions nucypher-core-wasm-bundler/src/index_slim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./index_core.js";
17 changes: 17 additions & 0 deletions nucypher-core-wasm-bundler/src/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import wasm from "./pkg/nucypher_core_wasm_bg.wasm";
import init, { InitInput } from "./pkg/nucypher_core_wasm.js";

let initialized: Promise<void> | undefined = undefined;
export const initialize = async (maybeWasm?: InitInput) => {
if (initialized === undefined) {
// We're using a Promise to make sure that the WASM module is properly recognized by the JS code
// generated by wasm-pack:
// V here
// const { instance, module } = await __wbg_load(await input, imports);
// Ignoring "expression not callable" error
// @ts-ignore
const wasmPromise = Promise.resolve(maybeWasm ? maybeWasm : wasm());
initialized = init(wasmPromise).then(() => void 0);
}
await initialized;
};
17 changes: 17 additions & 0 deletions nucypher-core-wasm-bundler/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"include": ["src", "tests"],
"compilerOptions": {
"target": "ES2022",
"noEmit": true,
"module": "ES2022",
"lib": ["dom", "ES2022"],
"declaration": true,
"declarationDir": "dist/types",
"strict": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
105 changes: 105 additions & 0 deletions nucypher-core-wasm-bundler/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@rollup/plugin-typescript@^11.1.3":
version "11.1.3"
resolved "https://registry.yarnpkg.com/@rollup/plugin-typescript/-/plugin-typescript-11.1.3.tgz#8172858a1e5f4c181aebc61f8920002fd5e04b91"
integrity sha512-8o6cNgN44kQBcpsUJTbTXMTtb87oR1O0zgP3Dxm71hrNgparap3VujgofEilTYJo+ivf2ke6uy3/E5QEaiRlDA==
dependencies:
"@rollup/pluginutils" "^5.0.1"
resolve "^1.22.1"

"@rollup/plugin-wasm@^6.1.3":
version "6.1.3"
resolved "https://registry.yarnpkg.com/@rollup/plugin-wasm/-/plugin-wasm-6.1.3.tgz#8d26a320780b15bf89d8d266ceda50823d5f978b"
integrity sha512-7ItTTeyauE6lwdDtQWceEHZ9+txbi4RRy0mYPFn9BW7rD7YdgBDu7HTHsLtHrRzJc313RM/1m6GKgV3np/aEaw==

"@rollup/pluginutils@^5.0.1":
version "5.0.4"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-5.0.4.tgz#74f808f9053d33bafec0cc98e7b835c9667d32ba"
integrity sha512-0KJnIoRI8A+a1dqOYLxH8vBf8bphDmty5QvIm2hqm7oFCFYKCAZWWd2hXgMibaPsNDhI0AtpYfQZJG47pt/k4g==
dependencies:
"@types/estree" "^1.0.0"
estree-walker "^2.0.2"
picomatch "^2.3.1"

"@types/estree@^1.0.0":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.1.tgz#aa22750962f3bf0e79d753d3cc067f010c95f194"
integrity sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==

"@types/node@^20.5.6":
version "20.6.3"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.6.3.tgz#5b763b321cd3b80f6b8dde7a37e1a77ff9358dd9"
integrity sha512-HksnYH4Ljr4VQgEy2lTStbCKv/P590tmPe5HqOnv9Gprffgv5WXAY+Y5Gqniu0GGqeTCUdBnzC3QSrzPkBkAMA==

estree-walker@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac"
integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==

fsevents@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6"
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==

function-bind@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d"
integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==

has@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/has/-/has-1.0.3.tgz#722d7cbfc1f6aa8241f16dd814e011e1f41e8796"
integrity sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==
dependencies:
function-bind "^1.1.1"

is-core-module@^2.13.0:
version "2.13.0"
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.13.0.tgz#bb52aa6e2cbd49a30c2ba68c42bf3435ba6072db"
integrity sha512-Z7dk6Qo8pOCp3l4tsX2C5ZVas4V+UxwQodwZhLopL91TX8UyyHEXafPcyoeeWuLrwzHcr3igO78wNLwHJHsMCQ==
dependencies:
has "^1.0.3"

path-parse@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==

picomatch@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==

resolve@^1.22.1:
version "1.22.6"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.6.tgz#dd209739eca3aef739c626fea1b4f3c506195362"
integrity sha512-njhxM7mV12JfufShqGy3Rz8j11RPdLy4xi15UurGJeoHLfJpVXKdh3ueuOqbYUcDZnffr6X739JBo5LzyahEsw==
dependencies:
is-core-module "^2.13.0"
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"

rollup@^3.29.2:
version "3.29.2"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.2.tgz#cbc76cd5b03b9f9e93be991d23a1dff9c6d5b740"
integrity sha512-CJouHoZ27v6siztc21eEQGo0kIcE5D1gVPA571ez0mMYb25LGYGKnVNXpEj5MGlepmDWGXNjDB5q7uNiPHC11A==
optionalDependencies:
fsevents "~2.3.2"

supports-preserve-symlinks-flag@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==

tslib@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae"
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==

typescript@^5.2.2:
version "5.2.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78"
integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==

0 comments on commit c81a543

Please sign in to comment.