mais ajustes de build

This commit is contained in:
Luiz Silva 2025-10-23 21:57:15 -03:00
parent 9a332853b6
commit 889148eb42
3 changed files with 42 additions and 23 deletions

View file

@ -7,65 +7,82 @@ import path from 'node:path'
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
// 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
// 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:
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 de fato
Object.keys(nextPkg).forEach((k) => {
// 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 (não embutir deps/peerDeps) ---
// --- 1) Externals automáticos ---
const external = [
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.peerDependencies ?? {}),
]
// --- 2) Configuração tsup: CJS + IIFE (global) ---
// --- 2) Config tsup: CJS + ESM + IIFE ---
export default defineConfig([
// 2.1) Bundle CJS para Node/SSR (gera dist/index.cjs + dist/index.d.ts)
// 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, // tipos aqui (um só lugar)
platform: 'neutral', // evita polyfills de node
dts: true, // emite tipos aqui
platform: 'neutral',
target: 'es2018',
treeshake: true,
sourcemap: false,
minify: true,
clean: true, // limpa dist apenas nesta job
clean: true, // limpa dist nesta job
external,
skipNodeModulesBundle: true,
outExtension: () => ({ js: '.cjs' }),
},
// 2.2) Bundle global para browser (IIFE) → dist/index.global.js
// 2.2) ESM para bundlers (gera dist/index.mjs)
{
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
format: ['esm'],
dts: false, // tipos já emitidos
platform: 'neutral',
target: 'es2018',
treeshake: true,
sourcemap: false,
minify: true,
clean: false,
external, // mantém deps externas se houver
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' }),
},