-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
85 lines (75 loc) · 2.48 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import esbuild from 'esbuild'
import path from 'path'
import fs from 'fs/promises'
import { exec } from 'child_process'
import { promisify } from 'util'
/**
* Recursively get all .ts files in a directory.
* @param dir - The directory to search.
* @returns An array of file paths.
*/
async function getAllFiles(dir: string): Promise<string[]> {
const dirents = await fs.readdir(dir, { withFileTypes: true })
const files = await Promise.all(
dirents.map((dirent) => {
const res = path.resolve(dir, dirent.name)
return dirent.isDirectory() ? getAllFiles(res) : res.endsWith('.ts') ? [res] : []
}),
)
return Array.prototype.concat(...files)
}
const execAsync = promisify(exec)
const entryDir = 'packages/core'
const entry = path.join(entryDir, 'index.ts')
const outDir = 'lib'
const external = ['react', 'react-native', 'use-sync-external-store/shim/with-selector']
// Ensure output directories
await fs.mkdir(path.join(outDir, 'cjs'), { recursive: true })
await fs.mkdir(path.join(outDir, 'esm'), { recursive: true })
await fs.mkdir(path.join(outDir, entryDir), { recursive: true })
// Copy source files for react-native compatibility
await fs.cp(entryDir, path.join(outDir, 'src'), { recursive: true })
// CommonJS build (single file)
await esbuild.build({
entryPoints: [entry],
bundle: true,
format: 'cjs',
outfile: path.join(outDir, 'cjs/index.js'),
minify: true,
preserveSymlinks: true,
external,
})
// ESM build (files as they are)
await esbuild.build({
entryPoints: await getAllFiles(entryDir),
bundle: false,
format: 'esm',
outdir: path.join(outDir, 'esm'),
minify: true,
preserveSymlinks: true,
// external,
})
// TypeScript types generation using tsconfig.types.json
await execAsync('bunx tsc --project tsconfig.types.json --module ESNext --outDir lib/types --emitDeclarationOnly true')
import packageJson from './package.json'
// @ts-ignore
delete packageJson.scripts
// @ts-ignore
delete packageJson.devDependencies
// @ts-ignore
delete packageJson.private
// @ts-ignore
delete packageJson.workspaces
// Copy package.json and README.md
await fs.writeFile(path.join(outDir, 'package.json'), JSON.stringify(packageJson, null, 2))
// Copy README.md
await fs.copyFile('README.md', path.join(outDir, 'README.md'))
// Copy LICENSE
await fs.copyFile('LICENSE', path.join(outDir, 'LICENSE'))
// Check also .npmrc if exist, if so copy it
try {
await fs.copyFile('.npmrc', path.join(outDir, '.npmrc'))
} catch {
console.log('No .npmrc file found')
}
// Copy all core