Skip to content

Commit 31fb4fd

Browse files
cecilemullerSpaceK33z
authored andcommitted
Added example "webworker" (#755)
1 parent 7e18f6a commit 31fb4fd

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

examples/webworker/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Webworker
2+
3+
4+
## How to run
5+
6+
```shell
7+
node ../../bin/webpack-dev-server.js
8+
```
9+
10+
11+
## What should happen
12+
13+
1. The main thread sends a message to the Worker.
14+
2. The worker outputs the message in the console.
15+
3. The worker sends a message back to the main thread.
16+
4. The main thread posts the message in the console.
17+
18+
No error, warning or other log traces should be in the console.
19+

examples/webworker/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Example "webworker"</title>
6+
</head>
7+
<body>
8+
<script src="web.bundle.js"></script>
9+
</body>
10+
</html>

examples/webworker/web.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-env browser */
2+
"use strict";
3+
4+
const worker = new Worker("worker.bundle.js");
5+
worker.onmessage = function(e) {
6+
console.log("[MAIN]", e);
7+
};
8+
worker.postMessage({
9+
hello: 111
10+
});

examples/webworker/webpack.config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
module.exports = [
4+
{
5+
devtool: "source-map",
6+
target: "web",
7+
entry: "./web.js",
8+
output: {
9+
filename: "web.bundle.js",
10+
path: __dirname
11+
}
12+
},
13+
{
14+
devtool: "source-map",
15+
target: "webworker",
16+
entry: "./worker.js",
17+
output: {
18+
filename: "worker.bundle.js",
19+
path: __dirname
20+
}
21+
}
22+
];

examples/webworker/worker.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-env worker */
2+
"use strict";
3+
4+
self.onmessage = function(e) {
5+
console.log("[WORKER]", e);
6+
self.postMessage({
7+
hello: 222
8+
});
9+
};

0 commit comments

Comments
 (0)