@codecov/bundle-analyzer
Version:
Official Codecov Bundle Analyzer
129 lines (123 loc) • 3.92 kB
JavaScript
import { getCompressedSize, normalizePath, normalizeOptions, Output } from '@codecov/bundler-plugin-core';
import path from 'node:path';
import fs from 'node:fs/promises';
import micromatch from 'micromatch';
const PLUGIN_NAME = (
// @ts-expect-error - value replaced by rollup
(
// @ts-expect-error - value replaced by rollup
"@codecov/bundle-analyzer"
)
);
const PLUGIN_VERSION = (
// @ts-expect-error - value replaced by rollup
"1.9.0"
);
const defaultBundleAnalyzerOptions = {
// eslint-disable-next-line @typescript-eslint/require-await
beforeReportUpload: async (original) => original,
ignorePatterns: [],
normalizeAssetsPattern: ""
};
function normalizeBundleAnalyzerOptions(options = {}) {
return {
...defaultBundleAnalyzerOptions,
...options
};
}
const getAssets = async (buildDirectoryPaths, ignorePatterns = [], normalizeAssetsPattern = "") => {
const allAssets = await Promise.all(
buildDirectoryPaths.map(async (buildDirectoryPath) => {
const absoluteAssetsDir = path.resolve(buildDirectoryPath);
const files = await listChildFilePaths(absoluteAssetsDir);
const filteredFiles = ignorePatterns.length ? files.filter(
(file) => !micromatch.isMatch(file, ignorePatterns, {
dot: true,
matchBase: true
})
) : files;
const assets = await Promise.all(
filteredFiles.map(
(file) => getAsset(file, absoluteAssetsDir, normalizeAssetsPattern)
)
);
return assets;
})
);
return allAssets.flat();
};
const getAsset = async (filePath, parentPath, normalizeAssetsPattern) => {
const fileName = path.relative(parentPath, filePath);
const fileContents = await fs.readFile(filePath);
const size = fileContents.byteLength;
const gzipSize = await getCompressedSize({ fileName, code: fileContents });
const normalizedName = normalizePath(
fileName,
normalizeAssetsPattern,
"bundle-analyzer"
);
return {
name: fileName,
size,
gzipSize,
normalized: normalizedName
};
};
const listChildFilePaths = async (directoryPath) => {
const results = [];
const list = await fs.readdir(directoryPath, {
withFileTypes: true
});
for (const file of list) {
const fullPath = path.join(directoryPath, file.name);
if (file.isDirectory()) {
const childPaths = await listChildFilePaths(fullPath);
childPaths.forEach((childFile) => results.push(childFile));
} else if (file.isFile()) {
results.push(fullPath);
}
}
return results;
};
const createAndUploadReport = async (buildDirectoryPaths, coreOptions, bundleAnalyzerOptions) => {
const coreOpts = normalizeOptions(coreOptions);
if (!coreOpts.success) {
throw new Error("Invalid options: " + coreOpts.errors.join(" "));
}
const bundleAnalyzerOpts = normalizeBundleAnalyzerOptions(
bundleAnalyzerOptions
);
const initialReport = await createReport(
buildDirectoryPaths,
coreOpts.options,
bundleAnalyzerOpts
);
let finalReport;
try {
finalReport = await bundleAnalyzerOpts.beforeReportUpload(initialReport);
} catch (error) {
throw new Error(`Error in beforeReportUpload: ${error}`);
}
if (!coreOptions.dryRun) {
await finalReport.write(true);
}
return finalReport.bundleStatsToJson();
};
const createReport = async (buildDirectoryPaths, normalizedCoreOptions, normalizedBundleAnalyzerOptions) => {
const output = new Output(normalizedCoreOptions, {
metaFramework: "bundle-analyzer"
});
output.start();
output.setPlugin(PLUGIN_NAME, PLUGIN_VERSION);
output.assets = await getAssets(
buildDirectoryPaths,
normalizedBundleAnalyzerOptions.ignorePatterns,
normalizedBundleAnalyzerOptions.normalizeAssetsPattern
);
output.chunks = [];
output.modules = [];
output.end();
return output;
};
export { createAndUploadReport };
//# sourceMappingURL=index.mjs.map