"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
fixCjsDts: () => fixCjsDts,
fixCjsExports: () => fixCjsExports,
reflect: () => reflect
});
module.exports = __toCommonJS(src_exports);
// src/reflect.ts
var import_promises = __toESM(require("fs/promises"));
var import_node_path = __toESM(require("path"));
var import_fast_glob = __toESM(require("fast-glob"));
var import_kolorist = require("kolorist");
var reflect = async (options) => {
const { globOptions = {}, name = "reflect", reflect: reflect2, silent } = options;
const cwd = process.cwd();
const files = await (0, import_fast_glob.default)(options.files, {
cwd: import_node_path.default.resolve(cwd, "dist"),
...globOptions,
absolute: true,
ignore: [...globOptions.ignore || [], "node_modules"]
});
const logger = (message) => {
if (!silent)
console.log(`[${name}]`, message);
};
if (!files.length)
logger((0, import_kolorist.gray)("No files matched"));
const output = [];
for (const file of files) {
const code = await import_promises.default.readFile(file, "utf8");
const result = await reflect2(code);
const filename = import_node_path.default.relative(cwd, file);
if (result) {
await import_promises.default.writeFile(file, result);
output.push(result);
logger(`${(0, import_kolorist.green)("\u2714")} ${filename}`);
} else {
logger((0, import_kolorist.gray)(`skip ${filename}`));
}
}
return output;
};
// src/fix-cjs-dts.ts
var exportRegex = /export \{\s*(?:\S.*)?\b(?<name>.+) as default.*(?:[\n\r\u{2028}\u{2029}]\s*)?\};/u;
var fixCjsDts = async (options) => {
return reflect({
files: "**/*.d.{ts,cts}",
...options,
name: "fix-cjs-dts",
reflect: (code) => {
const result = exportRegex.exec(code);
if (result?.groups?.name) {
const statement = `export = ${result.groups.name}`;
if (!code.endsWith(statement))
return code + statement;
}
}
});
};
// src/fix-cjs-exports.ts
var statements = `
// fix-cjs-exports
if (module.exports.default) {
Object.assign(module.exports.default, module.exports);
module.exports = module.exports.default;
delete module.exports.default;
}
`;
var fixCjsExports = async (options) => {
const { readPackage } = await import("read-pkg");
const { type } = await readPackage();
const isEsm = type === "module";
const suffix = isEsm ? "cjs" : "js";
const files = options?.files?.length ? options.files : `**/*.${suffix}`;
return reflect({
...options,
files,
name: "fix-cjs-exports",
reflect: (code) => {
if (code.includes("module.exports = __toCommonJS") && !code.endsWith(statements)) {
return code + statements;
}
}
});
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
fixCjsDts,
fixCjsExports,
reflect
});