@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).
77 lines • 3.22 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 { ContextService } from '../context/context-service.js';
import { MetadataService } from '../metadata/metadata-service.js';
import { javascript, template } from '../views/view-template-generator.js';
/**
* Addressable service responsible for generating view modules
* from the Communities template metadata
*/
export default class ViewProvider extends LwcModuleProvider {
constructor() {
super(...arguments);
this.name = 'communities-view-provider';
this.namespace = '@view/';
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 # <viewName>#<viewName>.html
const [viewName] = specifier.replace(this.namespace, '').split('#');
// Generate code for the requested module
const view = MetadataService.getAllViews().filter((v) => v.devName === viewName)[0];
if (!view) {
throw new Error(`Cannot find view '${this.namespace + viewName}'`);
}
const originalSource = generateModule(specifier, viewName, view);
// 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, viewName, { component, themeLayoutType }) {
const { isDesignMode } = ContextService.getContext();
const fileType = (path.extname(name) || '.js').substr(1);
// generate a template HTML based on the view metadata
if (fileType === 'html') {
const { html: generatedTemplate } = template(component, !themeLayoutType, isDesignMode);
return generatedTemplate;
}
else if (fileType === 'css') {
// TODO:lwr https://github.com/salesforce/lwr/issues/201
return ``;
}
const { attributes } = template(component, !themeLayoutType);
const generatedJavascript = javascript(viewName, attributes);
return generatedJavascript;
}
//# sourceMappingURL=view-provider.js.map