From 7382333c9eba7bd5b5ca2b15f61af8e5db9cfe24 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 30 Apr 2021 06:04:13 -0500 Subject: [PATCH 1/6] Add isWebpack and requireFunction --- src/master/implementation.node.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/master/implementation.node.ts b/src/master/implementation.node.ts index 68878c4a..5b60c911 100644 --- a/src/master/implementation.node.ts +++ b/src/master/implementation.node.ts @@ -17,7 +17,12 @@ interface WorkerGlobalScope { removeEventListener(eventName: string, listener: (event: Event) => void): void } -declare const __non_webpack_require__: typeof require +// TODO remove webpack hacks. These hurt the performance for non-web-pack situations +// Webpack hack +declare let __non_webpack_require__: typeof require +const isWebpack = typeof __non_webpack_require__ === "function" +const requireFunction: typeof require = isWebpack ? __non_webpack_require__ : eval("require") + declare const self: WorkerGlobalScope type WorkerEventName = "error" | "message" From fb54ff605cd296f38cee22eca5e0c29a0f7b2078 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 30 Apr 2021 06:04:48 -0500 Subject: [PATCH 2/6] Use isWebpack --- src/master/implementation.node.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/master/implementation.node.ts b/src/master/implementation.node.ts index 5b60c911..69983df8 100644 --- a/src/master/implementation.node.ts +++ b/src/master/implementation.node.ts @@ -32,7 +32,7 @@ let tsNodeAvailable: boolean | undefined export const defaultPoolSize = cpus().length function detectTsNode() { - if (typeof __non_webpack_require__ === "function") { + if (isWebpack) { // Webpack build: => No ts-node required or possible return false } @@ -86,7 +86,8 @@ function resolveScriptPath(scriptPath: string, baseURL?: string | undefined) { return path.isAbsolute(filePath) ? filePath : path.join(baseURL || eval("__dirname"), filePath) } - const workerFilePath = typeof __non_webpack_require__ === "function" + // Webpack hack + const workerFilePath = isWebpack ? __non_webpack_require__.resolve(makeRelative(scriptPath)) : eval("require").resolve(makeRelative(rebaseScriptPath(scriptPath, /[\/\\]worker_threads[\/\\]/))) From 0cf99be9ac96ee613c64e2401845228dd0f07037 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 30 Apr 2021 06:06:44 -0500 Subject: [PATCH 3/6] Use requireFunction --- src/master/implementation.node.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/master/implementation.node.ts b/src/master/implementation.node.ts index 69983df8..0d531cd3 100644 --- a/src/master/implementation.node.ts +++ b/src/master/implementation.node.ts @@ -96,11 +96,9 @@ function resolveScriptPath(scriptPath: string, baseURL?: string | undefined) { function initWorkerThreadsWorker(): ImplementationExport { // Webpack hack - const NativeWorker = typeof __non_webpack_require__ === "function" - ? __non_webpack_require__("worker_threads").Worker - : eval("require")("worker_threads").Worker + const NativeWorker = (requireFunction("worker_threads") as typeof import("worker_threads")).Worker - let allWorkers: Array = [] + let allWorkers: Array = [] class Worker extends NativeWorker { private mappedEventListeners: WeakMap @@ -278,9 +276,7 @@ export function isWorkerRuntime() { return typeof self !== "undefined" && self.postMessage ? true : false } else { // Webpack hack - const isMainThread = typeof __non_webpack_require__ === "function" - ? __non_webpack_require__("worker_threads").isMainThread - : eval("require")("worker_threads").isMainThread + const isMainThread = (requireFunction("worker_threads") as typeof import("worker_threads")).isMainThread return !isMainThread } } From c685aecbeb4b9df5765881d65ed373a905a724f8 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 30 Apr 2021 06:20:58 -0500 Subject: [PATCH 4/6] Move webpack hack to a separate file --- src/master/implementation.node.ts | 6 +----- src/webpack-hack.ts | 7 +++++++ 2 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 src/webpack-hack.ts diff --git a/src/master/implementation.node.ts b/src/master/implementation.node.ts index 0d531cd3..1868d029 100644 --- a/src/master/implementation.node.ts +++ b/src/master/implementation.node.ts @@ -10,6 +10,7 @@ import { ThreadsWorkerOptions, WorkerImplementation } from "../types/master" +import { isWebpack, requireFunction, __non_webpack_require__ } from '../webpack-hack' interface WorkerGlobalScope { addEventListener(eventName: string, listener: (event: Event) => void): void @@ -17,11 +18,6 @@ interface WorkerGlobalScope { removeEventListener(eventName: string, listener: (event: Event) => void): void } -// TODO remove webpack hacks. These hurt the performance for non-web-pack situations -// Webpack hack -declare let __non_webpack_require__: typeof require -const isWebpack = typeof __non_webpack_require__ === "function" -const requireFunction: typeof require = isWebpack ? __non_webpack_require__ : eval("require") declare const self: WorkerGlobalScope diff --git a/src/webpack-hack.ts b/src/webpack-hack.ts new file mode 100644 index 00000000..015fc654 --- /dev/null +++ b/src/webpack-hack.ts @@ -0,0 +1,7 @@ +// tslint:disable no-eval + +// TODO remove webpack hacks. These hurt the performance for non-web-pack situations +// Webpack hack +export declare let __non_webpack_require__: typeof require +export const isWebpack = typeof __non_webpack_require__ === "function" +export const requireFunction: typeof require = isWebpack ? __non_webpack_require__ : eval("require") From fcc43da6604ad918645e9503af411027bcf4e2e4 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 30 Apr 2021 06:34:33 -0500 Subject: [PATCH 5/6] Use webpack-hack inside worker_threads --- src/worker_threads.ts | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src/worker_threads.ts b/src/worker_threads.ts index 1219651a..35fd4c64 100644 --- a/src/worker_threads.ts +++ b/src/worker_threads.ts @@ -1,28 +1,12 @@ -// Webpack hack -// tslint:disable no-eval - -declare function __non_webpack_require__(module: string): any - -// FIXME -type MessagePort = any - -interface WorkerThreadsModule { - MessagePort: typeof MessagePort - isMainThread: boolean - parentPort: MessagePort -} - -let implementation: WorkerThreadsModule | undefined +// TODO this file isn't used! -function selectImplementation(): WorkerThreadsModule { - return typeof __non_webpack_require__ === "function" - ? __non_webpack_require__("worker_threads") - : eval("require")("worker_threads") -} +// Webpack hack +import { requireFunction } from './webpack-hack' -export default function getImplementation(): WorkerThreadsModule { +let implementation: typeof import("worker_threads") | undefined +export default function getImplementation() { if (!implementation) { - implementation = selectImplementation() + implementation = (requireFunction("worker_threads") as typeof import("worker_threads")) } return implementation } From 368c149924bde853a0dd6fdd3e559b9b55e060b9 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 30 Apr 2021 07:09:16 -0500 Subject: [PATCH 6/6] Declared variable shouldn't be imported --- src/master/implementation.node.ts | 3 ++- src/webpack-hack.ts | 2 +- src/worker_threads.ts | 2 -- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/master/implementation.node.ts b/src/master/implementation.node.ts index 1868d029..26639a18 100644 --- a/src/master/implementation.node.ts +++ b/src/master/implementation.node.ts @@ -10,7 +10,8 @@ import { ThreadsWorkerOptions, WorkerImplementation } from "../types/master" -import { isWebpack, requireFunction, __non_webpack_require__ } from '../webpack-hack' +import { isWebpack, requireFunction } from "../webpack-hack" +export declare const __non_webpack_require__: typeof require interface WorkerGlobalScope { addEventListener(eventName: string, listener: (event: Event) => void): void diff --git a/src/webpack-hack.ts b/src/webpack-hack.ts index 015fc654..f1965f74 100644 --- a/src/webpack-hack.ts +++ b/src/webpack-hack.ts @@ -2,6 +2,6 @@ // TODO remove webpack hacks. These hurt the performance for non-web-pack situations // Webpack hack -export declare let __non_webpack_require__: typeof require +declare const __non_webpack_require__: typeof require export const isWebpack = typeof __non_webpack_require__ === "function" export const requireFunction: typeof require = isWebpack ? __non_webpack_require__ : eval("require") diff --git a/src/worker_threads.ts b/src/worker_threads.ts index 35fd4c64..99714a6b 100644 --- a/src/worker_threads.ts +++ b/src/worker_threads.ts @@ -1,5 +1,3 @@ -// TODO this file isn't used! - // Webpack hack import { requireFunction } from './webpack-hack'