Skip to content

Instantly share code, notes, and snippets.

@passbyval
passbyval / useWorker.ts
Last active August 17, 2023 02:02
A web worker hook for React
import { useCallback, useEffect, useRef } from 'react'
import dedent from 'ts-dedent'
export interface IOptions extends Omit<WorkerOptions, 'type'> {
mimeType?: BlobPropertyBag['type']
workerType?: WorkerOptions['type']
}
export type IFunc<R, P = any> = (...args: P[]) => R | Promise<R>
@passbyval
passbyval / requireEsm.cjs
Created May 18, 2023 19:35
NX Plugin Workaround: Import ESM from CommonJS
/**
* Workaround for NX's lack of ESM support
*
* See the following issues:
* - {@link https://github.com/nrwl/nx/issues/16776 require() of ES Module executor.js ... not supported with custom plugin using es2015 module #16776 }
* - {@link https://github.com/nrwl/nx/issues/15682 ESM Support for Nx Plugins #15682}
*
* @type {<T, P = PromiseLike<T>>(module: string) => P}
*/
const requireEsm = async module => {