UNPKG

@relative-ci/agent

Version:

Send bundle stats and CI build information to RelativeCI

76 lines (73 loc) 2.68 kB
import webpack from 'webpack'; import _ from 'lodash'; import { debug } from './utils/debug.js'; import 'child_process'; import { getEnvVars } from './utils/get-env-vars.js'; import { logResponse } from './utils/log-response.js'; import { SOURCE_WEBPACK_STATS } from './constants.js'; import { normalizeParams } from './utils/normalize-params.js'; import ingest from './ingest/ingest.js'; import { filterArtifacts } from './artifacts/filter-artifacts.js'; import { validateWebpackStats } from './artifacts/validate-webpack-stats.js'; const PLUGIN_NAME = 'RelativeCiAgent'; const DEFAULT_OPTIONS = { includeCommitMessage: true, payloadFilepath: null, stats: { assets: true, chunks: true, modules: true, }, }; const isWebpack5 = parseInt(webpack.version, 10) === 5; async function sendStats(compilation, options) { const { stats: statsOptions, failOnError, ...config } = options; const data = compilation.getStats().toJson(statsOptions); const logger = compilation.compiler?.getInfrastructureLogger ? compilation.compiler.getInfrastructureLogger(PLUGIN_NAME) : console; try { validateWebpackStats(data); const params = normalizeParams({}, config); const artifactsData = filterArtifacts([{ key: SOURCE_WEBPACK_STATS, data }]); const response = await ingest(artifactsData, params, config, logger); logResponse(response); } catch (error) { if (failOnError) { compilation.errors.push(error); } else { logger.warn(error); // catch error to prevent failure on error } } } class RelativeCiAgentWebpackPlugin { constructor(options) { this.options = options; } apply(compiler) { const { isCi } = getEnvVars(); const options = _.merge({}, DEFAULT_OPTIONS, { enabled: isCi, }, this.options); debug(options); // Skip if not enabled if (!options.enabled) { debug(`${PLUGIN_NAME} is disabled, skip sending data`); return; } if (isWebpack5) { compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => { compilation.hooks.processAssets.tap({ name: PLUGIN_NAME, stage: webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT }, () => sendStats(compilation, options)); }); return; } compiler.hooks.emit.tapAsync(PLUGIN_NAME, async (compilation, callback) => { await sendStats(compilation, options); callback(); }); } } export { RelativeCiAgentWebpackPlugin }; //# sourceMappingURL=webpack-plugin.js.map