From 2804eb9aabea54a4de395bea63fb6d3a0ddc1d6a Mon Sep 17 00:00:00 2001 From: Christian Rackerseder Date: Sat, 2 Jun 2018 14:40:17 +0200 Subject: [PATCH] Add TypeScript documentation --- README.md | 71 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e4470c4..a2ceef0 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,11 @@ > A webpack loader that moves a module and its dependencies into a Web Worker, automatically reflecting exported functions as asynchronous proxies. -- Bundles a tiny, purpose-built RPC implementation into your app -- If exported module methods are already async, signature is unchanged -- Supports synchronous and asynchronous worker functions -- Works beautifully with async/await -- Imported value is instantiable, just a decorated `Worker` - +* Bundles a tiny, purpose-built RPC implementation into your app +* If exported module methods are already async, signature is unchanged +* Supports synchronous and asynchronous worker functions +* Works beautifully with async/await +* Imported value is instantiable, just a decorated `Worker` ## Install @@ -17,7 +16,6 @@ npm install -D workerize-loader ``` - ### Usage **worker.js**: @@ -25,28 +23,68 @@ npm install -D workerize-loader ```js // block for `time` ms, then return the number of loops we could run in that time: export function expensive(time) { - let start = Date.now(), - count = 0 - while (Date.now() - start < time) count++ - return count + let start = Date.now(), + count = 0; + while (Date.now() - start < time) count++; + return count; } ``` **index.js**: _(our demo)_ ```js -import worker from 'workerize-loader!./worker' +import worker from "workerize-loader!./worker"; -let instance = worker() // `new` is optional +let instance = worker(); // `new` is optional -instance.expensive(1000).then( count => { - console.log(`Ran ${count} loops`) -}) +instance.expensive(1000).then(count => { + console.log(`Ran ${count} loops`); +}); ``` +### About [TypeScript](http://typescriptlang.org) + +If you're using [TypeScript](http://typescriptlang.org) in your build you have to modify your +webpack configuration as follows: + +```js +module: { + rules: [ + { + test: /\.worker\.ts$/, + use: ['workerize-loader', 'ts-loader'], + }, + { + test: /\.tsx?$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], +}, +``` + +After that you can import your loaders with following syntax: + +```ts +import * as myWorker from "./workers/my.worker"; + +const { expensive } = (myWorker as any)() as typeof myWorker; +await expensive(1000); +``` + +**Important**: Don't forget to mark every exported worker function as `async` + +```ts +export async function expensive(time: number) {} +``` + +**Note**: If you are getting errors in your build like `Module build failed: Error: Typescript emitted no output` +try to set the compiler flag `noEmitOnError` to `false`. + ### About [Babel](https://babeljs.io/) If you're using [Babel](https://babeljs.io/) in your build, make sure you disabled commonJS transform. Otherwize, workerize-loader won't be able to retrieve the list of exported function from your worker script : + ```js { test: /\.js$/, @@ -68,7 +106,6 @@ If you're using [Babel](https://babeljs.io/) in your build, make sure you disabl The inner workings here are heavily inspired by [worker-loader](https://github.com/webpack-contrib/worker-loader). It's worth a read! - ### License [MIT License](https://oss.ninja/mit/developit) © [Jason Miller](https://jasonformat.com)