#!/bin/sh
# Upstream's build runs two babel invocations in parallel through npm-run-all,
# which Debian does not ship; running them one after the other is equivalent.
# The --env-name values select the api.env() branches in babel.config.js: "esm"
# keeps the module syntax, "cjs" rewrites it to CommonJS.
set -e
babeljs src -d dist/esm --env-name esm --copy-files --no-copy-ignored
babeljs src -d dist/cjs --env-name cjs --copy-files --no-copy-ignored

# package.json declares "type": "module", so dist/cjs needs its own marker, and
# upstream also appends the default export for the CommonJS entry point.
node -e 'const fs = require("fs");
fs.writeFileSync("dist/cjs/package.json", "{\"type\":\"commonjs\"}\n");
fs.appendFileSync("dist/cjs/index.js", "module.exports = exports.default;\nmodule.exports.default = exports.default;\n");'
