diff --git a/src/common/DOMWorld.ts b/src/common/DOMWorld.ts index b4db05dc..52b7ca99 100644 --- a/src/common/DOMWorld.ts +++ b/src/common/DOMWorld.ts @@ -29,6 +29,7 @@ import {TimeoutSettings} from './TimeoutSettings.js'; import {EvaluateFunc, HandleFor, NodeFor} from './types.js'; import { debugError, + importFS, isNumber, isString, makePredicateString, @@ -429,7 +430,7 @@ export class DOMWorld { if (path !== null) { let fs: typeof import('fs').promises; try { - fs = (await import('fs')).promises; + fs = (await importFS()).promises; } catch (error) { if (error instanceof TypeError) { throw new Error( diff --git a/src/common/Debug.ts b/src/common/Debug.ts index 3aaa9806..21db3566 100644 --- a/src/common/Debug.ts +++ b/src/common/Debug.ts @@ -21,6 +21,20 @@ declare global { var __PUPPETEER_DEBUG: string; } +/** + * @internal + */ +let debugModule: typeof import('debug') | null = null; +/** + * @internal + */ +export async function importDebug(): Promise { + if (!debugModule) { + debugModule = (await import('debug')).default; + } + return debugModule; +} + /** * A debug function that can be used in any environment. * @@ -61,7 +75,7 @@ declare global { export const debug = (prefix: string): ((...args: unknown[]) => void) => { if (isNode) { return async (...logArgs: unknown[]) => { - (await import('debug')).default(prefix)(logArgs); + (await importDebug())(prefix)(logArgs); }; } diff --git a/src/common/Page.ts b/src/common/Page.ts index f374e831..1ba4d411 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -55,6 +55,7 @@ import { debugError, evaluationString, getExceptionMessage, + importFS, getReadableAsBuffer, getReadableFromProtocolStream, isErrorLike, @@ -2920,7 +2921,7 @@ export class Page extends EventEmitter { if (options.path) { try { - const fs = (await import('fs')).promises; + const fs = (await importFS()).promises; await fs.writeFile(options.path, buffer); } catch (error) { if (error instanceof TypeError) { diff --git a/src/common/util.ts b/src/common/util.ts index 63c987bd..d9878ec1 100644 --- a/src/common/util.ts +++ b/src/common/util.ts @@ -412,6 +412,20 @@ export async function waitWithTimeout( } } +/** + * @internal + */ +let fs: typeof import('fs') | null = null; +/** + * @internal + */ +export async function importFS(): Promise { + if (!fs) { + fs = await import('fs'); + } + return fs; +} + /** * @internal */ @@ -423,7 +437,7 @@ export async function getReadableAsBuffer( if (path) { let fs: typeof import('fs').promises; try { - fs = (await import('fs')).promises; + fs = (await importFS()).promises; } catch (error) { if (error instanceof TypeError) { throw new Error(