diff --git a/packages/client/src/utils.js b/packages/client/src/utils.js index 775963e9b..369c14a59 100644 --- a/packages/client/src/utils.js +++ b/packages/client/src/utils.js @@ -135,8 +135,8 @@ export async function request(url, options = {}, callback) { let { protocol, hostname, port, pathname, search, hash } = new URL(url); // reference the default export so tests can mock it - // bundling cli inside electron (LCNC) fails if we import it like - // this: await import(protocol === 'https:' ? 'https' : 'http'); + // bundling cli inside electron or another package fails if we import it + // like this: await import(protocol === 'https:' ? 'https' : 'http'); let { default: http } = protocol === 'https:' ? await import('https') : await import('http'); let { proxyAgentFor } = await import('./proxy.js'); diff --git a/packages/core/src/api.js b/packages/core/src/api.js index dd9a3e3ed..0ba613c11 100644 --- a/packages/core/src/api.js +++ b/packages/core/src/api.js @@ -1,13 +1,22 @@ import fs from 'fs'; import path from 'path'; -import { createRequire } from 'module'; import logger from '@percy/logger'; import { normalize } from '@percy/config/utils'; import { getPackageJSON, Server, percyAutomateRequestHandler, percyBuildEventHandler } from './utils.js'; import WebdriverUtils from '@percy/webdriver-utils'; import { handleSyncJob } from './snapshot.js'; -// need require.resolve until import.meta.resolve can be transpiled -export const PERCY_DOM = createRequire(import.meta.url).resolve('@percy/dom'); +// Previously, we used `createRequire(import.meta.url).resolve` to resolve the path to the module. +// This approach relied on `createRequire`, which is Node.js-specific and less compatible with modern ESM (ECMAScript Module) standards. +// This was leading to hard coded paths when CLI is used as a dependency in another project. +// Now, we use `fileURLToPath` and `path.resolve` to determine the absolute path in a way that's more aligned with ESM conventions. +// This change ensures better compatibility and avoids relying on Node.js-specific APIs that might cause issues in ESM environments. +import { fileURLToPath } from 'url'; +import { dirname, resolve } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +export const PERCY_DOM = resolve(__dirname, 'node_modules/@percy/dom'); // Returns a URL encoded string of nested query params function encodeURLSearchParams(subj, prefix) {