diff --git a/src/instrumentation/machineId.ts b/src/instrumentation/machineId.ts index 233d4d10..03dcc479 100644 --- a/src/instrumentation/machineId.ts +++ b/src/instrumentation/machineId.ts @@ -21,7 +21,7 @@ export function getMacAddress() { const macAddresses = Object.keys(networkInterfaces) .reduce((macAddresses: string[], networkInterfaceName) => { const networkInterfaceInfo = - networkInterfaces[networkInterfaceName]; + networkInterfaces[networkInterfaceName] || []; macAddresses = macAddresses.concat( networkInterfaceInfo.map(info => { return info.mac; diff --git a/src/server/LocalDevServer.ts b/src/server/LocalDevServer.ts index 729c4de0..24b459ea 100644 --- a/src/server/LocalDevServer.ts +++ b/src/server/LocalDevServer.ts @@ -78,7 +78,6 @@ export default class LocalDevServer { } config.addMiddleware(middleware); - const routes: ContainerAppExtension[] = [ projectMetadata(this.sessionNonce, this.project) ]; diff --git a/src/server/__tests__/LocalDevServer.test.ts b/src/server/__tests__/LocalDevServer.test.ts index a01e9ffc..55aa1818 100644 --- a/src/server/__tests__/LocalDevServer.test.ts +++ b/src/server/__tests__/LocalDevServer.test.ts @@ -23,8 +23,8 @@ jest.mock('@webruntime/server', () => { super(...args); mockServerConstructor(...args); } - initialize() { } - start() { } + initialize() {} + start() {} shutdown() { this.emit('shutdown'); } diff --git a/src/server/config/WebruntimeConfig.ts b/src/server/config/WebruntimeConfig.ts index 46b9e62d..78ec21bc 100644 --- a/src/server/config/WebruntimeConfig.ts +++ b/src/server/config/WebruntimeConfig.ts @@ -15,6 +15,8 @@ import { Plugin } from 'rollup'; import { ApexService, SchemaService } from '@communities-webruntime/services'; import { ResourceUrlService } from '../services/ResourceUrlService'; import { ApexContinuationService } from '../services/ApexContinuationService'; +import { LWCMessageService } from '../services/LWCMessageService'; + import alias from '@rollup/plugin-alias'; export default class WebruntimeConfig implements Config { @@ -76,7 +78,8 @@ export default class WebruntimeConfig implements Config { AppBootstrapService, ImportMapService, ResourceUrlService, - SchemaService + SchemaService, + LWCMessageService ]; this.bundle = [ diff --git a/src/server/services/LWCMessageService.ts b/src/server/services/LWCMessageService.ts new file mode 100644 index 00000000..cb7ebd4b --- /dev/null +++ b/src/server/services/LWCMessageService.ts @@ -0,0 +1,41 @@ +import { CompileService } from '@webruntime/api'; +import path from 'path'; + +const APEX_LWC_MESSAGE_SERVICE_REGEX = /^(@salesforce\/messageChannel\/.*)/; + +function matchesLWCMessageServiceScopedModule(id: string) { + return id && id.match(APEX_LWC_MESSAGE_SERVICE_REGEX); +} + +/** + * Rollup plugin to resolve @salesforce/messageChannel + * + */ +function plugin() { + return { + name: 'rollup-plugin-lwc-message-service', + + resolveId(id: string) { + return matchesLWCMessageServiceScopedModule(id) ? id : null; + }, + load(id: any) { + const idParts = matchesLWCMessageServiceScopedModule(id); + if (idParts) { + throw new Error( + "You can't preview a component using Lightning Message Service. Test the component directly in the org" + ); + } + return null; + } + }; +} + +export class LWCMessageService implements CompileService { + getPlugin() { + return plugin(); + } + + async initialize() {} + + shutdown() {} +} diff --git a/src/server/services/__tests__/LWCMessageService.test.ts b/src/server/services/__tests__/LWCMessageService.test.ts new file mode 100644 index 00000000..75d4bc5d --- /dev/null +++ b/src/server/services/__tests__/LWCMessageService.test.ts @@ -0,0 +1,61 @@ +import { LWCMessageService } from '../LWCMessageService'; +import { getLatestVersion } from '@webruntime/server/dist/commonjs/utils/utils'; +import { PublicConfig } from '@webruntime/api'; + +jest.mock('@webruntime/server/dist/commonjs/utils/utils'); + +describe('LWCMessageService', () => { + let config: PublicConfig; + + beforeEach(() => { + (getLatestVersion as jest.Mock).mockReturnValue('158104e2eb'); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it('returns a rollup plugin', () => { + const service = new LWCMessageService(); + expect(service.getPlugin()).toBeTruthy(); + }); + + describe('rollup plugin', () => { + describe('resolveId', () => { + it('should resolve paths with @salesforce/messageChannel', () => { + const service = new LWCMessageService(); + const plugin = service.getPlugin(); + + const resolved = plugin.resolveId( + '@salesforce/messageChannel/SampleMessageChannel__c' + ); + expect(resolved).toBe( + '@salesforce/messageChannel/SampleMessageChannel__c' + ); + }); + }); + + describe('load', () => { + it('should throw error for @salesforce/messageChannel imports', () => { + (getLatestVersion as jest.Mock).mockReturnValue('158104e2eb'); + + const service = new LWCMessageService(); + const plugin = service.getPlugin(); + function loadApexContinuation() { + plugin.load( + '@salesforce/messageChannel/SampleMessageChannel__c' + ); + } + expect(loadApexContinuation).toThrowErrorMatchingSnapshot(); + }); + + it('should return null instead of throwing error for other than @salesforce/messageChannel imports', () => { + const service = new LWCMessageService(); + const plugin = service.getPlugin(); + const resolved = plugin.load('./lightningMessageService.html'); + + expect(resolved).toBeNull(); + }); + }); + }); +}); diff --git a/src/server/services/__tests__/__snapshots__/LWCMessageService.test.ts.snap b/src/server/services/__tests__/__snapshots__/LWCMessageService.test.ts.snap new file mode 100644 index 00000000..d776738f --- /dev/null +++ b/src/server/services/__tests__/__snapshots__/LWCMessageService.test.ts.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`LWCMessageService rollup plugin load should throw error for @salesforce/messageChannel imports 1`] = `"You can't preview a component using Lightning Message Service. Test the component directly in the org"`;