zephyr-xpack-internal
Version:
Xpack internals for Zephyr
87 lines (86 loc) • 4.07 kB
JavaScript
import "node:module";
import { ZeErrors, ZephyrError, handleGlobalError, zeBuildAssets, ze_log } from "zephyr-agent";
import { getBuildStats, getModuleFederationConfigs } from "../federation-dashboard-legacy/get-build-stats.mjs";
import { getLegacyModuleFederationConfig } from "./federation-config-metadata.mjs";
import { emitDeploymentDone } from "../lifecycle-events/index.mjs";
import { buildWebpackAssetMap } from "./build-webpack-assets-map.mjs";
async function xpack_zephyr_agent({ stats, stats_json, assets, pluginOptions }) {
ze_log.init('Initiating: Zephyr Webpack Upload Agent');
const zeStart = Date.now();
const { wait_for_index_html, zephyr_engine } = pluginOptions;
const coordinator = pluginOptions.coordinator;
let coordinatedParticipant;
const preserveTapArtifactPaths = zephyr_engine.env?.target === 'tap-app';
let logicalDeploymentCompleted = !coordinator;
try {
if (preserveTapArtifactPaths) zephyr_engine.buildProperties.baseHref = '';
let assetsMap = await buildWebpackAssetMap(assets, {
wait_for_index_html: preserveTapArtifactPaths ? false : wait_for_index_html,
failOnUnsupportedSource: preserveTapArtifactPaths
});
if (pluginOptions.assetPrefix && !preserveTapArtifactPaths) assetsMap = prefixAssetPaths(assetsMap, pluginOptions.assetPrefix);
if (coordinator) {
coordinatedParticipant = pluginOptions.participant;
if (!coordinatedParticipant) throw new ZephyrError(ZeErrors.ERR_DEPLOY_LOCAL_BUILD, {
message: 'A coordinated xpack build requires a participant name'
});
await coordinator.prepareParticipant(coordinatedParticipant, pluginOptions.generation);
}
const { EDGE_URL, PLATFORM, DELIMITER } = await zephyr_engine.application_configuration;
const dashData = await getBuildStats({
stats,
stats_json,
assets,
pluginOptions,
EDGE_URL,
PLATFORM,
DELIMITER
});
const mfConfigs = getModuleFederationConfigs(pluginOptions.mfConfig);
const mfConfig = getLegacyModuleFederationConfig(mfConfigs);
if (coordinator && coordinatedParticipant) {
logicalDeploymentCompleted = await coordinator.contribute({
participant: coordinatedParticipant,
generation: pluginOptions.generation,
assetsMap,
mfConfigs: mfConfigs.length > 0 ? mfConfigs : void 0,
buildStats: dashData,
hooks: pluginOptions.hooks,
dependencyPaths: pluginOptions.dependencyPaths
});
return;
}
await zephyr_engine.upload_assets({
assetsMap,
mfConfigs: mfConfigs.length > 0 ? mfConfigs : void 0,
mfConfig,
buildStats: dashData,
hooks: pluginOptions.hooks
});
await zephyr_engine.build_finished();
} catch (err) {
if (coordinator) throw err;
if (false !== zephyr_engine.hasActiveBuild) zephyr_engine.build_failed();
handleGlobalError(err);
} finally{
if (logicalDeploymentCompleted) emitDeploymentDone();
ze_log.upload('Zephyr Webpack Upload Agent: Done in', Date.now() - zeStart, 'ms');
}
}
function prefixAssetPaths(assetsMap, assetPrefix) {
const normalizedPrefix = assetPrefix.replace(/\\/g, '/').replace(/^\/+|\/+$/g, '');
if (!normalizedPrefix) return assetsMap;
return Object.fromEntries(Object.values(assetsMap).map((asset)=>{
const prefixedPath = asset.path.startsWith(`${normalizedPrefix}/`) ? asset.path : `${normalizedPrefix}/${asset.path.replace(/^\/+/, '')}`;
const prefixedAsset = zeBuildAssets({
filepath: prefixedPath,
content: asset.buffer
});
return [
prefixedAsset.hash,
prefixedAsset
];
}));
}
export { xpack_zephyr_agent };
//# sourceMappingURL=ze-xpack-upload-agent.mjs.map