"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BUNDLE_STATS_ENV_VAR = void 0;
exports.bundleStatsDir = bundleStatsDir;
exports.writeBundleStats = writeBundleStats;
function _path() {
  const data = require("path");
  _path = function () {
    return data;
  };
  return data;
}
function _fsExtra() {
  const data = require("fs-extra");
  _fsExtra = function () {
    return data;
  };
  return data;
}
/**
 * Set to a directory to write a stats file per compilation, or to `1`/`true` to write them under
 * `<cwd>/bundle-stats`. Off by default: collecting the module graph is not free, and the stats file
 * is far larger than the bundle it describes.
 */
const BUNDLE_STATS_ENV_VAR = exports.BUNDLE_STATS_ENV_VAR = 'BIT_UI_BUNDLE_STATS';
function bundleStatsDir() {
  const value = process.env[BUNDLE_STATS_ENV_VAR];
  if (!value) return undefined;
  if (value === '1' || value === 'true') return (0, _path().join)(process.cwd(), 'bundle-stats');
  return value;
}

/**
 * Write an rspack stats file for a finished compilation, for `scripts/analyze-bundle.mjs` to read.
 *
 * Deliberately written outside the output directory: everything under an output dir is matched by
 * the build task's artifact glob, so a stats file placed there would ship inside the package.
 */
function writeBundleStats(stats, name) {
  const dir = bundleStatsDir();
  if (!dir || !stats) return undefined;
  const json = stats.toJson({
    all: false,
    assets: true,
    chunks: true,
    chunkModules: true,
    modules: true,
    reasons: false,
    source: false
  });
  (0, _fsExtra().mkdirpSync)(dir);
  const filePath = (0, _path().join)(dir, `${name}.stats.json`);
  (0, _fsExtra().writeFileSync)(filePath, JSON.stringify(json));
  return filePath;
}

//# sourceMappingURL=bundle-stats.js.map