雪松树
执行各种雪松功能,使开发者能够使用Cedar和ham编写打字和javascript应用程序.
安装
安装很简单,只是运行 npm i @cedar-policy/cedar-wasm --save 或者安装任何你最喜欢的软件包管理器.
装入要复杂得多 这取决于你的环境。 我们提供三个子包:
- es 模块( 默认) 。 它装入的方式是,如果使用动态导入,它将被捆绑成一个文件,或者如果使用常规导入,它将被嵌入到主捆.
- commonjs(用于节点). 用节点装入的
fs模块,同步。 并非是用来捆绑或传送到浏览器的. - 网:更自定义. 这个是当你需要装入 以某种完全定制的方式。 详见"换装策略"部分.
这些子包被命名为 @cedar-policy/cedar-wasm, (中文(简体) ), @cedar-policy/cedar-wasm/nodejs,以及 @cedar-policy/cedar-wasm/web分别是:.
在没有捆绑器的情况下装入裸节点J
节点使用普通J,所以您必须随需要导入,或者随动态导入 import()。 。 。 .
导入 CJS 导出 :
const cedar = require('@cedar-policy/cedar-wasm/nodejs');
console.log(cedar.getCedarVersion());
使用 Esm async 导入 Esm 版本 :
import('@cedar-policy/cedar-wasm/nodejs')
.then(cedar => console.log(cedar.getCedarVersion()));
装入网络包装5:
最小软件包. json 用于包括 Dev 服务器在内的网络软件包 :
{
"name": "webpack-ts-tester",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"dev": "webpack serve"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@cedar-policy/cedar-wasm": "3.2.0"
},
"devDependencies": {
"ts-loader": "^9.5.1",
"typescript": "^5.4.5",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4",
"html-webpack-plugin": "^5.6.0"
}
}
最小 tsconfig :
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es2020",
"target": "es5",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node"
}
}
配置网页包. config.js :
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development', // change this to suit you
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
experiments: {
asyncWebAssembly: true, // enables wasm support in webpack
},
plugins: [new HtmlWebpackPlugin()],
devServer: {
static: {
directory: path.join(__dirname, 'dist'),
},
compress: true,
port: 8000,
}
};
最后,从你的代码加载 index.ts 文档。 我们建议动态进口:
import('@cedar-policy/cedar-wasm').then(mod => {
// cache it globally here or invoke functions like mod.getCedarVersion();
});
装入Vite 5:
从 vite 的打字模板开始, 安装这两个依赖以启用 sem :
npm i --save-dev vite-plugin-top-level-await vite-plugin-wasm
然后在您的 Vite 配置中添加这两个插件 vite.config.js数字 :
import wasm from 'vite-plugin-wasm';
import topLevelAwait from 'vite-plugin-top-level-await';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
wasm(),
topLevelAwait()
]
});
最后,装入代码。 我们建议动态进口:
import('@cedar-policy/cedar-wasm').then(mod => {
// cache it globally here or invoke functions like mod.getCedarVersion();
});
其它加载策略
如果你因为某种原因不能使用 es 模块,我们提供替代子包 web 财务报告和审定财务报表 nodejs (定义: exports (原始内容存档于2018-09-21). at the root package.json.
这个 nodejs 子包装用途 fs 和普通JS模块。 要使用它,可以这样导入:
const cedar = require('@cedar-policy/cedar-wasm/nodejs')
这个 web 子软件包暴露 initSync 函数,用于在各种情况下加载 Cedar,因为无论出于何种原因,您都想要加载原二进制合成。 使用 web 可能还需要一些子包装 jest 设置。 这是你如何使用 web 子包装 :
const wasmBuffer = ... // `fetch` it or use `fs` to read it from `node_modules` in jest setupTests
import * as cedarJsBindings from '@cedar-policy/cedar-wasm/web';
cedarJsBindings.initSync(wasmBuffer);