@communities-webruntime/services
Version:
If you would like to run Lightning Web Runtime without the CLI, we expose some of our programmatic APIs available in Node.js. If you're looking for the CLI documentation [you can find that here](https://www.npmjs.com/package/@communities-webruntime/cli).
85 lines (84 loc) • 3.27 kB
JavaScript
/** @hidden */
/**
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import * as path from 'path';
import { hashContent } from '@lwrjs/shared-utils';
const APEX_REGEX = /^(@salesforce\/apex)(?:\/([\w-]+\.[\w-]+(?:\.[\w-]+)?))?$/;
function matchesApexScopedModule(id) {
return id && id.match(APEX_REGEX);
}
/**
* Module provider that produces exports of the name of the requested
* apex method from a standard or custom controller:
* 1. @salesforce/apex for refreshApex and getSObjectValue functions
* 2. @salesforce/apex/[apexClass].[apexMethod] for custom, non-namespaced apex or
* 3. @salesforce/apex/[namespace].[apexClass].[apexMethod] for
* namespaced apex.
*
* examples:
* * import getCommunityName from '@salesforce/apex/applauncher.CommunityLogoController.getCommunityName';
* * import getContactList from '@salesforce/apex/MyContactController.getContactList';
* * import { refreshApex } from '@salesforce/apex';
*/
export default class SMCProvider {
constructor() {
this.name = 'salesforce-apex-provider';
this.version = '1';
}
async getModuleEntry({ specifier }) {
if (matchesApexScopedModule(specifier)) {
return {
id: `${specifier}|${this.version}`,
virtual: true,
entry: `<virtual>/${specifier}${path.extname(specifier) ? '' : '.js'}`,
specifier,
version: this.version,
};
}
}
async getModule({ specifier, namespace, name = specifier }) {
// Retrieve the Module Entry
const moduleEntry = await this.getModuleEntry({ specifier });
if (!moduleEntry) {
return;
}
let originalSource;
const idParts = matchesApexScopedModule(specifier);
// the first element is the whole id
const [scope, apexDefinition] = idParts.slice(1, 3);
if (apexDefinition) {
// handle @salesforce/apex/(ns.)class.method
// reverse because we don't require the namespace, but if
// its included, it will come first
const [method, classname, ns = '""'] = apexDefinition.split('.').reverse().map(JSON.stringify);
originalSource = `
import { getApexInvoker } from 'force/ldsAdaptersApex';
const apexInvoker = getApexInvoker(${ns}, ${classname}, ${method}, false);
export default apexInvoker;
`;
}
else if (scope) {
// otherwise, handle @salesforce/apex
// refreshApex and getSObjectValue special case
originalSource = `
export { refreshApex, getSObjectValue } from 'force/ldsAdaptersApex';
`;
}
return {
id: moduleEntry.id,
specifier,
namespace,
name,
version: this.version,
originalSource,
moduleEntry,
ownHash: hashContent(originalSource),
compiledSource: originalSource,
};
}
}
//# sourceMappingURL=salesforce-apex-provider.js.map