Skip to content

Commit 5ba0e95

Browse files
SwolebrainVictor Moreno
andauthored
improved wasm build process (#933)
Co-authored-by: Victor Moreno <[email protected]>
1 parent d8a9591 commit 5ba0e95

File tree

3 files changed

+100
-27
lines changed

3 files changed

+100
-27
lines changed

cedar-wasm/README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ An implementation of various cedar functions to enable developers to write types
44

55
## Installing
66

7-
Installing is simple, just run `npm i @cedar-policy/cedar-wasm` or install with whatever your favorite package manager is.
8-
7+
Installing is simple, just run `npm i @cedar-policy/cedar-wasm --save` or install with whatever your favorite package manager is.
98

109
## Loading in webpack 5:
1110

@@ -133,3 +132,20 @@ import('@cedar-policy/cedar-wasm').then(mod => {
133132
});
134133
```
135134

135+
## Alternate loading strategies
136+
137+
If for some reason you cannot use es modules, we provide alternate sub-packages `web` and `nodejs` (defined as `exports` in the root package.json).
138+
139+
The `nodejs` subpackage uses `fs` and CommonJS modules. To use it, you can import it like so:
140+
141+
```
142+
const cedar = require('@cedar-policy/cedar-wasm/nodejs')
143+
```
144+
145+
The `web` subpackage exposes an `initSync` function that you can use to load Cedar in scenarios where you want to load the wasm binary async for whatever reason. Using the `web` subpackage may also be necessary with some `jest` setups. Here's how you use the `web` subpackage:
146+
147+
```
148+
const wasmBuffer = ... // `fetch` it or use `fs` to read it from `node_modules` in jest setupTests
149+
import * as cedarJsBindings from '@cedar-policy/cedar-wasm/web';
150+
cedarJsBindings.initSync(wasmBuffer);
151+
```

cedar-wasm/build-wasm.sh

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,67 @@
1414
# limitations under the License.
1515

1616
# This script calls wasm-pack build and post-processes the generated TS types to fix them.
17+
# It also produces three sets of outputs for different needs of different consumers
1718
# Without this, the built wasm still works, but the Typescript definitions made by tsify don't.
1819
set -e
19-
cargo build
20-
wasm-pack build --scope cedar-policy --target bundler
21-
22-
sed -i "s/[{]\s*!: /{ \"!\": /g" pkg/cedar_wasm.d.ts
23-
sed -i "s/[{]\s*==: /{ \"==\": /g" pkg/cedar_wasm.d.ts
24-
sed -i "s/[{]\s*!=: /{ \"!=\": /g" pkg/cedar_wasm.d.ts
25-
sed -i "s/[{]\s*<: /{ \"<\": /g" pkg/cedar_wasm.d.ts
26-
sed -i "s/[{]\s*<=: /{ \"<=\": /g" pkg/cedar_wasm.d.ts
27-
sed -i "s/[{]\s*>: /{ \">\": /g" pkg/cedar_wasm.d.ts
28-
sed -i "s/[{]\s*>=: /{ \">=\": /g" pkg/cedar_wasm.d.ts
29-
sed -i "s/[{]\s*&&: /{ \"\&\&\": /g" pkg/cedar_wasm.d.ts
30-
sed -i "s/[{]\s*||: /{ \"||\": /g" pkg/cedar_wasm.d.ts
31-
sed -i "s/[{]\s*[+]: /{ \"+\": /g" pkg/cedar_wasm.d.ts
32-
sed -i "s/[{]\s*-: /{ \"-\": /g" pkg/cedar_wasm.d.ts
33-
sed -i "s/[{]\s*[*]: /{ \"*\": /g" pkg/cedar_wasm.d.ts
34-
sed -i "s/[{]\s*\.: /{ \".\": /g" pkg/cedar_wasm.d.ts
35-
sed -i "s/ | __skip//g" pkg/cedar_wasm.d.ts
36-
sed -i "s/SchemaFragment/Schema/g" pkg/cedar_wasm.d.ts
37-
38-
echo "type SmolStr = string;" >> pkg/cedar_wasm.d.ts
39-
echo "type Name = string;" >> pkg/cedar_wasm.d.ts
40-
echo "type Id = string;" >> pkg/cedar_wasm.d.ts
41-
echo "export type TypeOfAttribute = SchemaType & { required?: boolean };" >> pkg/cedar_wasm.d.ts
42-
echo "export type Context = Record<string, CedarValueJson>;" >> pkg/cedar_wasm.d.ts
43-
echo "Finished post-processing types file"
20+
main () {
21+
rm -rf pkg || true
22+
mkdir pkg
23+
cargo build
24+
wasm-pack build --scope cedar-policy --target bundler --out-dir pkg/esm
25+
wasm-pack build --scope cedar-policy --target nodejs --out-dir pkg/nodejs
26+
wasm-pack build --scope cedar-policy --target web --out-dir pkg/web
27+
cp pkg/esm/README.md pkg/README.md
28+
29+
fix_package_json_files
30+
31+
process_types_file "pkg/esm/cedar_wasm.d.ts"
32+
process_types_file "pkg/nodejs/cedar_wasm.d.ts"
33+
process_types_file "pkg/web/cedar_wasm.d.ts"
34+
}
35+
36+
fix_package_json_files() {
37+
jq -s '.[0] * .[1]' pkg/esm/package.json package.json.patch > pkg/package.json
38+
echo "Created root package.json"
39+
mv pkg/esm/package.json pkg/esm/package.json.bak
40+
mv pkg/web/package.json pkg/web/package.json.bak
41+
mv pkg/nodejs/package.json pkg/nodejs/package.json.bak
42+
jq '. + {"type": "module"}' pkg/esm/package.json.bak > pkg/esm/package.json
43+
jq '. + {"type": "module"}' pkg/web/package.json.bak > pkg/web/package.json
44+
jq '. + {"type": "commonjs"}' pkg/nodejs/package.json.bak > pkg/nodejs/package.json
45+
rm pkg/esm/package.json.bak
46+
rm pkg/web/package.json.bak
47+
rm pkg/nodejs/package.json.bak
48+
echo "Patched sub-package json files"
49+
}
50+
51+
process_types_file() {
52+
local types_file="$1"
53+
echo "processing types file: $1"
54+
sed -i "s/[{]\s*!: /{ \"!\": /g" "$types_file"
55+
sed -i "s/[{]\s*==: /{ \"==\": /g" "$types_file"
56+
sed -i "s/[{]\s*!=: /{ \"!=\": /g" "$types_file"
57+
sed -i "s/[{]\s*<: /{ \"<\": /g" "$types_file"
58+
sed -i "s/[{]\s*<=: /{ \"<=\": /g" "$types_file"
59+
sed -i "s/[{]\s*>: /{ \">\": /g" "$types_file"
60+
sed -i "s/[{]\s*>=: /{ \">=\": /g" "$types_file"
61+
sed -i "s/[{]\s*&&: /{ \"\&\&\": /g" "$types_file"
62+
sed -i "s/[{]\s*||: /{ \"||\": /g" "$types_file"
63+
sed -i "s/[{]\s*[+]: /{ \"+\": /g" "$types_file"
64+
sed -i "s/[{]\s*-: /{ \"-\": /g" "$types_file"
65+
sed -i "s/[{]\s*[*]: /{ \"*\": /g" "$types_file"
66+
sed -i "s/[{]\s*\.: /{ \".\": /g" "$types_file"
67+
sed -i "s/ | __skip//g" "$types_file"
68+
sed -i "s/SchemaFragment/SchemaJson/g" "$types_file"
69+
sed -i "s/[{] json: JsonValueWithNoDuplicateKeys /{ json: SchemaJson /g" "$types_file"
70+
71+
echo "type SmolStr = string;" >> "$types_file"
72+
echo "type Name = string;" >> "$types_file"
73+
echo "type Id = string;" >> "$types_file"
74+
echo "export type TypeOfAttribute = SchemaType & { required?: boolean };" >> "$types_file"
75+
echo "export type Context = Record<string, CedarValueJson>;" >> "$types_file"
76+
}
77+
78+
79+
main
80+
echo "Finished custom build script"

cedar-wasm/package.json.patch

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"files": ["esm/*", "nodejs/*", "web/*"],
3+
"sideEffects": ["./snippets/*"],
4+
"module": "esm/cedar_wasm.js",
5+
"types": "esm/cedar_wasm.d.ts",
6+
"exports": {
7+
".": {
8+
"import": "esm/cedar_wasm.js",
9+
"types": "esm/cedar_wasm.d.ts"
10+
},
11+
"./nodejs": {
12+
"import": "nodejs/cedar_wasm.js",
13+
"types": "nodejs/cedar_wasm.d.ts"
14+
},
15+
"./web": {
16+
"import": "web/cedar_wasm.js",
17+
"types": "web/cedar_wasm.d.ts"
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)