run-in-client

runs a single function in the client using puppeteer and esbuild

Usage no npm install needed!

<script type="module">
  import runInClient from 'https://cdn.skypack.dev/run-in-client';
</script>

README

run-in-client

runs a single function in the client using puppeteer and esbuild

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


Install

$ npm i run-in-client

API

Table of Contents

runInClient

src/index.ts:43-92

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

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

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

Contribute

Fork or edit and submit a PR.

All contributions are welcome!

License

MIT © 2021 stagas