Skip to content

Commit

Permalink
improved and exposed the tsup config
Browse files Browse the repository at this point in the history
  • Loading branch information
MP281X committed Aug 22, 2024
1 parent f4e376a commit 5be746f
Show file tree
Hide file tree
Showing 8 changed files with 1,355 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
uses: actions/setup-node@v4
with: { node-version: 'latest', registry-url: 'https://registry.npmjs.org' }

- name: install packages
run: pnpm install
- name: install / build
run: pnpm install && pnpm run build

- name: check
run: pnpm run check
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
**/node_modules
**/pnpm-lock.yaml

**/dist
**/*.tsbuildinfo
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To configure the package, use the following `package.json` or merge it with your

```jsonc
{
"name": "projectName",
"name": "projectName",
"type": "module",

"scripts": {
Expand Down
50 changes: 50 additions & 0 deletions configs/tsup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import fs from 'node:fs'

import { defineConfig } from 'tsup'
import type { Options } from 'tsup'

type EsbuildPluginHandler = NonNullable<Options['esbuildPlugins']>[number]['setup']
export const changeShebang: EsbuildPluginHandler = build =>
build.onEnd(result => {
const textDecoder = new TextDecoder()
const outputFiles = result.outputFiles ?? []

const getFirstLine = (data: Uint8Array) => {
const newLineIndex = data.indexOf(0x0a)
if (newLineIndex === -1 || newLineIndex > 200) return ''

return textDecoder.decode(data.slice(0, newLineIndex))
}

for (const file of outputFiles) {
if (file.contents.length === 0) continue

const firstLine = getFirstLine(file.contents)
if (firstLine.startsWith('#!/usr/bin/env -S node') === false) continue

const fileText = textDecoder.decode(file.contents)
const fileWithoutShebang = fileText.slice(firstLine.length + 1, fileText.length)

const shebang = '#!/usr/bin/env node'
const fileWithShebang = shebang + '\n' + fileWithoutShebang

fs.writeFileSync(file.path, fileWithShebang)
}
})

export const tsupConfig = (entryPoints: string[]) =>
defineConfig({
entryPoints,
format: ['esm'],
esbuildPlugins: [{ name: 'changeShebang', setup: changeShebang }],

dts: true,
shims: true,
treeshake: true,
splitting: false,
minify: true,

clean: true,
silent: true,
onSuccess: async () => console.log('\x1b[36m%s\x1b[0m', '⚡ Build success')
})
1 change: 1 addition & 0 deletions dist/cli/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/usr/bin/env -S node --experimental-strip-types
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@
"type": "module",
"name": "@mp281x/shared-config",
"scripts": {
"dev": "tsup-node",
"fix": "./cli/index.ts fix",
"check": "./cli/index.ts check"
"check": "./cli/index.ts check",
"build": "tsup-node"
},

"bin": {
"x": "./cli/index.ts",
"@mp281x/shared-config": "./cli/index.ts"
"x": "./dist/cli/index.js",
"@mp281x/shared-config": "./dist/cli/index.js"
},

"exports": {
"./biome": "./configs/biome.jsonc",
"./tsconfig": "./configs/tsconfig.json"
"./tsconfig": "./configs/tsconfig.json",

"./tsup": {
"types": "./dist/configs/tsup.d.ts",
"import": "./dist/configs/tsup.ts"
}
},

"files": ["./cli/**/*", "./configs/*"],
"files": ["./dist/**/*", "./configs/*"],

"dependencies": {
"turbo": "latest",
"@biomejs/biome": "latest",
"@biomejs/biome": "latest"
},

"devDependencies": {
"tsup": "^8",

"typescript": "^5",
"@types/node": "^20"
}
Expand Down
Loading

0 comments on commit 5be746f

Please sign in to comment.