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

Cli bundling import fix #1785

Merged
merged 7 commits into from
Nov 20, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/client/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
15 changes: 12 additions & 3 deletions packages/core/src/api.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import fs from 'fs';
import path from 'path';

Check failure on line 2 in packages/core/src/api.js

View workflow job for this annotation

GitHub Actions / Lint

'path' imported multiple times
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';

Check failure on line 14 in packages/core/src/api.js

View workflow job for this annotation

GitHub Actions / Lint

'path' imported multiple times

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) {
Expand Down
Loading