Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor webpack hacks #357

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/master/implementation.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ 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
postMessage(message: any, transferables?: any[]): void
removeEventListener(eventName: string, listener: (event: Event) => void): void
}

declare const __non_webpack_require__: typeof require

declare const self: WorkerGlobalScope

type WorkerEventName = "error" | "message"
Expand All @@ -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
}
Expand Down Expand Up @@ -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[\/\\]/)))

Expand All @@ -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<typeof NativeWorker> = []
let allWorkers: Array<Worker> = []

class Worker extends NativeWorker {
private mappedEventListeners: WeakMap<EventListener, EventListener>
Expand Down Expand Up @@ -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
}
}
7 changes: 7 additions & 0 deletions src/webpack-hack.ts
Original file line number Diff line number Diff line change
@@ -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")
26 changes: 4 additions & 22 deletions src/worker_threads.ts
Original file line number Diff line number Diff line change
@@ -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
}