#!/usr/bin/env node
"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 __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
));
// src/cli.ts
var import_node_path2 = __toESM(require("path"));
var import_cac = require("cac");
// package.json
var name = "fix-tsup-cjs";
var version = "1.2.0";
// 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: name2 = "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(`[${name2}]`, 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;
}
}
});
};
// src/to-array.ts
var toArray = (val) => {
if (!val)
return [];
return Array.isArray(val) ? val : [val];
};
// src/cli.ts
var cli = (0, import_cac.cac)(name);
cli.version(version);
cli.command("[...files]", "Custom matching files glob").option("--cwd [path]", "Set fix directory", { default: "dist" }).option("--dts", "Fix commonjs d.ts and d.cts files", { default: true }).option("-i, --ignore [...files]", "Ignore files").option("--silent", "Suppress logs").action(async (files, options) => {
const { dts, silent } = options;
const hasCustomMatchFiles = !!files?.length;
const currentDir = process.cwd();
const cwd = import_node_path2.default.resolve(currentDir, options.cwd);
const ignore = toArray(options.ignore);
await fixCjsExports({
files,
globOptions: {
cwd: hasCustomMatchFiles ? currentDir : cwd,
ignore
},
silent
});
if (dts) {
await fixCjsDts({
globOptions: {
cwd,
ignore
},
silent
});
}
});
cli.help();
cli.parse();