|
1 | | -import { readFileSync, openSync, writeSync, close, readdirSync } from 'fs' |
2 | | -import { resolve, extname } from 'path' |
| 1 | +import { readFileSync } from 'fs' |
3 | 2 | import { defineConfig } from 'tsup' |
| 3 | +import type { NormalizedOptions, Format } from 'tsup' |
4 | 4 |
|
5 | | -async function prependLicense(): Promise<void> { |
6 | | - const outDir = resolve(process.cwd(), 'dist/') |
7 | | - const filenames = readdirSync(outDir, { |
8 | | - withFileTypes: true |
9 | | - }) |
| 5 | +interface Context { |
| 6 | + options: NormalizedOptions |
| 7 | + format: Format |
| 8 | + /** "type" field in project's package.json */ |
| 9 | + pkgType?: string |
| 10 | +} |
| 11 | + |
| 12 | +interface Result { |
| 13 | + js?: string |
| 14 | +} |
| 15 | + |
| 16 | +function generateLicense(): string { |
10 | 17 | const packInfo = JSON.parse(readFileSync('package.json').toString()) |
11 | | - const license = Buffer.from( |
12 | | - `/**\n * ${packInfo.name} v${packInfo.version}\n * Copyright (c) 2024 Ram Amoncar <ramamonkar444@gmail.com>\n * @license ${packInfo.license}\n */\n` |
13 | | - ) |
14 | | - if ( |
15 | | - packInfo !== undefined && |
16 | | - packInfo.version !== undefined && |
17 | | - packInfo.name !== undefined && |
18 | | - packInfo.license !== undefined |
19 | | - ) { |
20 | | - for (const file of filenames) { |
21 | | - if (extname(file.name) !== '.map' && file.isFile()) { |
22 | | - const data = readFileSync(resolve(outDir, file.name)) |
23 | | - const fd = openSync(resolve(outDir, file.name), 'w+') |
24 | | - writeSync(fd, license, 0, license.length, 0) |
25 | | - writeSync(fd, data, 0, data.length, license.length) |
26 | | - close(fd, (err) => { |
27 | | - if (err !== null) throw err |
28 | | - }) |
29 | | - } |
30 | | - } |
31 | | - } |
| 18 | + return `/**\n * ${packInfo.name} v${packInfo.version}\n * Copyright (c) 2024 Ram Amoncar <ramamonkar444@gmail.com>\n * @license ${packInfo.license}\n */` |
| 19 | +} |
| 20 | + |
| 21 | +const license = generateLicense() |
| 22 | + |
| 23 | +function outputExtensions(ctx: Context): Result { |
| 24 | + if (ctx.format === 'cjs') return { js: '.js' } |
| 25 | + if (ctx.format === 'esm') return { js: '.mjs' } |
| 26 | + return { js: '.min.js' } |
32 | 27 | } |
33 | 28 |
|
34 | 29 | export default defineConfig({ |
35 | | - entry: ['src/index.ts'], |
36 | | - format: ['cjs', 'esm'], // Build for commonJS and ESmodules |
37 | | - dts: true, // Generate declaration file (.d.ts) |
38 | 30 | splitting: false, |
39 | | - sourcemap: true, |
| 31 | + entry: ['src/index.ts'], |
| 32 | + // Generate declaration file (.d.ts & .d.mts) |
| 33 | + // And adds banner |
| 34 | + dts: { |
| 35 | + banner: license |
| 36 | + }, |
| 37 | + // Builds for commonJS, ESmodules & Browser |
| 38 | + format: ['cjs', 'esm', 'iife'], |
| 39 | + outExtension: outputExtensions, |
| 40 | + // Cleaning './dist/' before building |
40 | 41 | clean: true, |
41 | | - minify: true, |
42 | | - onSuccess: prependLicense |
| 42 | + // Header/Banner |
| 43 | + banner: { |
| 44 | + js: license |
| 45 | + }, |
| 46 | + // Minify Options |
| 47 | + minifyIdentifiers: false, |
| 48 | + minifySyntax: true, |
| 49 | + minifyWhitespace: true |
43 | 50 | }) |
0 commit comments