zephyr-agent
Version:
Zephyr plugin agent
176 lines (175 loc) • 8.02 kB
JavaScript
;
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__, {
uploadOutputToZephyr: ()=>uploadOutputToZephyr
});
const external_node_path_namespaceObject = require("node:path");
const external_zephyr_edge_contract_namespaceObject = require("zephyr-edge-contract");
const index_js_namespaceObject = require("../zephyr-engine/index.js");
const external_errors_index_js_namespaceObject = require("./errors/index.js");
const ze_build_assets_map_js_namespaceObject = require("./transformers/ze-build-assets-map.js");
const ze_build_dash_data_js_namespaceObject = require("./transformers/ze-build-dash-data.js");
const read_dir_recursive_js_namespaceObject = require("./utils/read-dir-recursive.js");
const DEFAULT_DEPLOY_TARGET = 'web';
const DEFAULT_DEPLOY_SSR = true;
const SKIP_DEPLOY_PATTERNS = [
/\.map$/i,
/node_modules\//i,
/\.git\//i,
/\.DS_Store$/i,
/^\.zephyr\//i,
/thumbs\.db$/i
];
const DEPLOY_ENTRYPOINT_CANDIDATES = [
'server/index.js',
'server/index.mjs',
'server/server.js',
'server/server.mjs',
'server/_worker.js',
'server/_worker.mjs',
'index.mjs',
'index.js'
];
function normalizePath(filePath) {
return filePath.replace(/\\/g, '/').replace(/^\.?\//, '');
}
function normalizeBaseURL(baseURL) {
let normalized = baseURL.trim().replace(/\\/g, '/');
while(normalized.startsWith('./'))normalized = normalized.slice(2);
while(normalized.startsWith('/'))normalized = normalized.slice(1);
while(normalized.endsWith('/'))normalized = normalized.slice(0, -1);
return normalized;
}
function resolveAssetPath(file, outputDir, publicDir, baseURL = '/') {
const relativePath = normalizePath(file.relativePath);
if (!publicDir) return relativePath;
const fullPath = normalizePath(file.fullPath);
const publicRoot = normalizePath(publicDir).replace(/\/+$/, '');
const outputRoot = normalizePath(outputDir).replace(/\/+$/, '');
const publicRelativeRoot = normalizePath((0, external_node_path_namespaceObject.relative)(outputRoot, publicRoot)).replace(/\/+$/, '');
const isWithinPublicAlias = !!publicRelativeRoot && relativePath.startsWith(`${publicRelativeRoot}/`);
if (fullPath.startsWith(`${publicRoot}/`) || isWithinPublicAlias) {
const staticRelative = fullPath.startsWith(`${publicRoot}/`) ? fullPath.slice(publicRoot.length + 1) : relativePath.slice(publicRelativeRoot.length + 1);
if (!staticRelative) return relativePath;
const basePath = normalizeBaseURL(baseURL);
return basePath ? `client/${basePath}/${staticRelative}` : `client/${staticRelative}`;
}
return relativePath;
}
function shouldSkipDeployAsset(filePath) {
return SKIP_DEPLOY_PATTERNS.some((pattern)=>pattern.test(filePath));
}
function resolveDeployEntrypoint(assets) {
for (const candidate of DEPLOY_ENTRYPOINT_CANDIDATES)if (Object.prototype.hasOwnProperty.call(assets, candidate)) return candidate;
}
function getLegacyModuleFederationConfig(mfConfigs) {
const config = mfConfigs?.length === 1 ? mfConfigs[0] : void 0;
return config && 'string' == typeof config.name && 'string' == typeof config.filename ? config : void 0;
}
async function uploadOutputToZephyr(opts) {
const target = opts.target ?? DEFAULT_DEPLOY_TARGET;
(0, external_zephyr_edge_contract_namespaceObject.assertZephyrBuildTarget)(target, 'uploadOutputToZephyr({ target })');
const outputDir = (0, external_node_path_namespaceObject.resolve)(opts.outputDir);
const publicDir = opts.publicDir ? (0, external_node_path_namespaceObject.resolve)(opts.publicDir) : void 0;
const files = await (0, read_dir_recursive_js_namespaceObject.readDirRecursiveWithContents)(outputDir);
const assets = {};
const assetSources = new Map();
for (const file of files){
const relativePath = resolveAssetPath(file, outputDir, publicDir, opts.baseURL);
if (shouldSkipDeployAsset(relativePath)) continue;
const existingSource = assetSources.get(relativePath);
if (existingSource) throw new external_errors_index_js_namespaceObject.ZephyrError(external_errors_index_js_namespaceObject.ZeErrors.ERR_DEPLOY_LOCAL_BUILD, {
message: `uploadOutputToZephyr found conflicting output files for snapshot path "${relativePath}": "${existingSource}" and "${file.fullPath}".`
});
assets[relativePath] = {
content: file.content
};
assetSources.set(relativePath, file.fullPath);
}
if (0 === Object.keys(assets).length) throw new external_errors_index_js_namespaceObject.ZephyrError(external_errors_index_js_namespaceObject.ZeErrors.ERR_ASSETS_NOT_FOUND);
const ssr = opts.ssr ?? DEFAULT_DEPLOY_SSR;
const entrypoint = resolveDeployEntrypoint(assets);
if (ssr && !entrypoint) throw new external_errors_index_js_namespaceObject.ZephyrError(external_errors_index_js_namespaceObject.ZeErrors.ERR_SSR_ENTRYPOINT_NOT_FOUND, {
outputDir,
candidates: DEPLOY_ENTRYPOINT_CANDIDATES.join(', ')
});
const assetsMap = (0, ze_build_assets_map_js_namespaceObject.buildAssetsMapMock)(assets, (asset)=>asset.content, ()=>'buffer');
const zephyrEngine = await index_js_namespaceObject.ZephyrEngine.create({
builder: opts.builder ?? 'unknown',
context: opts.rootDir,
target
});
let buildInProgress = true;
try {
zephyrEngine.env.target = target;
zephyrEngine.env.ssr = ssr;
const buildStats = await (0, ze_build_dash_data_js_namespaceObject.zeBuildDashData)(zephyrEngine);
const mfConfig = getLegacyModuleFederationConfig(opts.mfConfigs);
const buildStatsWithFederation = opts.federation ? {
...buildStats,
federation: opts.federation
} : buildStats;
let deploymentUrl = null;
await zephyrEngine.upload_assets({
assetsMap,
buildStats: buildStatsWithFederation,
...mfConfig ? {
mfConfig
} : {},
...opts.mfConfigs ? {
mfConfigs: opts.mfConfigs
} : {},
snapshotType: ssr ? 'ssr' : 'csr',
entrypoint: ssr ? entrypoint : void 0,
hooks: {
onDeployComplete: async (deploymentInfo)=>{
deploymentUrl = deploymentInfo.url;
await opts.hooks?.onDeployComplete?.(deploymentInfo);
}
}
});
buildInProgress = false;
await zephyrEngine.build_finished();
return {
deploymentUrl,
entrypoint
};
} finally{
if (buildInProgress && false !== zephyrEngine.hasActiveBuild) zephyrEngine.build_failed();
}
}
exports.uploadOutputToZephyr = __webpack_exports__.uploadOutputToZephyr;
for(var __rspack_i in __webpack_exports__)if (-1 === [
"uploadOutputToZephyr"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
value: true
});
//# sourceMappingURL=upload-output-to-zephyr.js.map