Skip to content

Commit

Permalink
feat!: support per-entry outDir
Browse files Browse the repository at this point in the history
genDir removed
  • Loading branch information
pi0 committed Apr 21, 2021
1 parent 5312206 commit 5bb7ac3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
21 changes: 11 additions & 10 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { promisify } from 'util'
import Module from 'module'
import { unlink } from 'fs/promises'
import { resolve, basename } from 'upath'
import chalk from 'chalk'
import consola from 'consola'
import rimraf from 'rimraf'
import mkdirp from 'mkdirp'
import defu from 'defu'
import prettyBytes from 'pretty-bytes'
import jiti from 'jiti'
import { dumpObject } from './utils'
import { dumpObject, cleanDir } from './utils'
import type { BuildContext } from './types'
import { validateDependencies } from './validate'
import { rollupBuild } from './builder/rollup'
Expand Down Expand Up @@ -54,15 +50,21 @@ export async function build (rootDir: string, stub: boolean) {
if (typeof entry.name !== 'string') {
entry.name = basename(entry.input)
}

if (!entry.input) {
throw new Error('Missing entry input: ' + dumpObject(entry))
}

if (!entry.builder) {
entry.builder = entry.input.endsWith('/') ? 'mkdist' : 'rollup'
}

if (ctx.declaration !== undefined && entry.declaration === undefined) {
entry.declaration = ctx.declaration
}

entry.input = resolve(ctx.rootDir, entry.input)
entry.outDir = resolve(ctx.rootDir, entry.outDir || ctx.outDir)
}

// Collect dependencies and devDependnecies
Expand All @@ -81,12 +83,11 @@ export async function build (rootDir: string, stub: boolean) {
`)
}

// Clean dist dir
// Clean dist dirs
if (ctx.clean) {
const outDir = resolve(ctx.rootDir, ctx.outDir)
await unlink(outDir).catch(() => { })
await promisify(rimraf)(outDir)
await mkdirp(outDir)
for (const dir of new Set(ctx.entries.map(e => e.outDir).sort())) {
await cleanDir(dir!)
}
}

// Try to selflink
Expand Down
11 changes: 5 additions & 6 deletions src/builder/mkdist.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { mkdist } from 'mkdist'
import { resolve, join } from 'upath'
import { join } from 'upath'
import { symlink } from '../utils'
import type { BuildContext } from '../types'

export async function mkdistBuild (ctx: BuildContext) {
for (const entry of ctx.entries.filter(e => e.builder === 'mkdist')) {
const distDir = join(entry.outDir, entry.name)
if (ctx.stub) {
const srcDir = resolve(ctx.rootDir, entry.input)
const outDir = resolve(ctx.rootDir, ctx.outDir, entry.name)
await symlink(srcDir, outDir)
await symlink(entry.input, distDir)
} else {
const { writtenFiles } = await mkdist({
rootDir: ctx.rootDir,
srcDir: entry.input,
distDir: join(ctx.outDir, entry.name),
distDir,
format: entry.format,
cleanDist: false,
declaration: entry.declaration
})
ctx.buildEntries.push({
path: join(ctx.outDir, entry.name),
path: distDir,
chunks: [`${writtenFiles.length} files`]
})
}
Expand Down
7 changes: 3 additions & 4 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import type { BuildContext } from '../types'
export async function rollupBuild (ctx: BuildContext) {
if (ctx.stub) {
for (const entry of ctx.entries.filter(entry => entry.builder === 'rollup')) {
const input = resolve(ctx.rootDir, entry.input)
const output = resolve(ctx.rootDir, ctx.outDir, entry.name)
await writeFile(output + '.js', `module.exports = require('jiti')()('${input}')`)
await writeFile(output + '.mjs', `export * from '${input}'`)
await writeFile(output + '.d.ts', `export * from '${input}'`)
await writeFile(output + '.js', `module.exports = require('jiti')()('${entry.input}')`)
await writeFile(output + '.mjs', `export * from '${entry.input}'`)
await writeFile(output + '.d.ts', `export * from '${entry.input}'`)
}
return
}
Expand Down
14 changes: 6 additions & 8 deletions src/builder/untyped.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { writeFile } from 'fs/promises'
import { resolve } from 'upath'
import { resolve, join } from 'upath'
import { resolveSchema, generateTypes, generateMarkdown } from 'untyped'
import untypedPlugin from 'untyped/dist/loader/babel'
import jiti from 'jiti'
import { pascalCase } from 'scule'
import mkdirp from 'mkdirp'
import type { BuildContext } from '../types'

export async function typesBuild (ctx: BuildContext) {
Expand All @@ -20,18 +19,17 @@ export async function typesBuild (ctx: BuildContext) {
}
})

const distDir = join(entry.outDir, entry.name)
const srcConfig = _require(resolve(ctx.rootDir, entry.input))
const genDir = resolve(ctx.rootDir, ctx.genDir, entry.name)

const defaults = entry.defaults || {}
const schema = resolveSchema(srcConfig, defaults)

await mkdirp(genDir)
await writeFile(resolve(genDir, `${entry.name}.md`), generateMarkdown(schema))
await writeFile(resolve(genDir, `${entry.name}.schema.json`), JSON.stringify(schema, null, 2))
await writeFile(resolve(genDir, `${entry.name}.defaults.json`), JSON.stringify(defaults, null, 2))
await writeFile(resolve(distDir, `${entry.name}.md`), generateMarkdown(schema))
await writeFile(resolve(distDir, `${entry.name}.schema.json`), JSON.stringify(schema, null, 2))
await writeFile(resolve(distDir, `${entry.name}.defaults.json`), JSON.stringify(defaults, null, 2))
if (entry.declaration) {
await writeFile(resolve(genDir, `${entry.name}.d.ts`), 'export ' + generateTypes(schema, pascalCase(entry.name + '-schema')))
await writeFile(resolve(distDir, `${entry.name}.d.ts`), 'export ' + generateTypes(schema, pascalCase(entry.name + '-schema')))
}
}
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface BuildEntry {
format?: 'esm' | 'cjs'
defaults?: Record<string, any>
declaration?: boolean
outDir?: string
}

export interface BuildOptions {
Expand All @@ -14,7 +15,6 @@ export interface BuildOptions {
entries: BuildEntry[],
clean: boolean
outDir: string
genDir: string
stub: boolean
dependencies: string[],
devDependencies: string[]
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fsp from 'fs/promises'
import { promisify } from 'util'
import { dirname } from 'upath'
import mkdirp from 'mkdirp'
import rimraf from 'rimraf'

export async function ensuredir (path: string) {
await mkdirp(dirname(path))
Expand All @@ -22,3 +24,9 @@ export function getpkg (id: string = '') {
const s = id.split('/')
return s[0][0] === '@' ? `${s[0]}/${s[1]}` : s[0]
}

export async function cleanDir (dir: string) {
await fsp.unlink(dir).catch(() => { })
await promisify(rimraf)(dir)
await mkdirp(dir)
}

0 comments on commit 5bb7ac3

Please sign in to comment.