Skip to content

Commit 86efeb1

Browse files
committed
fix: simplify dependency loader
1 parent 7b6cde7 commit 86efeb1

3 files changed

Lines changed: 58 additions & 86 deletions

File tree

src/lwc-dev-server/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { LWCServer, ServerConfig, Workspace } from '@lwc/lwc-dev-server';
1919
import { Connection, Lifecycle, Logger, SfProject } from '@salesforce/core';
2020
import { SSLCertificateData } from '@salesforce/lwc-dev-mobile-core';
2121
import { glob } from 'glob';
22-
import { DependencyLoader } from '../shared/dependencyLoader.js';
22+
import { loadLwcDevServer } from '../shared/dependencyLoader.js';
2323
import { OrgUtils } from '../shared/orgUtils.js';
2424
import { VersionChannel } from '../shared/versionResolver.js';
2525
import {
@@ -34,7 +34,7 @@ async function createLWCServerConfig(
3434
clientType: string,
3535
serverPorts?: { httpPort: number; httpsPort: number },
3636
certData?: SSLCertificateData,
37-
workspace?: Workspace
37+
workspace?: Workspace,
3838
): Promise<ServerConfig> {
3939
const project = await SfProject.resolve();
4040
const packageDirs = project.getPackageDirectories();
@@ -85,12 +85,12 @@ export async function startLWCServer(
8585
serverPorts?: { httpPort: number; httpsPort: number },
8686
certData?: SSLCertificateData,
8787
workspace?: Workspace,
88-
versionChannelOverride?: VersionChannel
88+
versionChannelOverride?: VersionChannel,
8989
): Promise<LWCServer> {
9090
const channel = OrgUtils.getVersionChannel(connection, versionChannelOverride);
9191
logger.trace(`Using version channel: ${channel}`);
9292

93-
const lwcDevServerModule = await DependencyLoader.loadLwcDevServer(channel);
93+
const lwcDevServerModule = await loadLwcDevServer(channel);
9494

9595
const config = await createLWCServerConfig(rootDir, token, clientType, serverPorts, certData, workspace);
9696

src/shared/dependencyLoader.ts

Lines changed: 49 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -27,85 +27,62 @@ export type LwcDevServerModule = {
2727
};
2828

2929
/**
30-
* Dynamically loads LWC dependencies based on version channel
30+
* Loads the LWC dev server module for the specified channel
31+
* Uses dynamic import to load the aliased package at runtime
32+
*
33+
* @param channel - The version channel ('latest', 'prerelease', or 'next')
34+
* @returns The loaded module
3135
*/
32-
export class DependencyLoader {
33-
private static loadedModules: Map<VersionChannel, LwcDevServerModule> = new Map();
34-
35-
/**
36-
* Loads the LWC dev server module for the specified channel
37-
* Uses dynamic import to load the aliased package at runtime
38-
*
39-
* @param channel - The version channel ('latest' or 'prerelease')
40-
* @returns The loaded module
41-
*/
42-
public static async loadLwcDevServer(channel: VersionChannel): Promise<LwcDevServerModule> {
43-
// Check cache first
44-
if (this.loadedModules.has(channel)) {
45-
return this.loadedModules.get(channel)!;
46-
}
36+
export async function loadLwcDevServer(channel: VersionChannel): Promise<LwcDevServerModule> {
37+
const packageName = `@lwc/lwc-dev-server-${channel}`;
4738

48-
// Construct the aliased package name
49-
const packageName = `@lwc/lwc-dev-server-${channel}`;
50-
51-
try {
52-
// Dynamic import of the aliased package
53-
const module = (await import(packageName)) as LwcDevServerModule;
54-
this.loadedModules.set(channel, module);
55-
return module;
56-
} catch (error) {
57-
throw new Error(
58-
`Failed to load LWC dev server for channel '${channel}'. ` +
59-
`Package '${packageName}' could not be imported. ` +
60-
`Error: ${error instanceof Error ? error.message : String(error)}`
61-
);
62-
}
39+
try {
40+
return (await import(packageName)) as LwcDevServerModule;
41+
} catch (error) {
42+
throw new Error(
43+
`Failed to load LWC dev server for channel '${channel}'. ` +
44+
`Package '${packageName}' could not be imported. ` +
45+
`Error: ${error instanceof Error ? error.message : String(error)}`,
46+
);
6347
}
48+
}
6449

65-
/**
66-
* Loads the LWC compiler module for the specified channel
67-
*
68-
* @param channel - The version channel ('latest' or 'prerelease')
69-
* @returns The loaded compiler module
70-
*/
71-
public static async loadLwcCompiler(channel: VersionChannel): Promise<unknown> {
72-
const packageName = `@lwc/sfdc-lwc-compiler-${channel}`;
50+
/**
51+
* Loads the LWC compiler module for the specified channel
52+
*
53+
* @param channel - The version channel ('latest', 'prerelease', or 'next')
54+
* @returns The loaded compiler module
55+
*/
56+
export async function loadLwcCompiler(channel: VersionChannel): Promise<unknown> {
57+
const packageName = `@lwc/sfdc-lwc-compiler-${channel}`;
7358

74-
try {
75-
return (await import(packageName)) as unknown;
76-
} catch (error) {
77-
throw new Error(
78-
`Failed to load LWC compiler for channel '${channel}'. ` +
79-
`Package '${packageName}' could not be imported. ` +
80-
`Error: ${error instanceof Error ? error.message : String(error)}`
81-
);
82-
}
59+
try {
60+
return (await import(packageName)) as unknown;
61+
} catch (error) {
62+
throw new Error(
63+
`Failed to load LWC compiler for channel '${channel}'. ` +
64+
`Package '${packageName}' could not be imported. ` +
65+
`Error: ${error instanceof Error ? error.message : String(error)}`,
66+
);
8367
}
68+
}
8469

85-
/**
86-
* Loads the base LWC module for the specified channel
87-
*
88-
* @param channel - The version channel ('latest' or 'prerelease')
89-
* @returns The loaded LWC module
90-
*/
91-
public static async loadLwc(channel: VersionChannel): Promise<unknown> {
92-
const packageName = `lwc-${channel}`;
93-
94-
try {
95-
return (await import(packageName)) as unknown;
96-
} catch (error) {
97-
throw new Error(
98-
`Failed to load LWC for channel '${channel}'. ` +
99-
`Package '${packageName}' could not be imported. ` +
100-
`Error: ${error instanceof Error ? error.message : String(error)}`
101-
);
102-
}
103-
}
70+
/**
71+
* Loads the base LWC module for the specified channel
72+
*
73+
* @param channel - The version channel ('latest', 'prerelease', or 'next')
74+
* @returns The loaded LWC module
75+
*/
76+
export async function loadLwc(channel: VersionChannel): Promise<unknown> {
77+
const packageName = `lwc-${channel}`;
10478

105-
/**
106-
* Clears the module cache (useful for testing)
107-
*/
108-
public static clearCache(): void {
109-
this.loadedModules.clear();
79+
try {
80+
return (await import(packageName)) as unknown;
81+
} catch (error) {
82+
throw new Error(
83+
`Failed to load LWC for channel '${channel}'. ` +
84+
`Package '${packageName}' could not be imported. ` +
85+
`Error: ${error instanceof Error ? error.message : String(error)}`,
86+
);
11087
}
11188
}

test/shared/dependencyLoader.test.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,21 @@
1515
*/
1616

1717
import { expect } from 'chai';
18-
import { DependencyLoader } from '../../src/shared/dependencyLoader.js';
18+
import { loadLwcDevServer, loadLwcCompiler, loadLwc } from '../../src/shared/dependencyLoader.js';
1919

2020
describe('DependencyLoader', () => {
21-
beforeEach(() => {
22-
DependencyLoader.clearCache();
23-
});
24-
2521
it('exists and has expected methods', () => {
26-
expect(typeof DependencyLoader.loadLwcDevServer).to.equal('function');
27-
expect(typeof DependencyLoader.loadLwcCompiler).to.equal('function');
28-
expect(typeof DependencyLoader.loadLwc).to.equal('function');
29-
expect(typeof DependencyLoader.clearCache).to.equal('function');
22+
expect(typeof loadLwcDevServer).to.equal('function');
23+
expect(typeof loadLwcCompiler).to.equal('function');
24+
expect(typeof loadLwc).to.equal('function');
3025
});
3126

3227
it('loads the aliased package (real import call)', async () => {
3328
// This will actually try to call import() which should work since we ran yarn install.
3429
// However, loading LWC modules in Node might still trigger ReferenceErrors if browser globals are missing.
3530
// We use a try-catch to handle both cases and just verify the attempt was made.
3631
try {
37-
const module = await DependencyLoader.loadLwcDevServer('latest');
32+
const module = await loadLwcDevServer('latest');
3833
expect(module).to.exist;
3934
} catch (error) {
4035
// If it fails with a ReferenceError or similar, it's still "working" in terms of

0 commit comments

Comments
 (0)