|
1 | 1 | # cedar-wasm
|
2 | 2 |
|
3 | 3 | An implementation of various cedar functions to enable developers to write typescript and javascript applications using Cedar and wasm.
|
| 4 | + |
| 5 | +## Installing |
| 6 | + |
| 7 | +Installing is simple, just run `npm i @cedar-policy/cedar-wasm` or install with whatever your favorite package manager is. |
| 8 | + |
| 9 | + |
| 10 | +## Loading in webpack 5: |
| 11 | + |
| 12 | +Minimal package.json for webpack including dev server: |
| 13 | + |
| 14 | +``` |
| 15 | +{ |
| 16 | + "name": "webpack-ts-tester", |
| 17 | + "version": "1.0.0", |
| 18 | + "description": "", |
| 19 | + "private": true, |
| 20 | + "scripts": { |
| 21 | + "test": "echo \"Error: no test specified\" && exit 1", |
| 22 | + "build": "webpack", |
| 23 | + "dev": "webpack serve" |
| 24 | + }, |
| 25 | + "keywords": [], |
| 26 | + "author": "", |
| 27 | + "license": "ISC", |
| 28 | + "dependencies": { |
| 29 | + "@cedar-policy/cedar-wasm": "3.2.0" |
| 30 | + }, |
| 31 | + "devDependencies": { |
| 32 | + "ts-loader": "^9.5.1", |
| 33 | + "typescript": "^5.4.5", |
| 34 | + "webpack": "^5.91.0", |
| 35 | + "webpack-cli": "^5.1.4", |
| 36 | + "webpack-dev-server": "^5.0.4" |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Minimal tsconfig: |
| 42 | + |
| 43 | +``` |
| 44 | +{ |
| 45 | + "compilerOptions": { |
| 46 | + "outDir": "./dist/", |
| 47 | + "noImplicitAny": true, |
| 48 | + "module": "es2020", |
| 49 | + "target": "es5", |
| 50 | + "jsx": "react", |
| 51 | + "allowJs": true, |
| 52 | + "moduleResolution": "node" |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Configure webpack.config.js: |
| 58 | + |
| 59 | +``` |
| 60 | +const path = require('path'); |
| 61 | +
|
| 62 | +module.exports = { |
| 63 | + mode: 'development', // change this to suit you |
| 64 | + entry: './src/index.ts', |
| 65 | + module: { |
| 66 | + rules: [ |
| 67 | + { |
| 68 | + test: /\.tsx?$/, |
| 69 | + use: 'ts-loader', |
| 70 | + exclude: /node_modules/, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + resolve: { |
| 75 | + extensions: ['.tsx', '.ts', '.js'], |
| 76 | + }, |
| 77 | + output: { |
| 78 | + filename: 'bundle.js', |
| 79 | + path: path.resolve(__dirname, 'dist'), |
| 80 | + }, |
| 81 | + experiments: { |
| 82 | + asyncWebAssembly: true, // enables wasm support in webpack |
| 83 | + }, |
| 84 | + devServer: { |
| 85 | + static: { |
| 86 | + directory: path.join(__dirname, 'dist'), |
| 87 | + }, |
| 88 | + compress: true, |
| 89 | + port: 8000, |
| 90 | + } |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +Finally, load the code from your `index.ts` file. We recommend dynamic imports: |
| 95 | + |
| 96 | +``` |
| 97 | +import('@cedar-policy/cedar-wasm').then(mod => { |
| 98 | + // cache it globally here or invoke functions like mod.getCedarVersion(); |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +## Loading in vite 5: |
| 105 | + |
| 106 | +Starting from the vite typescript template, install these two dependencies to enable wasm: |
| 107 | + |
| 108 | +``` |
| 109 | +npm i --save-dev vite-plugin-top-level-await vite-plugin-wasm |
| 110 | +``` |
| 111 | + |
| 112 | +Then add those two plugins to your vite config in `vite.config.js`: |
| 113 | + |
| 114 | +``` |
| 115 | +import wasm from 'vite-plugin-wasm'; |
| 116 | +import topLevelAwait from 'vite-plugin-top-level-await'; |
| 117 | +import { defineConfig } from 'vite'; |
| 118 | +
|
| 119 | +export default defineConfig({ |
| 120 | + plugins: [ |
| 121 | + wasm(), |
| 122 | + topLevelAwait() |
| 123 | + ] |
| 124 | +}); |
| 125 | +
|
| 126 | +``` |
| 127 | + |
| 128 | +Finally, load the code. We recommend dynamic imports: |
| 129 | + |
| 130 | +``` |
| 131 | +import('@cedar-policy/cedar-wasm').then(mod => { |
| 132 | + // cache it globally here or invoke functions like mod.getCedarVersion(); |
| 133 | +}); |
| 134 | +``` |
| 135 | + |
0 commit comments