Skip to content

Commit 7be7bfd

Browse files
committed
feat: replace lwr with lwc-dev-server
1 parent b91e2f1 commit 7be7bfd

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/commands/lightning/dev/component.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77

88
import path from 'node:path';
99
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
10-
import { Messages, SfProject } from '@salesforce/core';
10+
import { Messages, SfProject, Logger } from '@salesforce/core';
11+
import { Platform } from '@salesforce/lwc-dev-mobile-core';
1112
import { ComponentUtils } from '../../../shared/componentUtils.js';
1213
import { PromptUtils } from '../../../shared/promptUtils.js';
1314
import { PreviewUtils } from '../../../shared/previewUtils.js';
15+
import { startLWCServer } from '../../../lwc-dev-server/index.js';
1416

1517
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1618
const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'lightning.dev.component');
1719

18-
// TODO: generate ldp server url
19-
const ldpServerUrl = 'http://localhost:3000';
20-
2120
export default class LightningDevComponent extends SfCommand<void> {
2221
public static readonly summary = messages.getMessage('summary');
2322
public static readonly description = messages.getMessage('description');
@@ -39,13 +38,30 @@ export default class LightningDevComponent extends SfCommand<void> {
3938

4039
public async run(): Promise<void> {
4140
const { flags } = await this.parse(LightningDevComponent);
41+
const logger = await Logger.child(this.ctor.name);
4242
const project = await SfProject.resolve();
4343

44+
let sfdxProjectRootPath = '';
45+
try {
46+
sfdxProjectRootPath = await SfProject.resolveProjectPath();
47+
} catch (error) {
48+
// return Promise.reject(new Error(messages.getMessage('error.no-project', [(error as Error)?.message ?? ''])));
49+
throw new Error('failed to resolve project path');
50+
}
51+
4452
let componentName = flags['name'];
4553
const clientSelect = flags['client-select'];
4654
const targetOrg = flags['target-org'];
4755

48-
const { ldpServerId } = await PreviewUtils.initializePreviewConnection(targetOrg);
56+
const { ldpServerId, ldpServerToken } = await PreviewUtils.initializePreviewConnection(targetOrg);
57+
58+
logger.debug('Determining the next available port for Local Dev Server');
59+
const serverPorts = await PreviewUtils.getNextAvailablePorts();
60+
logger.debug(`Next available ports are http=${serverPorts.httpPort} , https=${serverPorts.httpsPort}`);
61+
62+
logger.debug('Determining Local Dev Server url');
63+
const ldpServerUrl = PreviewUtils.generateWebSocketUrlForLocalDevServer(Platform.desktop, serverPorts, logger);
64+
logger.debug(`Local Dev Server url is ${ldpServerUrl}`);
4965

5066
const namespacePaths = await ComponentUtils.getNamespacePaths(project);
5167
const componentPaths = await ComponentUtils.getAllComponentPaths(namespacePaths);
@@ -101,7 +117,7 @@ export default class LightningDevComponent extends SfCommand<void> {
101117
}
102118
}
103119

104-
// TODO: launch the local dev server
120+
await startLWCServer(logger, sfdxProjectRootPath, ldpServerToken, Platform.desktop, serverPorts);
105121

106122
const targetOrgArg = PreviewUtils.getTargetOrgFromArguments(this.argv);
107123
const launchArguments = PreviewUtils.generateComponentPreviewLaunchArguments(

src/lwc-dev-server/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ async function createLWCServerConfig(
3030
const { namespace } = projectJson;
3131

3232
// e.g. lwc folders in force-app/main/default/lwc, package-dir/lwc
33-
const namespacePaths = (await Promise.all(packageDirs.map((dir) => glob(`${dir.fullPath}/**/lwc`, { absolute: true })))).flat();
33+
const namespacePaths = (
34+
await Promise.all(packageDirs.map((dir) => glob(`${dir.fullPath}/**/lwc`, { absolute: true })))
35+
).flat();
3436

3537
const ports = serverPorts ??
3638
(await ConfigUtils.getLocalDevServerPorts()) ?? {
@@ -46,6 +48,7 @@ async function createLWCServerConfig(
4648
// use custom workspace if any is provided, or fetch from config file (if any), otherwise use the default workspace
4749
workspace: workspace ?? (await ConfigUtils.getLocalDevServerWorkspace()) ?? LOCAL_DEV_SERVER_DEFAULT_WORKSPACE,
4850
identityToken: token,
51+
// @ts-expect-error - TEMP: there is dependency mismatch when linking a local copy of the lwc-dev-server
4952
lifecycle: Lifecycle.getInstance(),
5053
clientType,
5154
namespace: typeof namespace === 'string' && namespace.trim().length > 0 ? namespace.trim() : undefined,
@@ -74,6 +77,7 @@ export async function startLWCServer(
7477
const config = await createLWCServerConfig(rootDir, token, clientType, serverPorts, certData, workspace);
7578

7679
logger.trace(`Starting LWC Dev Server with config: ${JSON.stringify(config)}`);
80+
// @ts-expect-error - TEMP: there is dependency mismatch when linking a local copy of the lwc-dev-server
7781
let lwcDevServer: LWCServer | null = await startLwcDevServer(config, logger);
7882

7983
const cleanup = (): void => {

src/shared/previewUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,11 @@ export class PreviewUtils {
230230
targetOrg?: string
231231
): string[] {
232232
// TODO: vanity application target
233-
let appPath = `lwr/application/ai/${encodeURIComponent(
233+
let appPath = `lwr/application/e/devpreview/ai/${encodeURIComponent(
234234
'localdev%2Fpreview'
235235
)}?ldpServerUrl=${ldpServerUrl}&ldpServerId=${ldpServerId}`;
236236
if (componentName) {
237+
// TODO: support other namespaces
237238
appPath += `&specifier=c/${componentName}`;
238239
}
239240

0 commit comments

Comments
 (0)