diff --git a/package.json b/package.json index 6a0c2d4..c215850 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,13 @@ "license": "MIT", "author": "Federico Brigante for PixieBrix (https://www.pixiebrix.com)", "type": "module", - "main": "distribution/index.js", + "exports": { + ".": { + "types": "./distribution/index.js", + "default": "./distribution/index.js" + }, + "./*": "./distribution/*" + }, "scripts": { "build": "tsc", "demo:watch": "parcel watch --no-cache --no-hmr", diff --git a/source/index.ts b/source/index.ts index 583fe7f..d1f96d5 100644 --- a/source/index.ts +++ b/source/index.ts @@ -4,7 +4,7 @@ export * from "./receiver.js"; export * from "./sender.js"; export * from "./types.js"; export { getThisFrame, getTopLevelFrame } from "./thisTarget.js"; -export { toggleLogging } from "./shared.js"; +export { toggleLogging } from "./logging.js"; import { initPrivateApi } from "./thisTarget.js"; diff --git a/source/logging.ts b/source/logging.ts new file mode 100644 index 0000000..901c69d --- /dev/null +++ b/source/logging.ts @@ -0,0 +1,17 @@ +/* Warning: Do not use import browser-polyfill directly or indirectly */ + +// .bind preserves the call location in the console +const debug = console.debug.bind(console, "Messenger:"); +const warn = console.warn.bind(console, "Messenger:"); + +const noop: (...args: unknown[]) => void = () => { + /* */ +}; + +// Default to "no logs" +export const log = { debug: noop, warn: noop }; + +export function toggleLogging(enabled: boolean): void { + log.debug = enabled ? debug : noop; + log.warn = enabled ? warn : noop; +} diff --git a/source/receiver.ts b/source/receiver.ts index e87e773..453acb9 100644 --- a/source/receiver.ts +++ b/source/receiver.ts @@ -8,7 +8,8 @@ import { type Method, type Sender, } from "./types.js"; -import { isObject, MessengerError, log, __webextMessenger } from "./shared.js"; +import { isObject, MessengerError, __webextMessenger } from "./shared.js"; +import { log } from "./logging.js"; import { getActionForMessage } from "./thisTarget.js"; import { didUserRegisterMethods, handlers } from "./handlers.js"; diff --git a/source/sender.ts b/source/sender.ts index 26b387c..cca5bb9 100644 --- a/source/sender.ts +++ b/source/sender.ts @@ -12,7 +12,8 @@ import { type PageTarget, type AnyTarget, } from "./types.js"; -import { isObject, MessengerError, __webextMessenger, log } from "./shared.js"; +import { isObject, MessengerError, __webextMessenger } from "./shared.js"; +import { log } from "./logging.js"; import { type SetReturnType } from "type-fest"; import { handlers } from "./handlers.js"; diff --git a/source/shared.ts b/source/shared.ts index 5d18ddb..324d269 100644 --- a/source/shared.ts +++ b/source/shared.ts @@ -13,10 +13,6 @@ export function isObject(value: unknown): value is Record { return typeof value === "object" && value !== null; } -function noop() { - /* */ -} - export class MessengerError extends Error { override name = "MessengerError"; } @@ -24,20 +20,6 @@ export class MessengerError extends Error { // @ts-expect-error Wrong `errorConstructors` types errorConstructors.set("MessengerError", MessengerError); -// .bind preserves the call location in the console -const debug = console.debug.bind(console, "Messenger:"); -const warn = console.warn.bind(console, "Messenger:"); - -export const log = { debug, warn }; - -export function toggleLogging(enabled: boolean): void { - log.debug = enabled ? debug : noop; - log.warn = enabled ? warn : noop; -} - -// Default to "no logs" -toggleLogging(false); - export function isErrorObject(error: unknown): error is ErrorObject { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is a type guard function and it uses ?. return typeof (error as any)?.message === "string"; diff --git a/source/thisTarget.ts b/source/thisTarget.ts index 6bd6fe5..a20d387 100644 --- a/source/thisTarget.ts +++ b/source/thisTarget.ts @@ -14,7 +14,8 @@ import { type Sender, type FrameTarget, } from "./types.js"; -import { log, MessengerError, once } from "./shared.js"; +import { MessengerError, once } from "./shared.js"; +import { log } from "./logging.js"; import { type Entries } from "type-fest"; /** @@ -152,7 +153,7 @@ export function __getTabData(this: MessengerMeta): AnyTarget { } export async function getThisFrame(): Promise { - await storeTabData(); // It should already have been called by we still need to await it + await storeTabData(); // It should already have been called but we still need to await it const { tabId, frameId } = thisTarget;