Skip to content

Instantly share code, notes, and snippets.

@dmnsgn
Created November 24, 2021 15:28
Show Gist options
  • Save dmnsgn/1b6b464a6c6aa2ed4d4c1d2a18042e02 to your computer and use it in GitHub Desktop.
Save dmnsgn/1b6b464a6c6aa2ed4d4c1d2a18042e02 to your computer and use it in GitHub Desktop.

Revisions

  1. dmnsgn created this gist Nov 24, 2021.
    1 change: 1 addition & 0 deletions foo.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    console.log("foo");
    12 changes: 12 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    </head>
    <body>
    <script src="dist/bundle.js" type="module"></script>
    </body>
    </html>
    9 changes: 9 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const myWorker = new Worker(new URL("worker.js", import.meta.url), {
    type: "classic",
    });

    myWorker.onmessage = function (e) {
    console.log("Message received from worker", e.data);
    };

    myWorker.postMessage([42]);
    17 changes: 17 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    {
    "name": "webpack-worker-type-classic",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
    "build": "npx webpack",
    "serve": "npx browser-sync"
    },
    "keywords": [],
    "author": "Damien Seguin (https://github.com/dmnsgn)",
    "license": "MIT",
    "devDependencies": {
    "webpack": "^5.64.3",
    "webpack-cli": "^4.9.1"
    }
    }
    16 changes: 16 additions & 0 deletions webpack.config.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    const path = require("path");

    module.exports = {
    entry: "./index.js",
    mode: "production",
    optimization: {
    minimize: false,
    },
    output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js",
    clean: true,
    libraryTarget: "module",
    },
    experiments: { outputModule: true },
    };
    10 changes: 10 additions & 0 deletions worker.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    self.importScripts(new URL("./foo.js", import.meta.url));

    onmessage = function (e) {
    console.log("Worker: Message received from main script");

    const workerResult = "Result: " + e.data[0];

    console.log("Worker: Posting message back to main script");
    postMessage(workerResult);
    };