-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support ts config files * chore: changeset
- Loading branch information
Showing
4 changed files
with
109 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
"@heymp/scratchpad": minor | ||
--- | ||
|
||
Support ts config files | ||
|
||
`scratchpad.config.ts` | ||
```ts | ||
import { Config } from '@heymp/scratchpad/src/config.js'; | ||
|
||
export function hi(name: string) { | ||
console.log(`Hi there ${name}`); | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
hi: typeof hi; | ||
} | ||
} | ||
|
||
export default ({ | ||
playwright: async (args) => { | ||
const { context } = args; | ||
await context.exposeFunction('hi', hi); | ||
} | ||
}) satisfies Config; | ||
``` | ||
|
||
`test.ts` | ||
```.ts | ||
/// <reference path="./scratchpad.config.ts" /> | ||
|
||
window.hi('Bob'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { stat } from 'node:fs/promises'; | ||
|
||
/** | ||
* Helper function to check if a file exists | ||
*/ | ||
export const exists = (path: string) => stat(path).then(() => true, () => false); | ||
|
||
/** | ||
* Template Literal function that converts an string | ||
* containing ESM javascript to data URI. | ||
* | ||
* @example | ||
* const m1 = esm`export function f() { return 'Hello!' }`; | ||
* const m2 = esm`import {f} from '${m1}'; export default f()+f();`; | ||
* import(m1) | ||
*/ | ||
export function esm(templateStrings: TemplateStringsArray, ...substitutions: any[]): string { | ||
let js = templateStrings.raw[0]; | ||
for (let i = 0; i < substitutions.length; i++) { | ||
js += substitutions[i] + templateStrings.raw[i + 1]; | ||
} | ||
return 'data:text/javascript;base64,' + btoa(js); | ||
} |