UNPKG

@codecov/astro-plugin

Version:
114 lines (109 loc) 3.27 kB
import { createVitePlugin } from 'unplugin'; import { red, checkNodeVersion, normalizeOptions, handleErrors, createSentryInstance, Output, telemetryPlugin } from '@codecov/bundler-plugin-core'; import { _internal_viteBundleAnalysisPlugin } from '@codecov/vite-plugin'; function getBundleName(initialName = "", target, format, name) { let bundleName = name ? `${initialName}-${name}-${target}` : `${initialName}-${target}`; format = format === "es" ? "esm" : format; bundleName = `${bundleName}-${format}`; return bundleName; } const astroBundleAnalysisPlugin = ({ output, target, pluginName, pluginVersion }) => ({ version: output.version, name: pluginName, pluginVersion, vite: { generateBundle(options) { if (!output.bundleName || output.bundleName === "") { red("Bundle name is not present or empty. Skipping upload."); return; } const name = getBundleName( output.originalBundleName, target, options.format, options.name ); output.unlockBundleName(); output.setBundleName(name); output.lockBundleName(); output.setPlugin(pluginName, pluginVersion); } } }); const PLUGIN_NAME = "@codecov/astro-plugin"; const PLUGIN_VERSION = "1.9.1"; const astroPluginFactory = createVitePlugin( ({ target, ...userOptions }, unpluginMetaContext) => { if (checkNodeVersion(unpluginMetaContext)) { return []; } const normalizedOptions = normalizeOptions(userOptions); if (!normalizedOptions.success) { const { shouldExit } = handleErrors(normalizedOptions); if (shouldExit) { process.exit(1); } return []; } const plugins = []; const options = normalizedOptions.options; const sentryConfig = createSentryInstance({ telemetry: options.telemetry, isDryRun: options.dryRun, pluginName: PLUGIN_NAME, pluginVersion: PLUGIN_VERSION, options, bundler: unpluginMetaContext.framework, metaFramework: "astro" }); const output = new Output( options, { metaFramework: unpluginMetaContext.framework }, sentryConfig ); if (options.enableBundleAnalysis) { plugins.push( telemetryPlugin({ sentryClient: sentryConfig.sentryClient, sentryScope: sentryConfig.sentryScope, telemetry: options.telemetry }), astroBundleAnalysisPlugin({ output, target, pluginName: PLUGIN_NAME, pluginVersion: PLUGIN_VERSION }), _internal_viteBundleAnalysisPlugin({ output, pluginName: PLUGIN_NAME, pluginVersion: PLUGIN_VERSION }) ); } return plugins; } ); const codecovAstroPlugin = (options) => ({ name: PLUGIN_NAME, hooks: { // target is type "client" | "server" so instead of determining that on our // own we can just utilize this value. "astro:build:setup": ({ vite, target }) => { if (vite?.plugins) { const astroPlugin = astroPluginFactory({ ...options, target }); vite.plugins.push(astroPlugin); } } } }); export { codecovAstroPlugin as default }; //# sourceMappingURL=index.mjs.map