-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreconstruct.ts
55 lines (42 loc) · 1.59 KB
/
preconstruct.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
import * as fs from 'node:fs'
import { basename, dirname, resolve } from 'node:path'
import { getExports } from './utils/exports.js'
// biome-ignore lint/suspicious/noConsoleLog:
console.log('Setting up packages for development.')
const packagePath = resolve(import.meta.dirname, '../src/package.json')
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf-8'))
// biome-ignore lint/suspicious/noConsoleLog:
console.log(`${packageJson.name} — ${dirname(packagePath)}`)
const dir = resolve(dirname(packagePath))
// Empty dist directories
fs.rmSync(resolve(dir, '_dist'), { recursive: true, force: true })
const exports = getExports()
// Link exports to dist locations
for (const [key, distExports] of Object.entries(exports.dist ?? {})) {
// Skip `package.json` exports
if (/package\.json$/.test(key)) continue
let entries: any
if (typeof distExports === 'string')
entries = [
['default', distExports],
['types', distExports.replace('.js', '.d.ts')],
]
else entries = Object.entries(distExports as {})
// Link exports to dist locations
for (const [, value] of entries as [
type: 'types' | 'default',
value: string,
][]) {
const srcFilePath = resolve(dir, exports.src[key]!)
const distDir = resolve(dir, dirname(value))
const distFileName = basename(value)
const distFilePath = resolve(distDir, distFileName)
fs.mkdirSync(distDir, { recursive: true })
// Symlink src to dist file
try {
fs.symlinkSync(srcFilePath, distFilePath, 'file')
} catch {}
}
}
// biome-ignore lint/suspicious/noConsoleLog:
console.log('Done.')