74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { defineConfig } from "tsup";
|
|
|
|
/**
|
|
* Build del servidor:
|
|
* - incluye código TS de `modules/*` (buildless)
|
|
* - consume código ya compilado de `packages/*`
|
|
* - genera un bundle único ESM en `dist/`
|
|
*/
|
|
|
|
export default defineConfig({
|
|
entry: ["src/index.ts"], // punto de entrada principal
|
|
format: ["esm"], // ESM nativo (Node 18+)
|
|
target: "node22", // objetivo de compilación
|
|
bundle: true, // genera un único bundle, evita imports rotos
|
|
sourcemap: false,
|
|
clean: true,
|
|
minify: false,
|
|
treeshake: true,
|
|
dts: false, // opcional, genera .d.ts
|
|
outDir: "dist",
|
|
platform: "node",
|
|
|
|
// Paquetes de npm a mantener como externos (no se incluyen en el "bundle")
|
|
external: [],
|
|
|
|
noExternal: ["@repo", "@erp"],
|
|
|
|
esbuildOptions(options) {
|
|
// Permite resolver imports sin extensión (.ts, .js, .mjs, etc.)
|
|
options.resolveExtensions = [".ts", ".js", ".mjs", ".json"];
|
|
|
|
// Corrige la extensión de salida
|
|
options.outExtension = { ".js": ".js" };
|
|
|
|
// Permite usar require en contexto ESM (Node >=18)
|
|
options.banner = {
|
|
js: `
|
|
import { createRequire } from "module";
|
|
const require = createRequire(import.meta.url);
|
|
`,
|
|
};
|
|
},
|
|
|
|
// Plugin: fuerza a que los imports relativos se traten como locales, no "external"
|
|
esbuildPlugins: [
|
|
{
|
|
name: "fix-local-imports",
|
|
setup(build) {
|
|
build.onResolve({ filter: /^\.{1,2}\// }, (args) => {
|
|
const abs = path.resolve(args.resolveDir, args.path);
|
|
|
|
// Si es un directorio, intenta resolver a index.(ts|js)
|
|
if (fs.existsSync(abs) && fs.statSync(abs).isDirectory()) {
|
|
const indexTs = path.join(abs, "index.ts");
|
|
const indexJs = path.join(abs, "index.js");
|
|
if (fs.existsSync(indexTs)) return { path: indexTs, external: false };
|
|
if (fs.existsSync(indexJs)) return { path: indexJs, external: false };
|
|
}
|
|
|
|
// Si es un fichero con extensión válida
|
|
const withExts = [".ts", ".js", ".mjs"];
|
|
for (const ext of withExts) {
|
|
if (fs.existsSync(abs + ext)) return { path: abs + ext, external: false };
|
|
}
|
|
|
|
return { path: abs, external: false };
|
|
});
|
|
},
|
|
},
|
|
],
|
|
});
|