UNPKG

zephyr-astro-integration

Version:
198 lines (197 loc) • 8.14 kB
"use strict"; var __webpack_require__ = {}; (()=>{ __webpack_require__.d = (exports1, getters, values)=>{ var define = (defs, kind)=>{ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { enumerable: true, [kind]: defs[key] }); }; define(getters, "get"); define(values, "value"); }; })(); (()=>{ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop); })(); (()=>{ __webpack_require__.r = (exports1)=>{ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { value: 'Module' }); Object.defineProperty(exports1, '__esModule', { value: true }); }; })(); var __webpack_exports__ = {}; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { extractAstroAssetsFromBuildHook: ()=>extractAstroAssetsFromBuildHook, extractAstroAssetsMap: ()=>extractAstroAssetsMap }); const promises_namespaceObject = require("node:fs/promises"); const external_node_path_namespaceObject = require("node:path"); const external_node_url_namespaceObject = require("node:url"); const external_zephyr_agent_namespaceObject = require("zephyr-agent"); function extractBuffer(asset) { return asset.content; } function getAssetType(asset) { return asset.type; } function normalizePath(filePath) { return filePath.split(external_node_path_namespaceObject.sep).join('/'); } function looksLikeRoute(path) { if (!path || !path.startsWith('/')) return false; if (path.includes('[') || path.includes(']')) return true; const segments = path.split('/').filter(Boolean); if (segments.length <= 2) return true; return false; } async function extractAstroAssetsFromBuildHook(assets, outputPath, target) { if ('tap-app' === target) return extractAstroAssetsMap(outputPath, target); const astroAssets = {}; try { if (!assets) return await extractAstroAssetsMap(outputPath, target); const assetEntries = extractAssetEntries(assets); for (const [filePath, assetInfo] of assetEntries)try { let fullPath = null; if (assetInfo && 'object' == typeof assetInfo && 'href' in assetInfo) fullPath = (0, external_node_url_namespaceObject.fileURLToPath)(assetInfo); else if ('string' == typeof assetInfo && assetInfo) { if (looksLikeRoute(assetInfo)) continue; fullPath = (0, external_node_path_namespaceObject.isAbsolute)(assetInfo) && !looksLikeRoute(assetInfo) ? assetInfo : (0, external_node_path_namespaceObject.join)(outputPath, assetInfo); } else if ('string' == typeof filePath && filePath) { if (looksLikeRoute(filePath)) continue; fullPath = (0, external_node_path_namespaceObject.isAbsolute)(filePath) && !looksLikeRoute(filePath) ? filePath : (0, external_node_path_namespaceObject.join)(outputPath, filePath); } if (!fullPath) continue; const relativePath = normalizePath((0, external_node_path_namespaceObject.relative)(outputPath, fullPath)); if (shouldSkipFile(relativePath, target)) continue; const content = await (0, promises_namespaceObject.readFile)(fullPath); const fileType = getFileType(relativePath); astroAssets[relativePath] = { content, type: fileType }; } catch (readError) { (0, external_zephyr_agent_namespaceObject.logFn)('warn', `Failed to read asset file ${filePath}: ${readError}`); continue; } if (0 === Object.keys(astroAssets).length) return await extractAstroAssetsMap(outputPath, target); return (0, external_zephyr_agent_namespaceObject.buildAssetsMap)(astroAssets, extractBuffer, getAssetType); } catch (error) { (0, external_zephyr_agent_namespaceObject.logFn)('warn', 'Error processing assets from Astro build hook:' + JSON.stringify(error, null, 2)); return await extractAstroAssetsMap(outputPath, target); } } function extractAssetEntries(assets) { const entries = []; if (Array.isArray(assets)) assets.forEach((asset)=>{ if ('string' == typeof asset) entries.push([ asset, asset ]); else if (asset && 'object' == typeof asset) { const assetObj = asset; const path = assetObj['path'] || assetObj['url'] || assetObj['href'] || assetObj['pathname']; if (path && 'string' == typeof path) entries.push([ path, asset ]); } }); else if (assets instanceof Map) for (const [key, value] of assets.entries())entries.push([ key, value ]); else if (assets && 'object' == typeof assets) for (const [key, value] of Object.entries(assets))if (Array.isArray(value)) value.forEach((item)=>{ entries.push([ key, item ]); }); else entries.push([ key, value ]); return entries; } async function extractAstroAssetsMap(buildDir, target) { const assets = {}; if ('tap-app' === target) { const files = await (0, external_zephyr_agent_namespaceObject.readDirRecursiveWithContents)(buildDir, { includeIgnoredPaths: true, failOnError: true }); for (const file of files){ const relativePath = normalizePath(file.relativePath); assets[relativePath] = { content: file.content, type: getFileType(relativePath) }; } return (0, external_zephyr_agent_namespaceObject.buildAssetsMap)(assets, extractBuffer, getAssetType); } try { const files = await (0, external_zephyr_agent_namespaceObject.readDirRecursiveWithContents)(buildDir); for (const file of files){ const relativePath = normalizePath(file.relativePath); if (shouldSkipFile(relativePath, target)) continue; const fileType = getFileType(relativePath); assets[relativePath] = { content: file.content, type: fileType }; } } catch (error) { (0, external_zephyr_agent_namespaceObject.logFn)('warn', `Failed to read build directory ${buildDir}: ${error}`); } return (0, external_zephyr_agent_namespaceObject.buildAssetsMap)(assets, extractBuffer, getAssetType); } function shouldSkipFile(filePath, target) { if ('tap-app' === target) return false; const skipPatterns = [ /\.map$/, /node_modules/, /\.git/, /\.DS_Store$/, /thumbs\.db$/i ]; return skipPatterns.some((pattern)=>pattern.test(filePath)); } function getFileType(filePath) { const extension = filePath.split('.').pop()?.toLowerCase() || ''; const typeMap = { html: 'text/html', css: 'text/css', js: "application/javascript", mjs: "application/javascript", json: 'application/json', png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', svg: 'image/svg+xml', ico: 'image/x-icon', woff: 'font/woff', woff2: 'font/woff2', ttf: 'font/ttf', eot: 'application/vnd.ms-fontobject', xml: 'text/xml', txt: 'text/plain' }; return typeMap[extension] || 'application/octet-stream'; } exports.extractAstroAssetsFromBuildHook = __webpack_exports__.extractAstroAssetsFromBuildHook; exports.extractAstroAssetsMap = __webpack_exports__.extractAstroAssetsMap; for(var __rspack_i in __webpack_exports__)if (-1 === [ "extractAstroAssetsFromBuildHook", "extractAstroAssetsMap" ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i]; Object.defineProperty(exports, '__esModule', { value: true }); //# sourceMappingURL=extract-astro-assets-map.js.map