_comuns/tsup/tsup.config.ouro.ts
2025-10-23 21:57:15 -03:00

89 lines
2.5 KiB
TypeScript

// tsup.config.ts
import { defineConfig } from 'tsup'
import fs from 'node:fs'
import path from 'node:path'
// --- 0) Atualiza package.json antes do build (determinístico) ---
const pkgPath = path.resolve('package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
// Agora vamos de Day.js “turbinado”: CJS + ESM + IIFE
const nextPkg = {
...pkg,
main: 'dist/index.cjs', // Node / SSR (require)
module: 'dist/index.mjs', // Bundlers ESM
types: 'dist/index.d.ts', // Tipos
browser: 'dist/index.global.js', // Browser sem bundler (<script>)
unpkg: 'dist/index.global.js',
jsdelivr: 'dist/index.global.js',
// Remove campos que atrapalham o resolver:
type: undefined as unknown as string,
exports: undefined as unknown as Record<string, unknown>,
}
// remove chaves undefined
for (const k of Object.keys(nextPkg)) {
// @ts-ignore
if (nextPkg[k] === undefined) delete (nextPkg as any)[k]
}
fs.writeFileSync(pkgPath, JSON.stringify(nextPkg, null, 2) + '\n')
// --- 1) Externals automáticos ---
const external = [
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.peerDependencies ?? {}),
]
// --- 2) Config tsup: CJS + ESM + IIFE ---
export default defineConfig([
// 2.1) CJS para Node/SSR (gera dist/index.cjs + dist/index.d.ts)
{
entry: { index: 'src/index.ts' },
outDir: 'dist',
format: ['cjs'],
dts: true, // emite tipos aqui
platform: 'neutral',
target: 'es2018',
treeshake: true,
sourcemap: false,
minify: true,
clean: true, // limpa dist nesta job
external,
skipNodeModulesBundle: true,
outExtension: () => ({ js: '.cjs' }),
},
// 2.2) ESM para bundlers (gera dist/index.mjs)
{
entry: { index: 'src/index.ts' },
outDir: 'dist',
format: ['esm'],
dts: false, // tipos já emitidos
platform: 'neutral',
target: 'es2018',
treeshake: true,
sourcemap: false,
minify: true,
clean: false,
external,
skipNodeModulesBundle: true,
outExtension: () => ({ js: '.mjs' }),
},
// 2.3) IIFE global para <script> (gera dist/index.global.js)
{
entry: { index: 'src/index.ts' },
outDir: 'dist',
format: ['iife'],
dts: false,
platform: 'browser',
target: 'es2015',
globalName: 'PComuns', // window.PComuns
treeshake: true,
sourcemap: false,
minify: true,
clean: false,
external,
skipNodeModulesBundle: true,
outExtension: () => ({ js: '.global.js' }),
},
])