// Rollup configuration reproducing upstream's Vite dist build under Debian.
//
// Upstream builds dist/ with Vite (support/build-dist.mjs); since Vite uses
// Rollup internally, we reproduce the three published artifacts directly with
// Rollup, avoiding a Vite build-dependency:
//   * dist/js-yaml.js      UMD, readable, ES5
//   * dist/js-yaml.min.js  UMD, minified, ES5
//   * dist/js-yaml.mjs     ES module
//
// Upstream downlevels index.js and lib/ to ES5 with @babel/preset-env; buble
// does the same for the syntax used here without adding a build-dependency
// loop (babel -> eslint -> js-yaml).
//
// Plugins and package.json are loaded through createRequire because Debian's
// Node only augments the CommonJS resolver with /usr/share/nodejs, not the
// native ESM resolver used for `import` in a .mjs config.
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const buble = require('@rollup/plugin-buble')
const commonjs = require('@rollup/plugin-commonjs')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const terser = require('@rollup/plugin-terser')
const pkg = require('../package.json')

const banner = `/*! ${pkg.name} ${pkg.version} https://github.com/${pkg.repository} @license ${pkg.license} */`

// Same entry as the Vite build: a thin proxy re-exporting the CommonJS API
// both as named exports and as a default export.
const input = 'lib/index_vite_proxy.tmp.mjs'

const es5 = buble({
  include: ['index.js', 'lib/**'],
  transforms: { modules: false }
})

const plugins = [es5, nodeResolve(), commonjs()]

// generatedCode: 'es5' keeps Rollup's own helpers ES5 (no Symbol.toStringTag);
// terser strips the ES6 comments upstream's babel/terser pass also drops.
const umd = {
  format: 'umd',
  name: 'jsyaml',
  exports: 'named',
  esModule: true,
  generatedCode: 'es5',
  banner
}

export default [
  {
    input,
    output: [
      {
        ...umd,
        file: 'dist/js-yaml.js',
        plugins: [terser({
          ecma: 5,
          compress: false,
          mangle: false,
          format: { beautify: true, comments: /^!/ }
        })]
      },
      {
        ...umd,
        file: 'dist/js-yaml.min.js',
        plugins: [terser({
          ecma: 5,
          compress: true,
          mangle: true,
          format: { comments: /^!/ }
        })]
      }
    ],
    plugins
  },
  {
    input,
    output: [
      { format: 'es', file: 'dist/js-yaml.mjs', banner }
    ],
    plugins: [nodeResolve(), commonjs()]
  }
]
