Skip to content

Commit

Permalink
feat: declaration option to generate types
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 9, 2021
1 parent 949b44c commit 936478a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
3 changes: 2 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default <BuildConfig> {
'src/cli'
],
dependencies: [
'esbuild'
'esbuild',
'typescript'
]
}
4 changes: 4 additions & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function build (rootDir: string, stub: boolean) {
outDir: 'dist',
genDir: '.gen',
untyped: undefined,
declaration: undefined,
clean: true,
stub,
buildEntries: [],
Expand All @@ -57,6 +58,9 @@ export async function build (rootDir: string, stub: boolean) {
if (!entry.builder) {
entry.builder = entry.input.endsWith('/') ? 'mkdist' : 'rollup'
}
if (ctx.declaration !== undefined && entry.declaration === undefined) {
entry.declaration = ctx.declaration
}
}

// Add dependencies from package.json as externals
Expand Down
2 changes: 1 addition & 1 deletion src/builder/mkdist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function mkdistBuild (ctx: BuildContext) {
distDir: join(ctx.outDir, entry.name),
format: entry.format,
cleanDist: false,
declaration: true
declaration: entry.declaration
})
ctx.buildEntries.push({
path: join(ctx.outDir, entry.name),
Expand Down
25 changes: 14 additions & 11 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ export async function rollupBuild (ctx: BuildContext) {
}

// Types
rollupOptions.plugins = rollupOptions.plugins || []
rollupOptions.plugins.push(dts({
respectExternal: true
}))
const typesBuild = await rollup(rollupOptions)
await typesBuild.write({
dir: resolve(ctx.rootDir, ctx.outDir),
format: 'esm'
})
if (ctx.declaration) {
rollupOptions.plugins = rollupOptions.plugins || []
rollupOptions.plugins.push(dts({
respectExternal: true
}))
const typesBuild = await rollup(rollupOptions)
await typesBuild.write({
dir: resolve(ctx.rootDir, ctx.outDir),
format: 'esm'
})
}
}

export function getRollupOptions (ctx: BuildContext): RollupOptions {
Expand Down Expand Up @@ -103,8 +105,9 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
},

plugins: [
// TODO
alias({}),
alias({
[ctx.pkg.name]: ctx.rootDir
}),

nodeResolve({
extensions
Expand Down
4 changes: 3 additions & 1 deletion src/builder/untyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export async function typesBuild (ctx: BuildContext) {
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(genDir, `${entry.name}.d.ts`), 'export ' + generateTypes(schema, pascalCase(entry.name + '-schema')))
if (entry.declaration) {
await writeFile(resolve(genDir, `${entry.name}.d.ts`), 'export ' + generateTypes(schema, pascalCase(entry.name + '-schema')))
}
}
}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ export interface BuildEntry {
builder?: 'rollup' | 'mkdist' | 'untyped'
format?: 'esm' | 'cjs'
defaults?: Record<string, any>
declaration?: boolean
}

export interface BuildOptions {
pkg: any,
rootDir: string
declaration?: boolean
entries: BuildEntry[],
clean: boolean
outDir: string
Expand Down

0 comments on commit 936478a

Please sign in to comment.