diff --git a/src/master/implementation.node.ts b/src/master/implementation.node.ts index 68878c4a..26639a18 100644 --- a/src/master/implementation.node.ts +++ b/src/master/implementation.node.ts @@ -10,6 +10,8 @@ import { ThreadsWorkerOptions, WorkerImplementation } from "../types/master" +import { isWebpack, requireFunction } from "../webpack-hack" +export declare const __non_webpack_require__: typeof require interface WorkerGlobalScope { addEventListener(eventName: string, listener: (event: Event) => void): void @@ -17,7 +19,7 @@ interface WorkerGlobalScope { removeEventListener(eventName: string, listener: (event: Event) => void): void } -declare const __non_webpack_require__: typeof require + declare const self: WorkerGlobalScope type WorkerEventName = "error" | "message" @@ -27,7 +29,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 } @@ -81,7 +83,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[\/\\]/))) @@ -90,11 +93,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 @@ -272,9 +273,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 } } diff --git a/src/webpack-hack.ts b/src/webpack-hack.ts new file mode 100644 index 00000000..f1965f74 --- /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 +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 1219651a..99714a6b 100644 --- a/src/worker_threads.ts +++ b/src/worker_threads.ts @@ -1,28 +1,10 @@ // Webpack hack -// tslint:disable no-eval +import { requireFunction } from './webpack-hack' -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 - -function selectImplementation(): WorkerThreadsModule { - return typeof __non_webpack_require__ === "function" - ? __non_webpack_require__("worker_threads") - : eval("require")("worker_threads") -} - -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 }