UNPKG

@omnia/tooling-vue

Version:

Used to bundle and serve manifests web component that build on Vue framework.

116 lines (115 loc) 5.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPreBundleInfo = exports.validatePreBundles = exports.getManifest = exports.clearManifestCollection = exports.getManifestCollection = exports.entriesResolver = void 0; const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const globby_1 = tslib_1.__importDefault(require("globby")); const $ = tslib_1.__importStar(require("../../variables")); const fx_models_1 = require("@omnia/fx-models"); const utils_1 = require("./utils"); class ManifestBuildEntriesResolver { constructor() { this.eventHandlers = []; this.registerManifestEventHandler = (func) => { this.eventHandlers.push(func); }; this.getEntries = () => { this.ensure(); return this.promise; }; this.getPreBundleEntries = () => { if (!this.preBundle) { this.ensure(); const assetScriptsDir = `./${$.tooling.composer.getAssetsDir()}/scripts`; return this.getEntries().then(entries => { this.preBundle = Object.keys(entries).reduce((preBundleEntries, resourceIdentifier) => { const filePaths = entries[resourceIdentifier]; const resourceId = utils_1.manifestUtils.getResourceIdFromEntryKey(resourceIdentifier); if (resourceId == fx_models_1.OmniaResourceManifests.Vendor.toString() || resourceId == fx_models_1.OmniaResourceManifests.HMR.toString()) { return Object.assign(preBundleEntries, { [resourceIdentifier]: filePaths }); } const paths = filePaths.filter(p => p.startsWith('./node_modules/') || p.startsWith(assetScriptsDir)); if (paths.length > 0) { return Object.assign(preBundleEntries, { [resourceIdentifier]: paths }); } return preBundleEntries; }, {}); return this.preBundle; }); } return Promise.resolve(this.preBundle); }; this.resolveEntries = (value) => { this.promise = null; this.ensure(); this.resolve(value); this.eventHandlers.forEach(func => func()); }; this.ensure = () => { if (!this.promise) { this.promise = new Promise(resolve => { this.resolve = resolve; }); } }; } } exports.entriesResolver = new ManifestBuildEntriesResolver(); let _manifestCollection; function getManifestCollection() { if (!_manifestCollection) { _manifestCollection = $.tooling.composer.getManifestData(); } return _manifestCollection; } exports.getManifestCollection = getManifestCollection; function clearManifestCollection() { _manifestCollection = null; } exports.clearManifestCollection = clearManifestCollection; function getManifest(resourceId) { let manifests = getManifestCollection(); let manifest = manifests.webcomponent?.find(r => r.resourceId == resourceId); if (!manifest) { manifest = manifests.resource?.find(r => r.resourceId == resourceId); } if (!manifest) { manifest = manifests.groupedResouresAndComponents?.find(r => r.resourceId == resourceId); } return manifest; } exports.getManifest = getManifest; async function validatePreBundles(fileNames) { const preBundleDir = utils_1.envUtils.getBundleOutputDirPath(); const filePaths = fileNames.map(fileName => path_1.default.join(preBundleDir, fileName)); return new Promise((resolve, reject) => { (0, globby_1.default)(filePaths).then(result => { resolve(result); }).catch(reject); }); } exports.validatePreBundles = validatePreBundles; function getPreBundleInfo() { return exports.entriesResolver.getPreBundleEntries().then(entries => { const serviceId = $.tooling.composer.getServiceId(); const manifests = $.tooling.composer.getManifestData(); return Object.keys(entries).reduce((array, fileName) => { const resourceId = utils_1.manifestUtils.getResourceIdFromEntryKey(fileName); const filePaths = entries[fileName]; const manifest = manifests.resource.find(i => i.resourceId == resourceId); return array.concat(filePaths.map(filePath => ({ serviceId: serviceId, resourceId: resourceId, resourceName: manifest.resourceName, bundleTargetTypes: manifest.availableBundleTargetTypes, path: filePath }))); }, []); }); } exports.getPreBundleInfo = getPreBundleInfo;