Skip to content

Commit

Permalink
add web worker boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
pngwn committed Oct 18, 2020
1 parent fd783d8 commit abd5974
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 28 deletions.
4 changes: 4 additions & 0 deletions public/worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions public/worker.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 55 additions & 28 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import typescript from "@rollup/plugin-typescript";

const production = !process.env.ROLLUP_WATCH;

const onwarn = ({ message }) =>
message.includes("@rollup/plugin-typescript TS2315");

function serve() {
let server;

Expand All @@ -32,33 +35,57 @@ function serve() {
};
}

export default {
input: "src/main.ts",
output: {
sourcemap: true,
format: "iife",
name: "app",
file: "public/build/bundle.js",
},
plugins: [
svelte({
dev: !production,
css: (css) => {
css.write("bundle.css");
},
preprocess: sveltePreprocess(),
}),
export default [
{
input: "src/main.ts",
output: {
sourcemap: true,
format: "iife",
name: "app",
file: "public/build/bundle.js",
},
plugins: [
svelte({
dev: !production,
css: (css) => {
css.write("bundle.css");
},
preprocess: sveltePreprocess(),
}),

resolve({
browser: true,
dedupe: ["svelte"],
}),
commonjs(),
typescript(),
!production && serve(),
!production && livereload("public"),
],
watch: {
clearScreen: false,
resolve({
browser: true,
dedupe: ["svelte"],
}),
commonjs(),
typescript(),
!production && serve(),
!production && livereload("public"),
],
watch: {
clearScreen: false,
},
onwarn,
},
{
input: "src/worker.ts",
output: {
sourcemap: true,
format: "esm",
name: "app",
file: "public/worker.js",
},
plugins: [
resolve({
browser: true,
dedupe: ["svelte"],
}),
commonjs(),
typescript(),
],
watch: {
clearScreen: false,
},
onwarn,
},
};
];
12 changes: 12 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
];
let current: number = 0;
const worker = new Worker("./worker.js");
worker.addEventListener("message", (event) => {
console.log(event.data);
});
function compile(_components: Component[]): void {
worker.postMessage(_components);
}
$: compile(components);
</script>

<main>
Expand Down
8 changes: 8 additions & 0 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Component } from "./types";

self.addEventListener(
"message",
async (event: MessageEvent<Component[]>): Promise<void> => {
console.log(event.data);
}
);

0 comments on commit abd5974

Please sign in to comment.