egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
8.99k stars 217 forks source link

Mixing shebang and an esbuild plugin causes an error #684

Open segevfiner opened 2 years ago

segevfiner commented 2 years ago

Steps to reproduce:

  1. Clone https://github.com/segevfiner/tsup-fileloc
  2. Run npm install & npm build, it will error with:

    
    ✘ [ERROR] Syntax error "!"
    
    src/index.ts:10:1:
      10 │ #!/usr/bin/env node
         ╵  ^

CJS Build failed Error: Build failed with 1 error: src/index.ts:10:1: ERROR: Syntax error "!" at failureErrorWithLog (/prj/tsup-fileloc/node_modules/esbuild/lib/main.js:1621:15) at /prj/tsup-fileloc/node_modules/esbuild/lib/main.js:1263:28 at runOnEndCallbacks (/prj/tsup-fileloc/node_modules/esbuild/lib/main.js:1043:63) at buildResponseToResult (/prj/tsup-fileloc/node_modules/esbuild/lib/main.js:1261:7) at /prj/tsup-fileloc/node_modules/esbuild/lib/main.js:1374:14 at /prj/tsup-fileloc/node_modules/esbuild/lib/main.js:675:9 at handleIncomingPacket (/prj/tsup-fileloc/node_modules/esbuild/lib/main.js:772:9) at Socket.readFromStdout (/prj/tsup-fileloc/node_modules/esbuild/lib/main.js:641:7) at Socket.emit (node:events:527:28) at addChunk (node:internal/streams/readable:315:12)



If you either remove/comment out the shebang, or the esbuild plugin, it will work.

<!-- POLAR PLEDGE BADGE START -->
## Upvote & Fund

- We're using [Polar.sh](https://polar.sh/egoist) so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.

<a href="https://polar.sh/egoist/tsup/issues/684">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/egoist/tsup/issues/684/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/egoist/tsup/issues/684/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->
Ge-yuan-jun commented 2 years ago

same question

Ge-yuan-jun commented 2 years ago

I have tried banner option, it doesn't work. So I hacked it with some code

const fs = require('fs')
const path = require('path')

const build = path.join(__dirname, '../dist', 'index.cjs.js')
const unpatchedBuild = fs.readFileSync(build, 'utf-8')
const patchedBuild = unpatchedBuild.replace(
  "'use strict';",
  "#!/usr/bin/env node\n\n'use strict;'\n"
)
fs.writeFileSync(build, patchedBuild, 'utf-8')
AntoineKM commented 1 year ago

I don't know if this is possibly related: https://github.com/egoist/tsup/issues/910