Skip to content

Commit

Permalink
feat: use dtsx
Browse files Browse the repository at this point in the history
instead of oxc

chore: wip
  • Loading branch information
chrisbbreuer committed Oct 19, 2024
1 parent 2f01319 commit c12883f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 140 deletions.
Binary file removed .github/art/cover.jpg
Binary file not shown.
Binary file added .github/art/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed .github/art/github_social_preview.png
Binary file not shown.
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# bun-plugin-dts-auto
![Social Card of Bun Plugin DTS Auto](https://github.com/stacksjs/bun-plugin-dtsx/blob/main/.github/art/cover.jpg)

![Social Card of Bun Plugin DTS Auto](https://github.com/stacksjs/bun-plugin-dts-auto/blob/main/.github/art/cover.jpg)
[![npm version][npm-version-src]][npm-version-href]
[![GitHub Actions][github-actions-src]][github-actions-href]
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
Expand All @@ -12,21 +11,21 @@ This Bun plugin generates dts files for your TypeScript projects.
## Features

- Automatic & fast dts generation
- Powered by oxc-transform & isolatedDeclarations
- Powered by Bun & isolatedDeclarations
- Monorepo support

## Usage

```bash
bun install -d bun-plugin-dts-auto
bun install -d bun-plugin-dtsx
```

You may now use the plugin:

```ts
import dts from 'bun-plugin-dts-auto'
import dts from 'bun-plugin-dtsx'
// if you prefer named imports
// import { dts } from 'bun-plugin-dts-auto'
// import { dts } from 'bun-plugin-dtsx'

await Bun.build({
root: './src',
Expand All @@ -36,10 +35,11 @@ await Bun.build({
outdir: './dist',
plugins: [
dts({
cwd: process.cwd(), // optional
root: './src', // optional, default: 'src'
cwd: './', // optional, default: process.cwd()
root: './src', // optional, default: './src'
outdir: './dist/types', // optional, default: './dist'
files: ['src/index.ts'], // optional, if not specified, all .ts files in root will be processed
keepComments: true, // optional, default: true
tsconfigPath: './tsconfig.json', // optional, default: './tsconfig.json'
}),
],
})
Expand All @@ -54,7 +54,9 @@ The `dts` plugin accepts an options object with the following properties:
- `cwd`: The current working directory _(optional, default: `process.cwd()`)_
- `root`: The root directory of your TypeScript files _(optional, default: `'src'`)_
- `outdir`: The output directory for generated declaration files _(optional, default: `'./dist'`)_
- `files`: An array of file paths or a single file path to process _(optional, if not specified, all `.ts` files in the root directory will be processed)_
- `entrypoints`: An array or glob of file paths, or a single file path, to process _(optional, if not specified, defaults to build entrypoints)_
- `keepComments`: Whether to keep comments in the generated dts files _(optional, default: `true`)_
- `tsconfigPath`: The path to your tsconfig file _(optional, default: `'./tsconfig.json'`)_

## Testing

Expand All @@ -64,7 +66,7 @@ bun test

## Changelog

Please see our [releases](https://github.com/stacksjs/bun-plugin-dts-auto/releases) page for more information on what has changed recently.
Please see our [releases](https://github.com/stacksjs/bun-plugin-dtsx/releases) page for more information on what has changed recently.

## Contributing

Expand Down Expand Up @@ -103,17 +105,17 @@ Many thanks to the following core technologies & people who have contributed to

## License

The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/bun-plugin-dts-auto/tree/main/LICENSE.md) for more information.
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/bun-plugin-dtsx/tree/main/LICENSE.md) for more information.

Made with 💙

<!-- Badges -->
[npm-version-src]: <https://img.shields.io/npm/v/bun-plugin-dts-auto?style=flat-square>
[npm-version-href]: <https://npmjs.com/package/bun-plugin-dts-auto>
[npm-downloads-src]: <https://img.shields.io/npm/dm/bun-plugin-dts-auto?style=flat-square>
[npm-downloads-href]: <https://npmjs.com/package/bun-plugin-dts-auto>
[github-actions-src]: <https://img.shields.io/github/actions/workflow/status/stacksjs/bun-plugin-dts-auto/ci.yml?style=flat-square&branch=main>
[github-actions-href]: <https://github.com/stacksjs/bun-plugin-dts-auto/actions?query=workflow%3Aci>

<!-- [codecov-src]: https://img.shields.io/codecov/c/gh/stacksjs/bun-plugin-dts-auto/main?style=flat-square
[codecov-href]: https://codecov.io/gh/stacksjs/bun-plugin-dts-auto -->
[npm-version-src]: <https://img.shields.io/npm/v/bun-plugin-dtsx?style=flat-square>
[npm-version-href]: <https://npmjs.com/package/bun-plugin-dtsx>
[npm-downloads-src]: <https://img.shields.io/npm/dm/bun-plugin-dtsx?style=flat-square>
[npm-downloads-href]: <https://npmjs.com/package/bun-plugin-dtsx>
[github-actions-src]: <https://img.shields.io/github/actions/workflow/status/stacksjs/bun-plugin-dtsx/ci.yml?style=flat-square&branch=main>
[github-actions-href]: <https://github.com/stacksjs/bun-plugin-dtsx/actions?query=workflow%3Aci>

<!-- [codecov-src]: https://img.shields.io/codecov/c/gh/stacksjs/bun-plugin-dtsx/main?style=flat-square
[codecov-href]: https://codecov.io/gh/stacksjs/bun-plugin-dtsx -->
40 changes: 0 additions & 40 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { unlink } from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import dts from './src/index'
// import dts from './dist/index'

console.log('Building...')

Expand All @@ -25,41 +22,4 @@ if (!result.success) {
process.exit(1)
}

// need to replace requireNative() function with the one "unbuilt" one from src/stubs
// because the bundler will only bundle the bindings based on machine that's running the build

// Copy requireNative() function from stubs.js to dist/index.js
const stubsPath = path.join(process.cwd(), 'src', 'stubs.txt')
const distIndexPath = path.join(process.cwd(), 'dist', 'index.js')

const stubsContent = await Bun.file(stubsPath).text()
let distIndexContent = await Bun.file(distIndexPath).text()

// Extract requireNative function from stubs.js
const requireNativeRegex = /function requireNative\(\) {[\s\S]*?^}/m
const stubsRequireNative = stubsContent.match(requireNativeRegex)?.[0]

// Replace requireNative function in dist/index.js
const distRequireNativeRegex = /function requireNative\(\) {[\s\S]*?^}/m
distIndexContent = distIndexContent.replace(distRequireNativeRegex, stubsRequireNative as string)

// Remove transform-related code
const transformRegex = /\/\/ node_modules\/@oxc-transform\/binding-[\s\S]*?^}\);/gm
distIndexContent = distIndexContent.replace(transformRegex, '')

// Remove __commonJS variable
const commonJsRegex = /var __commonJS =[\s\S]*?;\s*$/m
distIndexContent = distIndexContent.replace(commonJsRegex, '')

// Write the modified content back to dist/index.js
await Bun.write(distIndexPath, distIndexContent)
console.log('Updated requireNative() function in dist/index.js')

// delete the transform.* files in dist
const transformFiles = new Bun.Glob('dist/transform.*')
for await (const file of transformFiles.scan()) {
await unlink(file)
}

// eslint-disable-next-line no-console
console.log('Build complete')
30 changes: 10 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "bun-plugin-dts-auto",
"name": "bun-plugin-dtsx",
"type": "module",
"version": "0.20.6",
"description": "A Bun Bundler plugin that auto generates your d.ts types.",
"description": "A Bun Bundler plugin that auto generates your d.ts types extremely fast.",
"author": "Chris Breuer <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/stacksjs/bun-plugin-dts-auto#readme",
Expand All @@ -15,12 +15,13 @@
},
"keywords": [
"dts",
"oxc",
"dtsx",
"emit",
"generation",
"typescript",
"types",
"auto",
"stacks",
"bun",
"plugin",
"package"
Expand All @@ -36,11 +37,11 @@
},
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": ["dist", "README.md", "LICENSE.md"],
"files": ["LICENSE.md", "README.md", "dist"],
"scripts": {
"build": "bun build.ts",
"lint": "biome check .",
"lint:fix": "biome check --fix .",
"lint": "bunx eslint .",
"lint:fix": "bunx eslint . --fix",
"fresh": "bunx rimraf node_modules/ bun.lock && bun i",
"commit": "git cz",
"changelog": "bunx changelogen --output CHANGELOG.md",
Expand All @@ -50,12 +51,11 @@
"typecheck": "bun tsc --noEmit"
},
"dependencies": {
"oxc-transform": "^0.30.5"
"@stacksjs/dtsx": "^0.2.2"
},
"devDependencies": {
"@biomejs/biome": "^1.9.3",
"@commitlint/cli": "^19.5.0",
"@stacksjs/biome-config": "^0.1.5",
"@stacksjs/eslint-config": "^3.8.1-beta.2",
"@types/bun": "^1.1.10",
"bumpp": "^9.6.1",
"changelogen": "^0.5.7",
Expand All @@ -65,22 +65,12 @@
"simple-git-hooks": "^2.11.1",
"typescript": "^5.6.2"
},
"optionalDependencies": {
"@oxc-transform/binding-win32-x64-msvc": "0.30.5",
"@oxc-transform/binding-win32-arm64-msvc": "0.30.5",
"@oxc-transform/binding-linux-x64-gnu": "0.30.5",
"@oxc-transform/binding-linux-arm64-gnu": "0.30.5",
"@oxc-transform/binding-linux-x64-musl": "0.30.5",
"@oxc-transform/binding-linux-arm64-musl": "0.30.5",
"@oxc-transform/binding-darwin-x64": "0.30.5",
"@oxc-transform/binding-darwin-arm64": "0.30.5"
},
"simple-git-hooks": {
"pre-commit": "bun lint-staged",
"commit-msg": "bunx --no -- commitlint --edit $1"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,vue}": "biome check --fix ."
"*.{js,jsx,ts,tsx}": "bunx eslint . --fix"
},
"config": {
"commitizen": {
Expand Down
69 changes: 17 additions & 52 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,35 @@
import fs from 'node:fs'
import path from 'node:path'
import type { DtsGenerationOption } from '@stacksjs/dtsx'
import type { BunPlugin } from 'bun'
import { isolatedDeclaration } from 'oxc-transform'
import type { DtsOptions } from './types'
import process from 'node:process'
import { generate } from '@stacksjs/dtsx'

export async function generate(options?: DtsOptions): Promise<void> {
const cwd = options?.cwd ?? process.cwd()
const root = options?.root ?? 'src'
const outdir = options?.outdir ?? './dist/'
const files = options?.files

const rootDir = path.join(cwd, root)

if (files) {
const f = Array.isArray(files) ? files : [files]
for (const file of f) {
await processFile(path.join(cwd, file), rootDir, cwd, outdir)
}
} else {
const glob = new Bun.Glob('**/*.ts')
for await (const file of glob.scan({ cwd: rootDir, absolute: true })) {
await processFile(file, rootDir, cwd, outdir)
}
}
}

async function processFile(file: string, rootDir: string, cwd: string, outdir: string) {
if (fs.existsSync(file)) {
const ts = fs.readFileSync(file, 'utf-8')
const dts = isolatedDeclaration(file, ts, { sourcemap: false })
const code = dts.code
const relativePath = path.relative(rootDir, file)
const outputPath = path.join(cwd, outdir, relativePath.replace(/\.ts$/, '.d.ts'))
write(outputPath, code)
} else {
console.warn(`File not found: ${file}`)
}
}

function write(file: string, content: string) {
const dir = path.dirname(file)
fs.mkdirSync(dir, { recursive: true })
fs.writeFileSync(file, content)
}

export function dts(options?: DtsOptions): BunPlugin {
export function dts(options?: DtsGenerationOption): BunPlugin {
return {
name: 'bun-plugin-dts-auto',
name: 'bun-plugin-dtsx',

async setup(build) {
const root = options?.root ?? build.config.root ?? './src/'
const outdir = options?.outdir ?? './dist/'
const cwd = options?.cwd ?? process.cwd()
const files = options?.files
const root = options?.root ?? build.config.root
const entrypoints = options?.entrypoints ?? build.config.entrypoints
const outdir = options?.outdir ?? build.config.outdir
const keepComments = options?.keepComments ?? true
const clean = options?.clean ?? false
const tsconfigPath = options?.tsconfigPath ?? './tsconfig.json'

await generate({
...options,
cwd,
root,
entrypoints,
outdir,
cwd,
files,
keepComments,
clean,
tsconfigPath,
})
},
}
}

export * from './types'
export type { DtsGenerationOption }

export default dts
7 changes: 0 additions & 7 deletions src/types.ts

This file was deleted.

0 comments on commit c12883f

Please sign in to comment.