All files / src manifest-json.ts

8.69% Statements 2/23
100% Branches 0/0
0% Functions 0/1
8.69% Lines 2/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231x 1x                                                                                                                                                                                                                                                  
import fsp from 'node:fs/promises';
import path from 'node:path';
 
import type { BuildSsrTarget } from './core';
import type { ParsedModuleConfig } from './module-config';
 
export interface ManifestJson {
    /**
     * 模块名称
     */
    name: string;
    /**
     * 导入映射
     */
    imports: Record<string, string>;
    /**
     * 导出项配置
     * 类型:Record<导出路径, 导出项信息>
     */
    exports: ManifestJsonExports;
    /**
     * 构建产物文件列表
     */
    buildFiles: string[];
    /**
     * 编译的文件信息
     * 类型:Record<源文件, 编译信息>
     */
    chunks: ManifestJsonChunks;
}
 
/**
 * 导出项配置映射
 * 类型:Record<导出路径, 导出项信息>
 */
export type ManifestJsonExports = Record<string, ManifestJsonExport>;
 
/**
 * 导出项信息
 */
export interface ManifestJsonExport {
    /**
     * 导出项名称
     */
    name: string;
    /**
     * 是否重写模块内的导入路径
     * - true: 重写为 '{服务名}/{导出名}' 格式
     * - false: 保持原始导入路径
     */
    rewrite: boolean;
    /**
     * 导出项对应的文件路径
     */
    file: string;
    /**
     * 导出项的标识符
     */
    identifier: string;
}
 
export type ManifestJsonChunks = Record<string, ManifestJsonChunk>;
 
export interface ManifestJsonChunk {
    name: string;
    /**
     * 当前编译的 JS 文件。
     */
    js: string;
    /**
     * 当前编译的 CSS 文件。
     */
    css: string[];
    /**
     * 其它的资源文件。
     */
    resources: string[];
    /**
     * 构建产物的大小。
     */
    sizes: ManifestJsonChunkSizes;
}
 
export interface ManifestJsonChunkSizes {
    /**
     * JavaScript 文件的大小,单位:字节
     */
    js: number;
    /**
     * CSS 文件的大小,单位:字节
     */
    css: number;
    /**
     * 资源文件的大小,单位:字节
     */
    resource: number;
}
 
/**
 * 获取服务清单文件
 */
export async function getManifestList(
    target: BuildSsrTarget,
    moduleConfig: ParsedModuleConfig
): Promise<ManifestJson[]> {
    return Promise.all(
        Object.values(moduleConfig.links).map(async (item) => {
            const filename = path.resolve(item[target], 'manifest.json');
            try {
                const data: ManifestJson = await JSON.parse(
                    await fsp.readFile(filename, 'utf-8')
                );
                data.name = item.name;
                return data;
            } catch (e) {
                throw new Error(
                    `'${item.name}' service '${target}/manifest.json' file read error`
                );
            }
        })
    );
}