fix: cache dynamic imports (#8652)

This commit is contained in:
Alex Rudenko 2022-07-07 21:09:07 +02:00 committed by GitHub
parent d0c4291995
commit 1de0383abf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 4 deletions

View File

@ -29,6 +29,7 @@ import {TimeoutSettings} from './TimeoutSettings.js';
import {EvaluateFunc, HandleFor, NodeFor} from './types.js'; import {EvaluateFunc, HandleFor, NodeFor} from './types.js';
import { import {
debugError, debugError,
importFS,
isNumber, isNumber,
isString, isString,
makePredicateString, makePredicateString,
@ -429,7 +430,7 @@ export class DOMWorld {
if (path !== null) { if (path !== null) {
let fs: typeof import('fs').promises; let fs: typeof import('fs').promises;
try { try {
fs = (await import('fs')).promises; fs = (await importFS()).promises;
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {
throw new Error( throw new Error(

View File

@ -21,6 +21,20 @@ declare global {
var __PUPPETEER_DEBUG: string; var __PUPPETEER_DEBUG: string;
} }
/**
* @internal
*/
let debugModule: typeof import('debug') | null = null;
/**
* @internal
*/
export async function importDebug(): Promise<typeof import('debug')> {
if (!debugModule) {
debugModule = (await import('debug')).default;
}
return debugModule;
}
/** /**
* A debug function that can be used in any environment. * A debug function that can be used in any environment.
* *
@ -61,7 +75,7 @@ declare global {
export const debug = (prefix: string): ((...args: unknown[]) => void) => { export const debug = (prefix: string): ((...args: unknown[]) => void) => {
if (isNode) { if (isNode) {
return async (...logArgs: unknown[]) => { return async (...logArgs: unknown[]) => {
(await import('debug')).default(prefix)(logArgs); (await importDebug())(prefix)(logArgs);
}; };
} }

View File

@ -55,6 +55,7 @@ import {
debugError, debugError,
evaluationString, evaluationString,
getExceptionMessage, getExceptionMessage,
importFS,
getReadableAsBuffer, getReadableAsBuffer,
getReadableFromProtocolStream, getReadableFromProtocolStream,
isErrorLike, isErrorLike,
@ -2920,7 +2921,7 @@ export class Page extends EventEmitter {
if (options.path) { if (options.path) {
try { try {
const fs = (await import('fs')).promises; const fs = (await importFS()).promises;
await fs.writeFile(options.path, buffer); await fs.writeFile(options.path, buffer);
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {

View File

@ -412,6 +412,20 @@ export async function waitWithTimeout<T>(
} }
} }
/**
* @internal
*/
let fs: typeof import('fs') | null = null;
/**
* @internal
*/
export async function importFS(): Promise<typeof import('fs')> {
if (!fs) {
fs = await import('fs');
}
return fs;
}
/** /**
* @internal * @internal
*/ */
@ -423,7 +437,7 @@ export async function getReadableAsBuffer(
if (path) { if (path) {
let fs: typeof import('fs').promises; let fs: typeof import('fs').promises;
try { try {
fs = (await import('fs')).promises; fs = (await importFS()).promises;
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {
throw new Error( throw new Error(