From 45ed53f8fe8de9db432846df220b04745996da21 Mon Sep 17 00:00:00 2001 From: Christian Bromann Date: Fri, 2 Feb 2024 23:46:28 -0800 Subject: [PATCH] add docs --- .npmignore | 3 +++ README.md | 41 ++++++++++++++++++++++++++++++++++++++-- examples/consoleLog.html | 2 +- src/runner.ts | 16 +++++++--------- src/server.ts | 5 +++-- 5 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 .npmignore diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..b328f71 --- /dev/null +++ b/.npmignore @@ -0,0 +1,3 @@ +src +*.json +examples \ No newline at end of file diff --git a/README.md b/README.md index f863145..d6a2af0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,39 @@ -# bx -Command line tool to run JavaScript, TypeScript or HTML files in the browser. +# 🌐 exweb - a browser runner + +With Node.js, Deno or Bun there are so many JavaScript environments to choose from. However, nothing is as good as the browser environment. `exweb` gives you an execution runtime for the browser. + +For example, let's say you have a script like this: + +```js +console.log(navigator.userAgent) +``` + +With `exweb` you can easily run this script within different browser environments: + +```sh +> npx exweb ./script.js +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 +``` + +You can easily switch browser via the `--browserName` parameter: + +```sh +> npx exweb ./script.js --browserName firefox +Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0 +``` + +It even allows you to run `.html` files, e.g. given this file: + +```html + +Hello World! +``` + +Running this with `exweb` results in: + +```sh +> npx exweb ./html.html +Hello World! +``` \ No newline at end of file diff --git a/examples/consoleLog.html b/examples/consoleLog.html index 9b6786f..03c70b0 100644 --- a/examples/consoleLog.html +++ b/examples/consoleLog.html @@ -1,4 +1,4 @@ -!Hello World! +Hello World! diff --git a/src/runner.ts b/src/runner.ts index e6c4c36..7b23d84 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -1,7 +1,7 @@ import { remote } from 'webdriverio' import { getHeadlessArgs } from './utils.js' -import type { ErrorEvent, ExecutionEnvironment, RunnerArgs } from './types.js' +import type { ExecutionEnvironment, RunnerArgs } from './types.js' export async function run (env: ExecutionEnvironment, args: RunnerArgs) { const browser = await remote({ @@ -13,18 +13,16 @@ export async function run (env: ExecutionEnvironment, args: RunnerArgs) { }) let error: Error | undefined - env.server.ws.on('bx:event', (message: ErrorEvent) => { - if (message.name === 'errorEvent') { - error = new Error(message.message) - error.stack = message.error.replace(`http://localhost:${env.server.config.server.port}/@fs`, 'file://') - } - }) - browser.url(env.url) await new Promise((resolve) => { env.server.ws.on('bx:event', (message) => { + if (message.name === 'errorEvent') { + error = new Error(message.message) + error.stack = message.error.replace(`http://localhost:${env.server.config.server.port}/@fs`, 'file://') + return resolve() + } if (message.name === 'doneEvent') { - resolve() + return resolve() } }) }) diff --git a/src/server.ts b/src/server.ts index 91cacc8..e6fa376 100644 --- a/src/server.ts +++ b/src/server.ts @@ -48,14 +48,14 @@ export class ViteServer { function instrument (filename: string, onConnect: (value: ViteDevServer) => void): Plugin { const instrumentation = path.resolve(__dirname, 'browser', 'index.js') - + const sendFinishEvent = `import.meta.hot?.send('bx:event', { name: 'doneEvent' })` return { name: 'instrument', enforce: 'post', transform: (code, id) => { if (id === filename) { return { - code: `${code}\nimport.meta.hot?.send('bx:event', { name: 'doneEvent' })` + code: `${code}\n${sendFinishEvent}` } } return null @@ -77,6 +77,7 @@ function instrument (filename: string, onConnect: (value: ViteDevServer) => void ${code} + ${path.extname(filename) === '.html' ? `` : ''} ` res.end(await server.transformIndexHtml(`${req.originalUrl}`, template)) })