Skip to content

Commit

Permalink
Fix Worker polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
W4G1 committed Dec 17, 2023
1 parent f5f0764 commit 84ffc96
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 21 deletions.
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"watch": ["."],
"ext": "js,ts,json",
"ignore": [".temp", "dist", ".rollup.cache"],
"exec": "rollup -c rollup.config.worker.dev.js --configPlugin @rollup/plugin-typescript && rollup -c rollup.config.dev.js --configPlugin @rollup/plugin-typescript && node ./node_test.js"
"exec": "rollup -c rollup.config.worker.dev.js --configPlugin @rollup/plugin-typescript && rollup -c rollup.config.dev.js --configPlugin @rollup/plugin-typescript && node ./test/node_test.js"
}
32 changes: 29 additions & 3 deletions package-lock.json

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

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multithreading",
"version": "0.1.4",
"version": "0.1.5",
"description": "Multithreading that feels like normal JavaScript",
"author": "Walter van der Giessen <waltervdgiessen@gmail.com>",
"homepage": "https://github.com/W4G1/multithreading/tree/main#readme",
Expand All @@ -12,12 +12,11 @@
"webworker",
"parallel"
],
"type": "module",
"types": "./dist/esm/index.d.ts",
"main": "./dist/cjs/index.js",
"main": "./dist/cjs/index.cjs",
"module": "./dist/esm/index.js",
"exports": {
"require": "./dist/cjs/index.js",
"require": "./dist/cjs/index.cjs",
"import": "./dist/esm/index.js"
},
"files": [
Expand All @@ -35,6 +34,7 @@
"@babel/preset-env": "^7.23.6",
"@babel/preset-typescript": "^7.23.3",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-inject": "^5.0.5",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
Expand All @@ -61,7 +61,9 @@
"dependencies": {
"@rollup/plugin-swc": "^0.3.0",
"acorn": "^8.11.2",
"acorn-walk": "^8.3.1",
"acorn-walk": "^8.3.1"
},
"peerDependencies": {
"web-worker": "^1.2.0"
}
}
5 changes: 5 additions & 0 deletions rollup.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import terser from "@rollup/plugin-terser";
import replace from "@rollup/plugin-replace";
import fs from "node:fs";
import swc from '@rollup/plugin-swc';
import inject from "@rollup/plugin-inject";
import path from "node:path";

export default ["cjs"].flatMap((type) =>
[""].map(
Expand All @@ -17,6 +19,9 @@ export default ["cjs"].flatMap((type) =>
.replaceAll("`", "\\`")
.replaceAll("$", "\\$"),
}),
inject({
Worker: path.resolve(`src/lib/polyfills/Worker.${type}.ts`)
}),
],
output: [
{
Expand Down
29 changes: 20 additions & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import terser from "@rollup/plugin-terser";
import babel from "@rollup/plugin-babel";
import replace from "@rollup/plugin-replace";
import typescript from "@rollup/plugin-typescript";
import inject from "@rollup/plugin-inject";
import fs from "node:fs";
import path from "node:path";

export default ["esm", "cjs"].flatMap((type) =>
["", ".min"].map(
Expand All @@ -22,9 +24,12 @@ export default ["esm", "cjs"].flatMap((type) =>
typescript(),
replace({
__INLINE_WORKER__: fs
.readFileSync(`.temp/worker.${type}${version}.js`, "utf8")
.replaceAll("`", "\\`")
.replaceAll("$", "\\$"),
.readFileSync(`.temp/worker.${type}${version}.js`, "utf8")
.replaceAll("`", "\\`")
.replaceAll("$", "\\$"),
}),
inject({
Worker: path.resolve(`src/lib/polyfills/Worker.${type}.ts`)
}),
],
output: [
Expand All @@ -36,12 +41,18 @@ export default ["esm", "cjs"].flatMap((type) =>
globals: {
"web-worker": "Worker",
},
plugins: [...(version === ".min" ? [terser({
compress: {
toplevel: true,
passes: 3,
}
})] : [])],
plugins: [
...(version === ".min"
? [
terser({
compress: {
toplevel: true,
passes: 3,
},
}),
]
: []),
],
},
],
external: ["web-worker"],
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "./lib/polyfills/Promise.withResolvers.ts";
import "./lib/polyfills/Worker.ts";
import { serialize } from "./lib/serialize.ts";
import { GLOBAL_FUNCTION_NAME } from "./constants.ts";
import * as $ from "./lib/keys.ts";
Expand Down
1 change: 1 addition & 0 deletions src/lib/polyfills/Worker.cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default globalThis.Worker || require("web-worker");
1 change: 1 addition & 0 deletions src/lib/polyfills/Worker.esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default globalThis.Worker || (await import("web-worker")).default;
1 change: 0 additions & 1 deletion src/lib/polyfills/Worker.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "es2015", /* Specify what module code is generated. */
"module": "es2022", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
Expand Down

0 comments on commit 84ffc96

Please sign in to comment.