72 lines
2.4 KiB
TypeScript
72 lines
2.4 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'))
|
|
|
|
// Define os campos ao estilo “dayjs-like”: 1 entry CJS + 1 bundle global
|
|
const nextPkg = {
|
|
...pkg,
|
|
main: 'dist/index.cjs', // Node / SSR (require)
|
|
types: 'dist/index.d.ts', // Tipos
|
|
// NÃO usar "module" aqui (muitos bundlers esperam ESM) — deixa sem.
|
|
browser: 'dist/index.global.js', // Browser sem bundler (<script>)
|
|
unpkg: 'dist/index.global.js', // CDN unpkg
|
|
jsdelivr: 'dist/index.global.js', // CDN jsDelivr
|
|
// Remove conflitos:
|
|
type: undefined as unknown as string,
|
|
exports: undefined as unknown as Record<string, unknown>,
|
|
}
|
|
// Remove chaves undefined de fato
|
|
Object.keys(nextPkg).forEach((k) => {
|
|
// @ts-ignore
|
|
if (nextPkg[k] === undefined) delete (nextPkg as any)[k]
|
|
})
|
|
fs.writeFileSync(pkgPath, JSON.stringify(nextPkg, null, 2) + '\n')
|
|
|
|
// --- 1) Externals automáticos (não embutir deps/peerDeps) ---
|
|
const external = [
|
|
...Object.keys(pkg.dependencies ?? {}),
|
|
...Object.keys(pkg.peerDependencies ?? {}),
|
|
]
|
|
|
|
// --- 2) Configuração tsup: CJS + IIFE (global) ---
|
|
export default defineConfig([
|
|
// 2.1) Bundle CJS para Node/SSR (gera dist/index.cjs + dist/index.d.ts)
|
|
{
|
|
entry: { index: 'src/index.ts' },
|
|
outDir: 'dist',
|
|
format: ['cjs'],
|
|
dts: true, // tipos aqui (um só lugar)
|
|
platform: 'neutral', // evita polyfills de node
|
|
target: 'es2018',
|
|
treeshake: true,
|
|
sourcemap: false,
|
|
minify: true,
|
|
clean: true, // limpa dist apenas nesta job
|
|
external,
|
|
skipNodeModulesBundle: true,
|
|
outExtension: () => ({ js: '.cjs' }),
|
|
},
|
|
|
|
// 2.2) Bundle global para browser (IIFE) → dist/index.global.js
|
|
{
|
|
entry: { index: 'src/index.ts' },
|
|
outDir: 'dist',
|
|
format: ['iife'],
|
|
dts: false, // tipos já emitidos na primeira
|
|
platform: 'browser',
|
|
target: 'es2015',
|
|
globalName: 'PComuns', // expõe window.PComuns
|
|
treeshake: true,
|
|
sourcemap: false,
|
|
minify: true,
|
|
clean: false,
|
|
external, // mantém deps externas se houver
|
|
skipNodeModulesBundle: true,
|
|
outExtension: () => ({ js: '.global.js' }),
|
|
},
|
|
])
|