UNPKG

@omnia/tooling-vue

Version:

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

70 lines (69 loc) 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.omniaCurrentManifestRuntime = void 0; const base_1 = require("../base"); const fx_models_1 = require("@omnia/fx-models"); const utils_1 = require("../../utils"); /** Inject omnia current manifest runtime */ exports.omniaCurrentManifestRuntime = (0, base_1.defineTransformer)("omnia-current-manifest-runtime", () => { return { test(code, id) { return utils_1.pathUtils.isJavaScriptSuperset(id); }, transform(code, id, fileGraph, ctx, fromWorker) { const isOmniaEnv = ctx.serviceId == 'bb000000-0000-bbbb-0000-0000000000bb'; const fxIndexPaths = isOmniaEnv ? ['./fx/index.ts', '@omnia/fx'] : ['@omnia/fx']; const uxIndexPaths = isOmniaEnv ? ['./fx/ux/index.ts', '@omnia/fx/ux'] : ['@omnia/fx/ux']; const dependencies = [ ...fileGraph.js.metadata.importPaths, ...fileGraph.js.metadata.exportPaths ]; const hasFxIndexPath = dependencies.some(filePath => fxIndexPaths.includes(filePath)); const hasUxIndexPath = dependencies.some(filePath => uxIndexPaths.includes(filePath)); if (hasFxIndexPath) { const declaredVariable = 'setCurrentManifestFx'; code = injectCurrentManifestSetter(code, ctx.serviceId, fileGraph, fxIndexPaths, declaredVariable, () => { return isOmniaEnv ? `import { setCurrentManifest as ${declaredVariable} } from '${ctx.toModuleId(fxIndexPaths[0])}';` : `const { setCurrentManifest: ${declaredVariable} } = omniaWebpackJsonp['bb000000-0000-bbbb-0000-0000000000bb']['${fx_models_1.OmniaResourceManifests.Fx}']('14a8d443-2e58-450b-a462-258bdaf69b49');`; }); } if (hasUxIndexPath) { const declaredVariable = 'setCurrentManifestFxUx'; code = injectCurrentManifestSetter(code, ctx.serviceId, fileGraph, uxIndexPaths, declaredVariable, () => { return isOmniaEnv ? `import { setCurrentManifest as ${declaredVariable} } from '${ctx.toModuleId(uxIndexPaths[0])}';` : `const { setCurrentManifest: ${declaredVariable} } = omniaWebpackJsonp['bb000000-0000-bbbb-0000-0000000000bb']['${fx_models_1.OmniaResourceManifests.FxUx}']('d7327742-5647-4075-b7ab-4f9ca852addb');`; }); } return code; } }; }); function injectCurrentManifestSetter(code, serviceId, fileGraph, indexPaths, declaredVariable, currentManifestDeclarationScript) { const currentManifestDeclaration = currentManifestDeclarationScript(); code = currentManifestDeclaration + '\n' + code; fileGraph.js.metadata.imports.defaultVariables.filter(item => indexPaths.includes(item.path)).forEach(item => { const script = `const ${item.default} = ${declaredVariable}('${serviceId}', '${fileGraph.resourceId}').default`; code = code.replace(item.statement, script); }); fileGraph.js.metadata.imports.namedVariables.filter(item => indexPaths.includes(item.path)).forEach(item => { let variables = ''; if (item.values.length > 0) { variables = item.values.join(', '); } if (Object.keys(item.asValues).length > 0) { if (variables.length > 0) { variables += ', '; } variables += Object.keys(item.asValues).map(name => name + ': ' + item.asValues[name]).join(', '); } const script = `const { ${variables} } = ${declaredVariable}('${serviceId}', '${fileGraph.resourceId}')`; code = code.replace(item.statement, script); }); fileGraph.js.metadata.imports.wildcardVariables.filter(item => indexPaths.includes(item.path)).forEach(item => { const script = `const ${item.value} = ${declaredVariable}('${serviceId}', '${fileGraph.resourceId}')`; code = code.replace(item.statement, script); }); return code; }