Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache dynamic imports #8652

Merged
merged 1 commit into from Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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