File size: 1,476 Bytes
1e92f2d |
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 |
import { Alias } from "@rollup/plugin-alias"
import { rmSync } from "fs"
import { join } from "path/posix"
import * as rollup from "rollup"
import { getConfig } from "./config.js"
import { generateTypes } from "./tsc.js"
interface BuildOptions {
dir: string
name: string
watch?: boolean
clean?: boolean
dts?: boolean
aliases?: Alias[]
}
export async function buildProject(options: BuildOptions) {
const { dir, name, watch, clean, dts, aliases = [] } = options
console.log(`[${name}] Building...`)
if (clean) {
//
const distDir = join(dir, "dist")
rmSync(distDir, { recursive: true, force: true })
}
const config = await getConfig({ dir, aliases })
if (watch) {
//
config.watch = {
include: config.input as string[],
chokidar: { ignoreInitial: true },
}
const watcher = rollup.watch(config)
console.log(`[${name}][JS] Watching source files...`)
watcher.on("change", () => {
console.log(`[${name}][JS] File changed, rebuilding...`)
})
//
} else {
//
const build = await rollup.rollup(config)
const outputs: rollup.OutputOptions[] = Array.isArray(config.output)
? config.output
: [config.output!]
await Promise.all(outputs.map((output) => build.write(output)))
console.log(`[${name}][JS] Generated CJS and ESM files ✅`)
if (dts) {
await generateTypes(dir)
console.log(`[${name}][DTS] Generated type definitions ✅`)
}
}
}
|