@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).
75 lines • 2.7 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 SCHEMA_PREFIX = '@salesforce/schema';
function isSchemaScopedModule(id) {
return id && id.startsWith(SCHEMA_PREFIX);
}
/**
* Provider that produce exports of the name of the requested
* sobject or field schema for scoped imports expressed in the following way:
* 1. @salesforce/schema/[objectApiName] for SObject schema,
* 2. @salesforce/schema/[objectApiName].[fieldApiName] for field schema or
* 3. @salesforce/schema/[objectApiName].[relationshipApiName+].[fieldApiName] for
* spanning field schema.
*
* See https://salesforce.quip.com/MgkVAKaONfcw for details.
*
* example:
* import MyObject__c from '@salesforce/schema/MyObject__c.myField' gets converted to
* export default {"objectApiName":"MyObject__c","fieldApiName":"myField"}
*/
export default class SalesforceResourceUrlProvider {
constructor() {
this.name = 'salesforce-schema-provider';
this.version = '1';
}
async getModuleEntry({ specifier }) {
if (isSchemaScopedModule(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;
}
const resource = specifier.split('/')[2].split('.js')[0];
const idx = resource.indexOf('.');
let schema;
if (idx === -1) {
schema = { objectApiName: resource };
}
else {
const objectApiName = resource.substring(0, idx);
const fieldApiName = resource.substring(idx + 1);
schema = { fieldApiName, objectApiName };
}
const originalSource = `export default ${JSON.stringify(schema)}`;
return {
id: moduleEntry.id,
specifier,
namespace,
name,
version: this.version,
originalSource,
moduleEntry,
ownHash: hashContent(originalSource),
compiledSource: originalSource,
};
}
}
//# sourceMappingURL=salesforce-schema-provider.js.map