UNPKG

zephyr-xpack-internal

Version:

Xpack internals for Zephyr

54 lines (53 loc) • 2.25 kB
import "node:module"; import { ZeErrors, ZephyrError, buildAssetsMap, onIndexHtmlResolved, zeBuildAssets, ze_log } from "zephyr-agent"; function getAssetType(asset) { return asset?.constructor?.name ?? typeof asset; } function extractBuffer(asset) { try { if ('function' == typeof asset?.buffer) { const content = asset.buffer(); if (Buffer.isBuffer(content) || 'string' == typeof content) return content; } } catch {} try { if ('function' == typeof asset?.source) { const content = asset.source(); if (Buffer.isBuffer(content) || 'string' == typeof content) return content; } } catch {} } function cacheExtractedBuffers() { const contents = new Map(); return (source)=>{ if (contents.has(source)) return contents.get(source); const content = extractBuffer(source); contents.set(source, content); return content; }; } function assertReadableTapSources(assets, getBuffer) { for (const [path, source] of Object.entries(assets))if (void 0 === getBuffer(source)) throw new ZephyrError(ZeErrors.ERR_DEPLOY_LOCAL_BUILD, { message: `Cannot publish TAP artifact ${JSON.stringify(path)} because its ${getAssetType(source)} source does not expose readable bytes.` }); } async function buildWebpackAssetMap(assets, props) { const { wait_for_index_html, failOnUnsupportedSource = false } = props; ze_log.upload('Building assets map from webpack assets.'); const getBuffer = cacheExtractedBuffers(); if (failOnUnsupportedSource) assertReadableTapSources(assets, getBuffer); const assetsMap = buildAssetsMap(assets, getBuffer, getAssetType); if (wait_for_index_html) { ze_log.upload('Assets map built. Checking for index.html waiter.'); const index_html_content = await onIndexHtmlResolved(); const index_html_asset = zeBuildAssets({ filepath: 'index.html', content: index_html_content }); assetsMap[index_html_asset.hash] = index_html_asset; ze_log.misc('Index.html added to assets map.'); } return assetsMap; } export { buildWebpackAssetMap }; //# sourceMappingURL=build-webpack-assets-map.mjs.map