project restruct: use webpack instead of wrangler's wasm-pack support
The generated script from wrangler keeps re-instantiating the WASM module every time the request is processed. Instead, let's switch to using the much, much better Webpack support for WASM.
This commit is contained in:
parent
e562a8d7e4
commit
c91d152d65
7 changed files with 3636 additions and 59 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -4,6 +4,8 @@ Cargo.lock
|
|||
bin/
|
||||
pkg/
|
||||
wasm-pack.log
|
||||
worker/generated/
|
||||
worker/
|
||||
config.json
|
||||
wrangler.toml
|
||||
wrangler.toml
|
||||
node_modules/
|
||||
dist/
|
39
README.md
39
README.md
|
@ -1,39 +0,0 @@
|
|||
# 👷♀️🦀🕸️ `rustwasm-worker-template`
|
||||
|
||||
A template for kick starting a Cloudflare worker project using
|
||||
[`wasm-pack`](https://github.com/rustwasm/wasm-pack).
|
||||
|
||||
This template is designed for compiling Rust libraries into WebAssembly and
|
||||
publishing the resulting worker to Cloudflare's worker infrastructure.
|
||||
|
||||
## 🔋 Batteries Included
|
||||
|
||||
* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
|
||||
between WebAssembly and JavaScript.
|
||||
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
|
||||
for logging panic messages to the developer console.
|
||||
* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized
|
||||
for small code size.
|
||||
|
||||
## 🚴 Usage
|
||||
|
||||
### 🐑 Use `wrangler generate` to Clone this Template
|
||||
|
||||
[Learn more about `wrangler generate` here.](https://github.com/cloudflare/wrangler)
|
||||
|
||||
```
|
||||
wrangler generate wasm-worker https://github.com/cloudflare/rustwasm-worker-template.git
|
||||
cd wasm-worker
|
||||
```
|
||||
|
||||
### 🛠️ Build with `wasm-pack build`
|
||||
|
||||
```
|
||||
wasm-pack build
|
||||
```
|
||||
|
||||
### 🔬 Test in Headless Browsers with `wasm-pack test`
|
||||
|
||||
```
|
||||
wasm-pack test --headless --firefox
|
||||
```
|
3595
package-lock.json
generated
Normal file
3595
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
23
package.json
Normal file
23
package.json
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "paprika",
|
||||
"version": "0.1.0",
|
||||
"description": "A simple blog integrated with Standard Notes and running on Cloudflare Workers",
|
||||
"main": "worker.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/PeterCxy/paprika.git"
|
||||
},
|
||||
"author": "PeterCxy",
|
||||
"license": "WTFPL",
|
||||
"bugs": {
|
||||
"url": "https://github.com/PeterCxy/paprika/issues"
|
||||
},
|
||||
"homepage": "https://github.com/PeterCxy/paprika#readme",
|
||||
"devDependencies": {
|
||||
"@wasm-tool/wasm-pack-plugin": "^1.2.0",
|
||||
"webpack": "^4.42.1"
|
||||
}
|
||||
}
|
12
webpack.config.js
Normal file
12
webpack.config.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
|
||||
|
||||
module.exports = {
|
||||
target: "webworker",
|
||||
entry: "./worker.js",
|
||||
mode: "production",
|
||||
plugins: [
|
||||
new WasmPackPlugin({
|
||||
crateDirectory: __dirname,
|
||||
})
|
||||
]
|
||||
};
|
|
@ -1,6 +1,3 @@
|
|||
const { handle_request_rs } = wasm_bindgen;
|
||||
var gen = false;
|
||||
|
||||
addEventListener('fetch', event => {
|
||||
event.respondWith(handleRequest(event.request))
|
||||
})
|
||||
|
@ -10,9 +7,6 @@ addEventListener('fetch', event => {
|
|||
* @param {Request} request
|
||||
*/
|
||||
async function handleRequest(request) {
|
||||
if (!gen) {
|
||||
await wasm_bindgen(wasm);
|
||||
gen = true;
|
||||
}
|
||||
return await handle_request_rs(request);
|
||||
const rust = await import("./pkg/index");
|
||||
return await rust.handle_request_rs(request);
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
{
|
||||
"body_part": "script",
|
||||
"bindings": [
|
||||
{
|
||||
"name": "wasm",
|
||||
"type": "wasm_module",
|
||||
"part": "wasmprogram"
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in a new issue