@codecov/rollup-plugin
Version:
Official Codecov Rollup plugin
199 lines (192 loc) • 6.77 kB
JavaScript
'use strict';
const unplugin = require('unplugin');
const bundlerPluginCore = require('@codecov/bundler-plugin-core');
const path = require('node:path');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
const rollupBundleAnalysisPlugin = ({
output,
pluginName,
pluginVersion
}) => ({
version: output.version,
name: pluginName,
pluginVersion,
buildStart: () => {
output.start();
output.setPlugin(pluginName, pluginVersion);
},
buildEnd: () => {
output.end();
},
writeBundle: async () => {
await output.write();
},
rollup: {
async generateBundle(options, bundle) {
if (!output.bundleName || output.bundleName === "") {
bundlerPluginCore.red("Bundle name is not present or empty. Skipping upload.");
return;
}
output.setBundleName(output.originalBundleName);
if (options.name && options.name !== "") {
output.setBundleName(`${output.bundleName}-${options.name}`);
}
const format = options.format === "es" ? "esm" : options.format;
output.setBundleName(`${output.bundleName}-${format}`);
const cwd = process.cwd();
const assets = [];
const chunks = [];
const moduleByFileName = /* @__PURE__ */ new Map();
const items = Object.values(bundle);
const customOptions = {
moduleOriginalSize: false,
...options
};
let assetFormatString = "";
if (typeof customOptions.assetFileNames === "string") {
assetFormatString = customOptions.assetFileNames;
}
let chunkFormatString = "";
if (typeof customOptions.chunkFileNames === "string") {
chunkFormatString = customOptions.chunkFileNames;
}
let counter = 0;
await Promise.all(
items.map(async (item) => {
if (item?.type === "asset") {
const fileName = item?.fileName ?? "";
if (path__default.extname(fileName) === ".map") {
return;
}
const asset = await bundlerPluginCore.createRollupAsset({
fileName,
source: item.source,
formatString: assetFormatString,
metaFramework: output.metaFramework
});
assets.push(asset);
} else if (item?.type === "chunk") {
const fileName = item?.fileName ?? "";
if (path__default.extname(fileName) === ".map") {
return;
}
const asset = await bundlerPluginCore.createRollupAsset({
fileName,
source: item.code,
formatString: chunkFormatString,
metaFramework: output.metaFramework
});
assets.push(asset);
const chunkId = item?.name ?? "";
const uniqueId = `${counter}-${chunkId}`;
chunks.push({
id: chunkId,
uniqueId,
entry: item?.isEntry,
initial: item?.isDynamicEntry,
files: [fileName],
names: [item?.name],
dynamicImports: item?.dynamicImports ?? []
});
const moduleEntries = Object.entries(item?.modules ?? {});
for (const [modulePath, moduleInfo] of moduleEntries) {
const normalizedModulePath = modulePath.replace("\0", "");
const relativeModulePath = path__default.relative(
cwd,
normalizedModulePath
);
const relativeModulePathWithPrefix = relativeModulePath.match(
/^\.\./
) ? relativeModulePath : `.${path__default.sep}${relativeModulePath}`;
const moduleEntry = moduleByFileName.get(
relativeModulePathWithPrefix
);
if (moduleEntry) {
moduleEntry.chunkUniqueIds.push(uniqueId);
} else {
const size = customOptions.moduleOriginalSize ? moduleInfo.originalLength : moduleInfo.renderedLength;
const module = {
name: relativeModulePathWithPrefix,
size,
chunkUniqueIds: [uniqueId]
};
moduleByFileName.set(relativeModulePathWithPrefix, module);
}
}
counter += 1;
}
return;
})
);
const modules = Array.from(moduleByFileName.values());
output.bundler = {
name: "rollup",
version: this.meta.rollupVersion
};
output.assets = assets;
output.chunks = chunks;
output.modules = modules;
output.outputPath = options.dir ?? "";
if (output.dryRun) {
this.emitFile({
type: "asset",
fileName: `${output.bundleName}-stats.json`,
source: output.bundleStatsToJson()
});
}
}
}
});
const PLUGIN_NAME = "@codecov/rollup-plugin";
const PLUGIN_VERSION = "1.9.1";
const codecovRollupPluginFactory = unplugin.createRollupPlugin(
(userOptions, unpluginMetaContext) => {
if (bundlerPluginCore.checkNodeVersion(unpluginMetaContext)) {
return [];
}
const normalizedOptions = bundlerPluginCore.normalizeOptions(userOptions);
if (!normalizedOptions.success) {
const { shouldExit } = bundlerPluginCore.handleErrors(normalizedOptions);
if (shouldExit) {
process.exit(1);
}
return [];
}
const plugins = [];
const options = normalizedOptions.options;
const sentryConfig = bundlerPluginCore.createSentryInstance({
telemetry: options.telemetry,
isDryRun: options.dryRun,
pluginName: PLUGIN_NAME,
pluginVersion: PLUGIN_VERSION,
options,
bundler: unpluginMetaContext.framework
});
const output = new bundlerPluginCore.Output(
options,
{ metaFramework: unpluginMetaContext.framework },
sentryConfig
);
if (options.enableBundleAnalysis) {
plugins.push(
bundlerPluginCore.telemetryPlugin({
sentryClient: sentryConfig.sentryClient,
sentryScope: sentryConfig.sentryScope,
telemetry: options.telemetry
}),
rollupBundleAnalysisPlugin({
output,
pluginName: PLUGIN_NAME,
pluginVersion: PLUGIN_VERSION
})
);
}
return plugins;
}
);
const codecovRollupPlugin = codecovRollupPluginFactory;
const _internal_rollupBundleAnalysisPlugin = rollupBundleAnalysisPlugin;
exports._internal_rollupBundleAnalysisPlugin = _internal_rollupBundleAnalysisPlugin;
exports.codecovRollupPlugin = codecovRollupPlugin;
//# sourceMappingURL=index.cjs.map