From e82beaccacc3a8126f0eae9bb661bbd6e0aa7cca Mon Sep 17 00:00:00 2001 From: Khushhal Maheshwari <25052873+khushhalm@users.noreply.github.com> Date: Wed, 13 Nov 2024 02:29:59 +0530 Subject: [PATCH 1/8] Fix import in ternary statement --- packages/client/src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/utils.js b/packages/client/src/utils.js index 9bb0f81fd..13f5e7fb0 100644 --- a/packages/client/src/utils.js +++ b/packages/client/src/utils.js @@ -135,7 +135,7 @@ 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 - let { default: http } = await import(protocol === 'https:' ? 'https' : 'http'); + let { default: http } = protocol === 'https:' ? await import('https') : await import('http'); let { proxyAgentFor } = await import('./proxy.js'); // automatically stringify body content From 43a7cfcd7b87c50fbdf5bba997b07a17c43665d2 Mon Sep 17 00:00:00 2001 From: Khushhal Maheshwari <25052873+khushhalm@users.noreply.github.com> Date: Mon, 18 Nov 2024 17:54:43 +0530 Subject: [PATCH 2/8] Added comment --- packages/client/src/utils.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/client/src/utils.js b/packages/client/src/utils.js index 13f5e7fb0..b1d719f96 100644 --- a/packages/client/src/utils.js +++ b/packages/client/src/utils.js @@ -135,6 +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 insdie electron (LCNC) 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'); From c57ecc2c75ca7138e3588dd5497372a6358cbe97 Mon Sep 17 00:00:00 2001 From: Khushhal Maheshwari <25052873+khushhalm@users.noreply.github.com> Date: Tue, 19 Nov 2024 11:26:47 +0530 Subject: [PATCH 3/8] Typo fix in comment Co-authored-by: ninadbstack <60422475+ninadbstack@users.noreply.github.com> --- packages/client/src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/utils.js b/packages/client/src/utils.js index b1d719f96..bae3d76f7 100644 --- a/packages/client/src/utils.js +++ b/packages/client/src/utils.js @@ -135,7 +135,7 @@ 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 insdie electron (LCNC) fails if we import it like + // bundling cli inside electron (LCNC) 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'); From 45375f93e63b2b856cdf2d4e92102cfcacb9a823 Mon Sep 17 00:00:00 2001 From: Khushhal Maheshwari <25052873+khushhalm@users.noreply.github.com> Date: Tue, 19 Nov 2024 12:02:01 +0530 Subject: [PATCH 4/8] Removed trailing space --- packages/client/src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/utils.js b/packages/client/src/utils.js index bae3d76f7..775963e9b 100644 --- a/packages/client/src/utils.js +++ b/packages/client/src/utils.js @@ -135,7 +135,7 @@ 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 + // bundling cli inside electron (LCNC) 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'); From b78953ce0e0a655df453c2ded9e6dc759314776c Mon Sep 17 00:00:00 2001 From: Khushhal Maheshwari <25052873+khushhalm@users.noreply.github.com> Date: Wed, 20 Nov 2024 09:04:50 +0530 Subject: [PATCH 5/8] Fix import of PERCY_DOM in line w ESM req --- packages/core/src/api.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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) { From 81ae1ac537f380c15c914a91bb9aed8d90652c2b Mon Sep 17 00:00:00 2001 From: Khushhal Maheshwari <25052873+khushhalm@users.noreply.github.com> Date: Wed, 20 Nov 2024 09:15:20 +0530 Subject: [PATCH 6/8] made a minor comment change --- packages/client/src/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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'); From f5d7881d866b34ff0b99d44281e97effcaf2864a Mon Sep 17 00:00:00 2001 From: Khushhal Maheshwari <25052873+khushhalm@users.noreply.github.com> Date: Tue, 26 Nov 2024 00:05:15 +0530 Subject: [PATCH 7/8] Added PERCY_FORCE_DIRNAME flag in getPackageJSON util method --- packages/client/src/utils.js | 3 +++ packages/core/src/api.js | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/client/src/utils.js b/packages/client/src/utils.js index 369c14a59..3434413b3 100644 --- a/packages/client/src/utils.js +++ b/packages/client/src/utils.js @@ -37,6 +37,9 @@ export function waitForTimeout() { // Returns the package.json content at the package path. export function getPackageJSON(rel) { + /* istanbul ignore else: sanity check */ + if (process.env.PERCY_FORCE_DIRNAME) rel = __dirname; + /* istanbul ignore else: sanity check */ if (rel.startsWith('file:')) rel = url.fileURLToPath(rel); diff --git a/packages/core/src/api.js b/packages/core/src/api.js index 0ba613c11..7f682d639 100644 --- a/packages/core/src/api.js +++ b/packages/core/src/api.js @@ -1,5 +1,5 @@ import fs from 'fs'; -import path from 'path'; +import path, { dirname, resolve } from 'path'; import logger from '@percy/logger'; import { normalize } from '@percy/config/utils'; import { getPackageJSON, Server, percyAutomateRequestHandler, percyBuildEventHandler } from './utils.js'; @@ -11,7 +11,6 @@ import { handleSyncJob } from './snapshot.js'; // 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); From 3af6e20e5b806a19637be771d5f2b2f99caf1f30 Mon Sep 17 00:00:00 2001 From: Khushhal Maheshwari <25052873+khushhalm@users.noreply.github.com> Date: Tue, 26 Nov 2024 11:19:17 +0530 Subject: [PATCH 8/8] Env var name updated to PERCY_FORCE_EXECUTABLE_DIRNAME Co-authored-by: ninadbstack <60422475+ninadbstack@users.noreply.github.com> --- packages/client/src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/utils.js b/packages/client/src/utils.js index 3434413b3..7536aa9a3 100644 --- a/packages/client/src/utils.js +++ b/packages/client/src/utils.js @@ -38,7 +38,7 @@ export function waitForTimeout() { // Returns the package.json content at the package path. export function getPackageJSON(rel) { /* istanbul ignore else: sanity check */ - if (process.env.PERCY_FORCE_DIRNAME) rel = __dirname; + if (process.env.PERCY_FORCE_EXECUTABLE_DIRNAME) rel = __dirname; /* istanbul ignore else: sanity check */ if (rel.startsWith('file:')) rel = url.fileURLToPath(rel);