Skip to content

Commit 8ac1c72

Browse files
authored
feat: add worker inline example (#254)
1 parent 1ee7112 commit 8ac1c72

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed

pnpm-lock.yaml

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rspack/worker-inline/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
</body>
12+
</html>

rspack/worker-inline/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "example-worker-inline",
3+
"version": "1.0.0",
4+
"private": true,
5+
"license": "MIT",
6+
"sideEffects": ["**/*.css", "**/*.less", "**/*.scss"],
7+
"main": "index.js",
8+
"scripts": {
9+
"build": "rspack build",
10+
"dev": "rspack serve"
11+
},
12+
"devDependencies": {
13+
"@rspack/cli": "1.2.7",
14+
"@rspack/core": "1.2.7"
15+
},
16+
"dependencies": {
17+
"worker-rspack-loader": "^3.1.2"
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @ts-check
2+
import { rspack } from "@rspack/core";
3+
import { defineConfig } from "@rspack/cli";
4+
5+
export default defineConfig({
6+
entry: "./src/index.js",
7+
plugins: [
8+
new rspack.HtmlRspackPlugin({
9+
template: "./index.html",
10+
}),
11+
],
12+
module: {
13+
rules: [
14+
{
15+
test: /worker\.js$/,
16+
loader: "worker-rspack-loader",
17+
options: {
18+
inline: "no-fallback",
19+
},
20+
},
21+
],
22+
},
23+
});

rspack/worker-inline/src/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Worker from './worker.js';
2+
3+
const worker = new Worker();
4+
5+
let result;
6+
7+
worker.onmessage = function (event) {
8+
if (!result) {
9+
result = document.createElement('div');
10+
result.setAttribute('id', 'result');
11+
12+
document.body.append(result);
13+
}
14+
15+
result.innerText = JSON.stringify(event.data);
16+
};
17+
18+
worker.postMessage({ postMessage: true });

rspack/worker-inline/src/worker.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
onmessage = function (event) {
2+
const workerResult = event.data;
3+
4+
workerResult.onmessage = true;
5+
6+
postMessage(workerResult);
7+
};

0 commit comments

Comments
 (0)