UNPKG

@relative-ci/agent

Version:

Send bundle stats and CI build information to RelativeCI

83 lines (77 loc) 3.06 kB
'use strict'; var webpack = require('webpack'); var _ = require('lodash'); var debug = require('./utils/debug.js'); require('child_process'); var getEnvVars = require('./utils/get-env-vars.js'); var logResponse = require('./utils/log-response.js'); var constants = require('./constants.js'); var normalizeParams = require('./utils/normalize-params.js'); var ingest = require('./ingest/ingest.js'); var filterArtifacts = require('./artifacts/filter-artifacts.js'); var validateWebpackStats = require('./artifacts/validate-webpack-stats.js'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var webpack__default = /*#__PURE__*/_interopDefault(webpack); var ___default = /*#__PURE__*/_interopDefault(_); const PLUGIN_NAME = 'RelativeCiAgent'; const DEFAULT_OPTIONS = { includeCommitMessage: true, payloadFilepath: null, stats: { assets: true, chunks: true, modules: true, }, }; const isWebpack5 = parseInt(webpack__default.default.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.validateWebpackStats(data); const params = normalizeParams.normalizeParams({}, config); const artifactsData = filterArtifacts.filterArtifacts([{ key: constants.SOURCE_WEBPACK_STATS, data }]); const response = await ingest(artifactsData, params, config, logger); logResponse.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.getEnvVars(); const options = ___default.default.merge({}, DEFAULT_OPTIONS, { enabled: isCi, }, this.options); debug.debug(options); // Skip if not enabled if (!options.enabled) { debug.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__default.default.Compilation.PROCESS_ASSETS_STAGE_REPORT }, () => sendStats(compilation, options)); }); return; } compiler.hooks.emit.tapAsync(PLUGIN_NAME, async (compilation, callback) => { await sendStats(compilation, options); callback(); }); } } exports.RelativeCiAgentWebpackPlugin = RelativeCiAgentWebpackPlugin; //# sourceMappingURL=webpack-plugin.js.map