-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved and exposed the tsup config
- Loading branch information
Showing
8 changed files
with
1,355 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
**/node_modules | ||
**/pnpm-lock.yaml | ||
|
||
**/dist | ||
**/*.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#!/usr/bin/env -S node --experimental-strip-types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.