|
| 1 | +import { byteSize, sortBy } from '@utils'; |
| 2 | +import type * as d from '../../declarations'; |
| 3 | +import { isOutputTargetStats } from '../output-targets/output-utils'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Generates the Build Stats from the buildCtx. Writes any files to the file system. |
| 7 | + * @param config the project build configuration |
| 8 | + * @param buildCtx An instance of the build which holds the details about the build |
| 9 | + * @returns CompilerBuildStats or an Object including diagnostics. |
| 10 | + */ |
| 11 | +export function generateBuildStats( |
| 12 | + config: d.Config, |
| 13 | + buildCtx: d.BuildCtx |
| 14 | +): d.CompilerBuildStats | { diagnostics: d.Diagnostic[] } { |
| 15 | + const buildResults = buildCtx.buildResults; |
| 16 | + |
| 17 | + let jsonData: d.CompilerBuildStats | { diagnostics: d.Diagnostic[] }; |
| 18 | + |
| 19 | + try { |
| 20 | + if (buildResults.hasError) { |
| 21 | + jsonData = { |
| 22 | + diagnostics: buildResults.diagnostics, |
| 23 | + }; |
| 24 | + } else { |
| 25 | + const stats: d.CompilerBuildStats = { |
| 26 | + timestamp: buildResults.timestamp, |
| 27 | + compiler: { |
| 28 | + name: config.sys.name, |
| 29 | + version: config.sys.version, |
| 30 | + }, |
| 31 | + app: { |
| 32 | + namespace: config.namespace, |
| 33 | + fsNamespace: config.fsNamespace, |
| 34 | + components: Object.keys(buildResults.componentGraph).length, |
| 35 | + entries: Object.keys(buildResults.componentGraph).length, |
| 36 | + bundles: buildResults.outputs.reduce((total, en) => total + en.files.length, 0), |
| 37 | + outputs: getAppOutputs(config, buildResults), |
| 38 | + }, |
| 39 | + options: { |
| 40 | + minifyJs: config.minifyJs, |
| 41 | + minifyCss: config.minifyCss, |
| 42 | + hashFileNames: config.hashFileNames, |
| 43 | + hashedFileNameLength: config.hashedFileNameLength, |
| 44 | + buildEs5: config.buildEs5, |
| 45 | + }, |
| 46 | + formats: { |
| 47 | + esmBrowser: sanitizeBundlesForStats(buildCtx.esmBrowserComponentBundle), |
| 48 | + esm: sanitizeBundlesForStats(buildCtx.esmComponentBundle), |
| 49 | + es5: sanitizeBundlesForStats(buildCtx.es5ComponentBundle), |
| 50 | + system: sanitizeBundlesForStats(buildCtx.systemComponentBundle), |
| 51 | + commonjs: sanitizeBundlesForStats(buildCtx.commonJsComponentBundle), |
| 52 | + }, |
| 53 | + components: getComponentsFileMap(config, buildCtx), |
| 54 | + entries: buildCtx.entryModules, |
| 55 | + componentGraph: buildResults.componentGraph, |
| 56 | + sourceGraph: getSourceGraph(config, buildCtx), |
| 57 | + rollupResults: buildCtx.rollupResults, |
| 58 | + collections: getCollections(config, buildCtx), |
| 59 | + }; |
| 60 | + |
| 61 | + jsonData = stats; |
| 62 | + } |
| 63 | + } catch (e) { |
| 64 | + jsonData = { |
| 65 | + diagnostics: [e.message], |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + return jsonData; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Writes the files from the stats config to the file system |
| 74 | + * @param config the project build configuration |
| 75 | + * @param buildCtx An instance of the build which holds the details about the build |
| 76 | + * @returns |
| 77 | + */ |
| 78 | +export async function writeBuildStats(config: d.Config, data: d.CompilerBuildStats | { diagnostics: d.Diagnostic[] }) { |
| 79 | + const statsTargets = config.outputTargets.filter(isOutputTargetStats); |
| 80 | + |
| 81 | + await Promise.all( |
| 82 | + statsTargets.map(async (outputTarget) => { |
| 83 | + const result = await config.sys.writeFile(outputTarget.file, JSON.stringify(data, null, 2)); |
| 84 | + |
| 85 | + if (result.error) { |
| 86 | + config.logger.warn([`Stats failed to write file to ${outputTarget.file}`]); |
| 87 | + } |
| 88 | + }) |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +function sanitizeBundlesForStats(bundleArray: ReadonlyArray<d.BundleModule>): ReadonlyArray<d.CompilerBuildStatBundle> { |
| 93 | + if (!bundleArray) { |
| 94 | + return []; |
| 95 | + } |
| 96 | + |
| 97 | + return bundleArray.map((bundle) => { |
| 98 | + return { |
| 99 | + key: bundle.entryKey, |
| 100 | + components: bundle.cmps.map((c) => c.tagName), |
| 101 | + bundleId: bundle.output.bundleId, |
| 102 | + fileName: bundle.output.fileName, |
| 103 | + imports: bundle.rollupResult.imports, |
| 104 | + // code: bundle.rollupResult.code, // (use this to debug) |
| 105 | + // Currently, this number is inaccurate vs what seems to be on disk. |
| 106 | + originalByteSize: byteSize(bundle.rollupResult.code), |
| 107 | + }; |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +function getSourceGraph(config: d.Config, buildCtx: d.BuildCtx) { |
| 112 | + let sourceGraph: d.BuildSourceGraph = {}; |
| 113 | + |
| 114 | + sortBy(buildCtx.moduleFiles, (m) => m.sourceFilePath).forEach((moduleFile) => { |
| 115 | + const key = relativePath(config, moduleFile.sourceFilePath); |
| 116 | + sourceGraph[key] = moduleFile.localImports.map((localImport) => relativePath(config, localImport)).sort(); |
| 117 | + }); |
| 118 | + |
| 119 | + return sourceGraph; |
| 120 | +} |
| 121 | + |
| 122 | +function getAppOutputs(config: d.Config, buildResults: d.CompilerBuildResults) { |
| 123 | + return buildResults.outputs.map((output) => { |
| 124 | + return { |
| 125 | + name: output.type, |
| 126 | + files: output.files.length, |
| 127 | + generatedFiles: output.files.map((file) => relativePath(config, file)), |
| 128 | + }; |
| 129 | + }); |
| 130 | +} |
| 131 | + |
| 132 | +function getComponentsFileMap(config: d.Config, buildCtx: d.BuildCtx) { |
| 133 | + return buildCtx.components.map((component) => { |
| 134 | + return { |
| 135 | + tag: component.tagName, |
| 136 | + path: relativePath(config, component.jsFilePath), |
| 137 | + source: relativePath(config, component.sourceFilePath), |
| 138 | + elementRef: component.elementRef, |
| 139 | + componentClassName: component.componentClassName, |
| 140 | + assetsDirs: component.assetsDirs, |
| 141 | + dependencies: component.dependencies, |
| 142 | + dependents: component.dependents, |
| 143 | + directDependencies: component.directDependencies, |
| 144 | + directDependents: component.directDependents, |
| 145 | + docs: component.docs, |
| 146 | + encapsulation: component.encapsulation, |
| 147 | + excludeFromCollection: component.excludeFromCollection, |
| 148 | + events: component.events, |
| 149 | + internal: component.internal, |
| 150 | + legacyConnect: component.legacyConnect, |
| 151 | + legacyContext: component.legacyContext, |
| 152 | + listeners: component.listeners, |
| 153 | + methods: component.methods, |
| 154 | + potentialCmpRefs: component.potentialCmpRefs, |
| 155 | + properties: component.properties, |
| 156 | + shadowDelegatesFocus: component.shadowDelegatesFocus, |
| 157 | + states: component.states, |
| 158 | + }; |
| 159 | + }); |
| 160 | +} |
| 161 | + |
| 162 | +function getCollections(config: d.Config, buildCtx: d.BuildCtx) { |
| 163 | + return buildCtx.collections |
| 164 | + .map((c) => { |
| 165 | + return { |
| 166 | + name: c.collectionName, |
| 167 | + source: relativePath(config, c.moduleDir), |
| 168 | + tags: c.moduleFiles.map((m) => m.cmps.map((cmp: d.ComponentCompilerMeta) => cmp.tagName)).sort(), |
| 169 | + }; |
| 170 | + }) |
| 171 | + .sort((a, b) => { |
| 172 | + if (a.name < b.name) return -1; |
| 173 | + if (a.name > b.name) return 1; |
| 174 | + return 0; |
| 175 | + }); |
| 176 | +} |
| 177 | + |
| 178 | +function relativePath(config: d.Config, file: string) { |
| 179 | + return config.sys.normalizePath(config.sys.platformPath.relative(config.rootDir, file)); |
| 180 | +} |
0 commit comments