* The page source of one script. A function is called with its JSON argument * as Playwright calls it, beside the `__name` helper tsx's output needs; the * argument is parsed in the page, since an object literal would turn an own * `__proto__` key into the prototype. A * file gets a `sourceURL`,
( script: WebInitScript, arg: JsonValue | undefined, resolvePath: (file: string) => string, )
| 100 | * file gets a `sourceURL`, so a stack trace in the page names it. |
| 101 | */ |
| 102 | async function initScriptSource( |
| 103 | script: WebInitScript, |
| 104 | arg: JsonValue | undefined, |
| 105 | resolvePath: (file: string) => string, |
| 106 | ): Promise<string> { |
| 107 | if (typeof script === 'string') return script; |
| 108 | if (typeof script === 'function') { |
| 109 | return `(() => {\n${KEEP_NAMES_HELPER}\n(${script.toString()}\n)(${arg === undefined ? '' : `JSON.parse(${JSON.stringify(JSON.stringify(arg))})`});\n})();`; |
| 110 | } |
| 111 | const file = resolvePath(script.path); |
| 112 | try { |
| 113 | return `${await readFile(file, 'utf8')}\n//# sourceURL=${file.replace(/[\r\n]/g, '')}`; |
| 114 | } catch (cause) { |
| 115 | throw new Error(`path cannot be read: ${file} (${(cause as NodeJS.ErrnoException).code ?? message(cause)})`, { cause }); |
| 116 | } |
| 117 | } |
no test coverage detected