@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).
62 lines • 2.17 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 RESOURCE_URL_PREFIX = '@salesforce/resourceUrl/';
const CONTENT_ASSET_URL_PREFIX = '@salesforce/contentAssetUrl/';
/**
* Check whether the given id is a @salesforce/resourceUrl scoped module id
*/
function isResourceUrlScopedModule(id) {
return id && (id.startsWith(RESOURCE_URL_PREFIX) || id.startsWith(CONTENT_ASSET_URL_PREFIX));
}
/**
* Provider that resolves @salesforce/resourceUrl/xxx imports to the actual resource URL.
*
* The returned URL format is `${basePath}/assets/${resourceName}`.
*/
export default class SalesforceResourceUrlProvider {
constructor() {
this.name = 'salesforce-resource-url-provider';
this.version = '1';
}
async getModuleEntry({ specifier }) {
if (isResourceUrlScopedModule(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 [resourceName] = specifier.split('/')[2].split('.');
const originalSource = `
import basePath from '@app/basePath';
export default \`\${basePath}/assets/${resourceName}\`;`;
return {
id: moduleEntry.id,
specifier,
namespace,
name,
version: this.version,
originalSource,
moduleEntry,
ownHash: hashContent(originalSource),
compiledSource: originalSource,
};
}
}
//# sourceMappingURL=salesforce-resource-url-provider.js.map