Skip to content

Instantly share code, notes, and snippets.

@dmnsgn
Created November 24, 2021 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmnsgn/1b6b464a6c6aa2ed4d4c1d2a18042e02 to your computer and use it in GitHub Desktop.
Save dmnsgn/1b6b464a6c6aa2ed4d4c1d2a18042e02 to your computer and use it in GitHub Desktop.
console.log("foo");
<!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>
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]);
{
"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"
}
}
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 },
};
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);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment