Skip to content

Commit

Permalink
fix: cache dynamic imports (#8652)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Jul 7, 2022
1 parent d0c4291 commit 1de0383
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/common/DOMWorld.ts
Expand Up @@ -29,6 +29,7 @@ import {TimeoutSettings} from './TimeoutSettings.js';
import {EvaluateFunc, HandleFor, NodeFor} from './types.js';
import {
debugError,
importFS,
isNumber,
isString,
makePredicateString,
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 15 additions & 1 deletion src/common/Debug.ts
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/common/Page.ts
Expand Up @@ -55,6 +55,7 @@ import {
debugError,
evaluationString,
getExceptionMessage,
importFS,
getReadableAsBuffer,
getReadableFromProtocolStream,
isErrorLike,
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion src/common/util.ts
Expand Up @@ -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
*/
Expand All @@ -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(
Expand Down

0 comments on commit 1de0383

Please sign in to comment.