Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stagas/run-in-client

runs a single function in the client using puppeteer and esbuild
https://github.com/stagas/run-in-client

esbuild function puppeteer test testing typescript

Last synced: about 1 month ago
JSON representation

runs a single function in the client using puppeteer and esbuild

Awesome Lists containing this project

README

        

run-in-client


runs a single function in the client using puppeteer and esbuild


🔧 Install
· 🧩 Example
· 📜 API docs
· 🔥 Releases
· 💪🏼 Contribute
· 🖐️ Help

***

## Install

```sh
$ npm i run-in-client
```

## API

#### Table of Contents

* [runInClient](#runinclient)
* [Parameters](#parameters)

### runInClient

[src/index.ts:43-92](https://github.com/stagas/run-in-client/blob/73a6c003203299a30c1469173e1a00bcd43a986d/src/index.ts#L43-L92 "Source code on GitHub")

Creates a static server with esbuild transforms, creates a puppeteer instance,
executes a single function, returns its result and tears down.

```ts
const setup = {
root: path.resolve(path.join(__dirname, '..')),
include: `
import { someModule } from './some-module.ts' // note we can import .ts files
window.someModule = someModule
`,
}

// this is how our function below gets intellisense
declare window: WindowOrWorkerGlobalScope & { someModule: typeof someModule }

// the function runs in the client, as such doesn't have access to the scope!
const result = await runInClient(setup, async () => {
const output = await window.someModule.doSomething()
return output
})
// => `result` is now `output`
```

#### Parameters

* `setup` **ClientSetup** A setup object

* `setup.root` The static root directory to serve and esbuild transform files from
* `setup.include` JavaScript to run before the function, like import statements
* `setup.buildOptions` Esbuild build options (@see )
* `setup.launchOptions` Puppeteer launch options (@see )
* `fn` **function (): any** The function to run. This will be passed in `page.evaluate(fn)`

Returns **any** The result value of `page.evaluate(fn)`

## Contribute

[Fork](https://github.com/stagas/run-in-client/fork) or
[edit](https://github.dev/stagas/run-in-client) and submit a PR.

All contributions are welcome!

## License

MIT © 2021
[stagas](https://github.com/stagas)