runs a single function in the client using puppeteer and esbuild
🔧 Install · 🧩 Example · 📜 API docs · 🔥 Releases · 💪🏼 Contribute · 🖐️ Help
$ npm i run-in-client
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`
-
setup
ClientSetup A setup objectsetup.root
The static root directory to serve and esbuild transform files fromsetup.include
JavaScript to run before the function, like import statementssetup.buildOptions
Esbuild build options (@see https://esbuild.github.io/api/#build-api)setup.launchOptions
Puppeteer launch options (@see https://puppeteer.github.io/puppeteer/docs/puppeteer.launch/)
-
fn
function (): any The function to run. This will be passed inpage.evaluate(fn)
Returns any The result value of page.evaluate(fn)
All contributions are welcome!
MIT © 2021 stagas