@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).
86 lines • 3.67 kB
JavaScript
/**
* 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 LwcModuleProvider from '@lwrjs/lwc-module-provider';
import { hashContent } from '@lwrjs/shared-utils';
import { MetadataService } from '../metadata/metadata-service.js';
import { generateAttributesJS, javascript } from '../views/view-template-generator.js';
import { component } from '../template-generators/template-utils.js';
const { getComponent } = MetadataService;
/**
* Addressable service responsible for generating view modules
* from the Communities template metadata
*/
export default class DesignComponentProvider extends LwcModuleProvider {
constructor() {
super(...arguments);
this.name = 'communities-design-component-provider';
this.namespace = '@design/';
this.version = '1';
}
async getModuleEntry({ specifier }) {
// Get filename without any query param
const parsedSpecifier = specifier?.split('?')[0];
// Modules handled by this provider have specifiers in this form: "@view/"
if (parsedSpecifier.startsWith(this.namespace)) {
return {
id: `${parsedSpecifier}|${this.version}`,
entry: `<virtual>/${parsedSpecifier}${path.extname(parsedSpecifier) ? '' : '.js'}`,
specifier: parsedSpecifier,
version: this.version,
};
}
}
// Return a ModuleSource object, which includes the generated code as `originalSource`
async getModuleSource({ name, namespace, specifier }, moduleEntry) {
// remove namespace and # @design#<uuid>.html
const [specifierParsed] = specifier.split('?'); // remove queryParam
const [uuid] = specifierParsed.replace(this.namespace, '').split('#');
// Generate code for the requested design module
const cmp = getComponent(uuid);
if (!cmp) {
throw new Error(`Cannot find component '${this.namespace + uuid}'`);
}
// generate a template HTML based on the component metadata
const moduleName = uuid;
const originalSource = generateModule(specifier, moduleName, cmp);
// Create an return a ModuleSource object
const { version, id } = moduleEntry;
return {
id,
specifier,
namespace,
name: name || specifier,
version,
moduleEntry,
ownHash: hashContent(originalSource),
originalSource,
};
}
}
function generateModule(name, moduleName, cmp) {
const fileType = (path.extname(name) || '.js').substr(1);
if (fileType === 'css') {
// TODO:lwr https://github.com/salesforce/lwr/issues/201
return ``;
}
// Using "isTopLevel=true" because it is the top level of this component tree
// Otherwise, a second component wrapper would be returned for this component.
// isLastComponent true/false has no effect here since isTopLevel=true, which means
// the component wrapper will not be generated
const { html, attributes } = component({
cmp,
isThemeLayout: false,
regionName: cmp.region,
isTopLevel: true,
attributeSet: {},
isRenderDesignMode: true,
isLastComponent: false,
});
return fileType === 'html' ? `<template lwc:render-mode="light">${html}</template>` : javascript(moduleName, generateAttributesJS(attributes.attributes));
}
//# sourceMappingURL=design-component-provider.js.map