@codecov/bundle-analyzer
Version:
Official Codecov Bundle Analyzer
137 lines (128 loc) • 4.37 kB
JavaScript
;
const bundlerPluginCore = require('@codecov/bundler-plugin-core');
const path = require('node:path');
const fs = require('node:fs/promises');
const micromatch = require('micromatch');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
const micromatch__default = /*#__PURE__*/_interopDefaultCompat(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__default.resolve(buildDirectoryPath);
const files = await listChildFilePaths(absoluteAssetsDir);
const filteredFiles = ignorePatterns.length ? files.filter(
(file) => !micromatch__default.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__default.relative(parentPath, filePath);
const fileContents = await fs__default.readFile(filePath);
const size = fileContents.byteLength;
const gzipSize = await bundlerPluginCore.getCompressedSize({ fileName, code: fileContents });
const normalizedName = bundlerPluginCore.normalizePath(
fileName,
normalizeAssetsPattern,
"bundle-analyzer"
);
return {
name: fileName,
size,
gzipSize,
normalized: normalizedName
};
};
const listChildFilePaths = async (directoryPath) => {
const results = [];
const list = await fs__default.readdir(directoryPath, {
withFileTypes: true
});
for (const file of list) {
const fullPath = path__default.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 = bundlerPluginCore.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 bundlerPluginCore.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;
};
exports.createAndUploadReport = createAndUploadReport;
//# sourceMappingURL=index.cjs.map