UNPKG

sonda

Version:

Universal bundle analyzer and visualizer that works with most popular bundlers and frameworks.

52 lines (50 loc) 2.26 kB
import { basename, relative, resolve } from "path"; import { readFileSync, readdirSync } from "fs"; import { Config, processEsbuildMetafile } from "sonda"; //#region src/entrypoints/angular.ts async function SondaAngular({ config = "angular.json", projects = [],...userOptions }) { const options = new Config(userOptions, { integration: "angular", filename: "sonda_[env]_[index]" }); const angularConfig = loadJson(config); const projectsToGenerate = projects.length ? projects : Object.keys(angularConfig.projects); for (const project of projectsToGenerate) { const { outputPath } = angularConfig.projects[project].architect.build.options; const paths = typeof outputPath === "object" ? outputPath : { base: outputPath }; paths.base = resolve(process.cwd(), paths.base); paths.browser = resolve(paths.base, paths.browser || "browser"); paths.server = resolve(paths.base, paths.server || "server"); const metafile = updateMetafile(loadJson(resolve(paths.base, "stats.json")), paths.base); const sondaOptions = options.clone(); sondaOptions.filename = sondaOptions.filename.replace("[env]", project); sondaOptions.sourcesPathNormalizer = (path) => resolve(process.cwd(), path); await processEsbuildMetafile(metafile, sondaOptions); } } function loadJson(path) { return JSON.parse(readFileSync(resolve(process.cwd(), path), "utf8")); } /** * Output paths in metafile only include file name, without the relative path from the current * working directory. For example, in the metafile the output path is "main-xxx.js", but in the * file system it's "dist/project/browser/en/main-xxx.js". This function updates the output paths * to include the relative path from the current working directory. */ function updateMetafile(metafile, basePath) { const cwd = process.cwd(); const outputs = Object.assign({}, metafile.outputs); metafile.outputs = {}; for (const path of readdirSync(basePath, { encoding: "utf8", recursive: true })) { const absolutePath = resolve(basePath, path); const filename = basename(absolutePath); const originalOutput = outputs[filename]; if (originalOutput) metafile.outputs[relative(cwd, absolutePath)] = originalOutput; } return metafile; } //#endregion export { SondaAngular as default };