From 3600f2f99bce74da402fb43bf1b7bb04f6f35714 Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Tue, 21 Apr 2020 10:22:20 +0100 Subject: [PATCH] chore: migrate src/helpers.ts to ESM (#5699) * chore: migrate src/helpers.ts to ESM Doing this means we can avoid the global `types.d.ts` file and export the interface via ESM instead. I would ideally like to rewrite the helper module so that it doesn't export all the functions under the `helper` namespace, but I'll leave that for a separate PR to keep mechanical changes to one per PR and easier to review. --- src/Connection.ts | 3 +-- src/Dialog.ts | 4 +--- src/PipeTransport.ts | 3 +-- src/helper.ts | 42 ++++++++++++++++++++++-------------------- src/types.d.ts | 25 ------------------------- 5 files changed, 25 insertions(+), 52 deletions(-) delete mode 100644 src/types.d.ts diff --git a/src/Connection.ts b/src/Connection.ts index cb55228469ff1..04329d1336b0d 100644 --- a/src/Connection.ts +++ b/src/Connection.ts @@ -13,8 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import helper = require('./helper'); -const {assert} = helper; +import {assert} from './helper'; import EventsModule = require('./Events'); const {Events} = EventsModule; diff --git a/src/Dialog.ts b/src/Dialog.ts index 91204f25574d4..792c37700ceab 100644 --- a/src/Dialog.ts +++ b/src/Dialog.ts @@ -14,11 +14,9 @@ * limitations under the License. */ -import helpers = require('./helper'); +import {assert} from './helper'; import {CDPSession} from './Connection'; -const {assert} = helpers; - enum DialogType { Alert = 'alert', BeforeUnload = 'beforeunload', diff --git a/src/PipeTransport.ts b/src/PipeTransport.ts index d23ef0965173b..b997ee5dc0687 100644 --- a/src/PipeTransport.ts +++ b/src/PipeTransport.ts @@ -13,8 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import helperUtils = require('./helper'); -const {helper, debugError} = helperUtils; +import {helper, debugError, PuppeteerEventListener} from './helper'; class PipeTransport implements Puppeteer.ConnectionTransport { _pipeWrite: NodeJS.WritableStream; diff --git a/src/helper.ts b/src/helper.ts index b3e9e8fe4b977..fb9fa265b3c26 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -16,7 +16,6 @@ import Errors = require('./Errors'); import debug = require('debug'); -const debugError = debug('puppeteer:error'); import fs = require('fs'); import promisify = require('./promisify'); import {CDPSession} from './Connection'; @@ -27,7 +26,9 @@ const openAsync = promisify(fs.open); const writeAsync = promisify(fs.write); const closeAsync = promisify(fs.close); -function assert(value: unknown, message?: string): void { +export const debugError = debug('puppeteer:error'); + +export function assert(value: unknown, message?: string): void { if (!value) throw new Error(message); } @@ -103,6 +104,11 @@ function installAsyncStackHooks(classType: AnyClass): void { } } +export interface PuppeteerEventListener { + emitter: NodeJS.EventEmitter; + eventName: string | symbol; + handler: (...args: any[]) => void; +} function addEventListener(emitter: NodeJS.EventEmitter, eventName: string|symbol, handler: (...args: any[]) => void): PuppeteerEventListener { emitter.on(eventName, handler); @@ -212,22 +218,18 @@ async function readProtocolStream(client: CDPSession, handle: string, path?: str } -export = { - helper: { - promisify, - evaluationString, - readProtocolStream, - waitWithTimeout, - waitForEvent, - isString, - isNumber, - addEventListener, - removeEventListeners, - valueFromRemoteObject, - installAsyncStackHooks, - getExceptionMessage, - releaseObject, - }, - assert, - debugError +export const helper = { + promisify, + evaluationString, + readProtocolStream, + waitWithTimeout, + waitForEvent, + isString, + isNumber, + addEventListener, + removeEventListeners, + valueFromRemoteObject, + installAsyncStackHooks, + getExceptionMessage, + releaseObject, }; diff --git a/src/types.d.ts b/src/types.d.ts deleted file mode 100644 index 8be41bae854cd..0000000000000 --- a/src/types.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright 2020 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* These types exist here until we migrate over to ESM where we can - * import / export them properly from modules - TS doesn't support - * exposing interfaces in CommonJS land. - */ -interface PuppeteerEventListener { - emitter: NodeJS.EventEmitter; - eventName: string | symbol; - handler: (...args: any[]) => void; -}