-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add simple example, update readme for bundler instructions
Signed-off-by: ks2211 <[email protected]>
- Loading branch information
ks2211
committed
Aug 31, 2021
1 parent
0810351
commit 51a6884
Showing
9 changed files
with
167 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules/* | ||
dist/* | ||
index.html | ||
!examples/**/*index.html* | ||
*.sh* | ||
wasmcloud-rs-js/pkg/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# wasmcloud-js Examples | ||
|
||
This directory contains examples of using the `wasmcloud-js` library with sample `webpack` and `esbuild` configurations. | ||
|
||
## Prerequisities | ||
|
||
* NATS with WebSockets enabled | ||
|
||
* wasmCloud lattice (OTP Version) | ||
|
||
* (OPTIONAL) Docker Registry with CORS configured | ||
|
||
* If launching actors from remote registries in the browser host, CORS must be configured on the registry server | ||
|
||
* NodeJS, NPM, npx | ||
|
||
## Build | ||
|
||
```sh | ||
$ npm install webpack esbuild copy-webpack-plugin fs | ||
$ node esbuild.js # this produces the esbuild version | ||
$ npx webpack --config=example-webpack.config.js # this produces the webpack output | ||
``` | ||
|
||
## Usage | ||
|
||
1. Build the code | ||
|
||
2. Start a web server inside this directory (e.g `python3 -m http.server`) | ||
|
||
3. Navigate to a browser `localhost:<PORT>` | ||
|
||
4. Open the developer console to view the host output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Use this path in projects using the node import | ||
let defaultWasmFileLocation = './node_modules/@wasmcloud/wasmcloud-js/dist/wasmcloud-rs-js/pkgwasmcloud.wasm' | ||
let wasmFileLocationForLocal = '../../dist/wasmcloud-rs-js/pkg/wasmcloud.wasm' | ||
|
||
let copyPlugin = { | ||
name: 'copy', | ||
setup(build) { | ||
require('fs').copyFile(wasmFileLocationForLocal, `${process.cwd()}/wasmcloud.wasm`, (err) => { | ||
if (err) throw err; | ||
}); | ||
} | ||
} | ||
require('esbuild').build({ | ||
entryPoints: ['main.js'], | ||
bundle: true, | ||
outfile: 'out-esbuild.js', | ||
plugins: [copyPlugin] | ||
}).catch(() => process.exit(1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const path = require('path'); | ||
const CopyPlugin = require('copy-webpack-plugin') | ||
|
||
module.exports = { | ||
stats: { assets: false, modules: false }, | ||
entry: './main.js', | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.tsx?$/, | ||
use: 'ts-loader', | ||
exclude: /node_modules/ | ||
} | ||
] | ||
}, | ||
// this is needed to copy the wasm file used by the js code to initiate a host key/extract the token from a signed actor | ||
// this SHOULD go away once the upstream webpack build issues are resolved (webpack will automatically pick up the webpack file without needing a copy) | ||
plugins: [ | ||
new CopyPlugin({ | ||
patterns: [ | ||
{ | ||
// the node_module path should be referenced in projects using the node import | ||
// from: 'node_modules/@wasmcloud/wasmcloud-js/dist/*.wasm', | ||
from: '../../dist/wasmcloud-rs-js/pkg/*.wasm', | ||
to: '[name].wasm' | ||
} | ||
] | ||
}) | ||
], | ||
mode: 'production', | ||
resolve: { | ||
extensions: ['.tsx', '.ts', '.js', '.wasm'] | ||
}, | ||
experiments: { | ||
asyncWebAssembly: true | ||
}, | ||
output: { | ||
filename: 'out-webpack.js', | ||
path: path.resolve(__dirname, '') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Hello World - wasmcloudjs</title> | ||
<!-- <script src="https://unpkg.com/@wasmcloud/[email protected]/dist/wasmcloud.js"></script> --> | ||
<script src="./out.js"></script> | ||
|
||
<!-- this uses the npm library--> | ||
<!-- <script src="./out-webpack.js"></script> --> | ||
<!-- <script src="./out-esbuild.js"></script> --> | ||
<script> | ||
// (async() => { | ||
// console.log("USING THE BROWSER BUNDLE") | ||
// const host = await wasmcloudjs.startHost("default", false, ["ws://localhost:4222"]) | ||
// console.log(host); | ||
// })() | ||
</script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { startHost } from '../../dist/src' | ||
|
||
// Importing inside of a project | ||
// import { startHost } from '@wasmcloud/wasmcloud-js'; | ||
// const { startHost } = require('@wasmcloud/wasmcloud-js'); | ||
|
||
(async () => { | ||
console.log('USING A JS BUNDLER') | ||
const host = await startHost('default', false, ['ws://localhost:4222']) | ||
console.log(host); | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters