UNPKG

atlassian-webresource-webpack-plugin

Version:

Auto-generates web-resource definitions from your webpacked code, for usage in an Atlassian product or plugin.

105 lines 5.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderWebResource = void 0; const path_1 = __importDefault(require("path")); const renderCondition_1 = __importDefault(require("./renderCondition")); const renderTransformations_1 = __importDefault(require("./renderTransformations")); const web_resource_parser_1 = require("./web-resource-parser"); const xml_1 = require("./xml"); /** * Renders list of data providers {@see DataProvider} as <data key="provider-key" class="data.provider.Class" /> elements */ const renderDataProviders = (dataProviders) => { if (!Array.isArray(dataProviders) || dataProviders.length === 0) { return []; } return dataProviders.map((dataProvider) => (0, xml_1.renderElement)('data', { key: dataProvider.key, class: dataProvider.class, })); }; function renderContexts(contexts) { return contexts ? contexts.map((context) => `<context>${context}</context>`) : []; } function renderDependencies(dependencies) { return dependencies ? dependencies.map((dependency) => `<dependency>${dependency}</dependency>`) : []; } function renderDeprecationInfo(deprecationInfo, pluginKey, webResourceDescriptors) { if (deprecationInfo) { if (deprecationInfo === true) { return ['<deprecated />']; } const attributes = {}; if (deprecationInfo.sinceVersion) { attributes.since = deprecationInfo.sinceVersion; } if (deprecationInfo.removeInVersion) { attributes.remove = deprecationInfo.removeInVersion; } if (deprecationInfo.alternative) { attributes.alternative = calculateAlternativeResourceKey(deprecationInfo.alternative, pluginKey, webResourceDescriptors); } return [(0, xml_1.renderElement)('deprecated', attributes, deprecationInfo.extraInfo)]; } return []; } /** * Expand the alternative attribute value to a full resource key if it is a reference to another entrypoint. * @param alternativeAttributeValue the value of the "alternative" attribute in the deprecatedEntrypoints map. Can be a simple string * referencing another entrypoint or a full resource key. * @param pluginKey the plugin key * @param webResourceDescriptors list all entry-points specified in the current build */ function calculateAlternativeResourceKey(alternativeAttributeValue, pluginKey, webResourceDescriptors) { // If a colon is present, the users have specified a full resource key, and we don't need to do any mapping. if (alternativeAttributeValue.includes(':')) { return alternativeAttributeValue; } // We need to check if the alternative entry-point's web resource key has been mapped to something else. const entryPointDescriptor = webResourceDescriptors.find((descriptor) => descriptor.attributes.moduleId === alternativeAttributeValue); if (entryPointDescriptor !== undefined) { return `${pluginKey}:${entryPointDescriptor.attributes.key}`; } // If it's not an entry point just assume it's some other web resource in the same plugin defined outside of webpack. return `${pluginKey}:${alternativeAttributeValue}`; } const generateResourceElement = (resource, parameterMap) => { const { name, location } = resource; const assetContentType = path_1.default.extname(location).substr(1); const parameters = parameterMap.get(assetContentType) || []; const children = []; const renderParameters = (attributes) => children.push((0, xml_1.renderElement)('param', attributes)); parameters.forEach(renderParameters); return (0, xml_1.renderElement)('resource', { type: 'download', name, location, }, children); }; const renderResources = (parameterMap, resources) => { return resources ? resources .filter(Boolean) // ignore all `.map` files, since the WRM finds them of its own accord. .filter((resource) => !resource.location.endsWith('.map')) .map((resource) => generateResourceElement(resource, parameterMap)) : []; }; const renderWebResource = (webresource, descriptors, options) => { const { resources = [], externalResources = [], contexts, dependencies, deprecationInfo, conditions, dataProviders, } = webresource; const attributes = (0, web_resource_parser_1.parseWebResourceAttributes)(webresource.attributes); const allResources = []; const children = []; const prependPathPrefix = (location) => options.locationPrefix + location; // add resources for direct dependencies (e.g., JS and CSS files) allResources.push(...resources.map((res) => ({ name: res, location: prependPathPrefix(res) }))); // add resources for indirect dependencies (e.g., images extracted from CSS) allResources.push(...externalResources.map((wr) => ({ name: wr.name, location: prependPathPrefix(wr.location) }))); children.push(...(0, renderTransformations_1.default)(options.transformationMap, allResources), ...renderContexts(contexts), ...renderDependencies(dependencies), ...renderDeprecationInfo(deprecationInfo, options.pluginKey, descriptors), ...renderResources(options.resourceParamMap, allResources), ...renderDataProviders(dataProviders), (0, renderCondition_1.default)(conditions)); return (0, xml_1.renderElement)('web-resource', attributes, children); }; exports.renderWebResource = renderWebResource; //# sourceMappingURL=web-resource-generator.js.map