This is a short demo of using rollup-plugin-off-main-thread and Comlink.
$ npm install
$ npm run build
$ npm run serve
package-lock.json | |
node_modules | |
dist |
This is a short demo of using rollup-plugin-off-main-thread and Comlink.
$ npm install
$ npm run build
$ npm run serve
<!doctype html> | |
<script src="main.js" defer></script> |
import { wrap } from "comlink"; | |
async function init() { | |
const worker = new Worker("./worker.js"); | |
const api = wrap(worker); | |
const result = await api.add(40, 2); | |
alert(`Result: ${result}`); | |
} | |
init(); |
{ | |
"name": "omt-demo", | |
"version": "0.0.1", | |
"description": "", | |
"private": true, | |
"scripts": { | |
"build": "rollup -c && cp index.html dist", | |
"serve": "http-server -c0 dist" | |
}, | |
"keywords": [], | |
"author": "Surma <surma@surma.link>", | |
"license": "Apache-2.0", | |
"dependencies": { | |
"comlink": "^4.0.1", | |
"rollup": "^1.17.0", | |
"rollup-plugin-node-resolve": "^5.2.0", | |
"rollup-plugin-off-main-thread": "^1.0.0" | |
}, | |
"devDependencies": { | |
"http-server": "^0.11.1" | |
} | |
} |
import nodeResolve from "rollup-plugin-node-resolve"; | |
import OMT from "rollup-plugin-off-main-thread"; | |
export default { | |
input: "main.js", | |
output: { | |
dir: "dist", | |
format: "amd" | |
}, | |
plugins: [ | |
nodeResolve(), | |
OMT() | |
] | |
} |
import { expose } from "comlink"; | |
class API { | |
static add(a, b) { | |
return a + b; | |
} | |
} | |
expose(API); |