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 {
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(

View File

@ -21,6 +21,20 @@ declare global {
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.
*
@ -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);
};
}

View File

@ -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) {

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
*/
@ -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(