UNPKG

@codecov/astro-plugin

Version:
116 lines (110 loc) 3.31 kB
'use strict'; const unplugin = require('unplugin'); const bundlerPluginCore = require('@codecov/bundler-plugin-core'); const vitePlugin = require('@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 === "") { bundlerPluginCore.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 = unplugin.createVitePlugin( ({ target, ...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, metaFramework: "astro" }); 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 }), astroBundleAnalysisPlugin({ output, target, pluginName: PLUGIN_NAME, pluginVersion: PLUGIN_VERSION }), vitePlugin._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); } } } }); module.exports = codecovAstroPlugin; //# sourceMappingURL=index.cjs.map