Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent TypeScript builds #247

Closed
shcca opened this issue Jul 10, 2020 · 4 comments
Closed

Inconsistent TypeScript builds #247

shcca opened this issue Jul 10, 2020 · 4 comments

Comments

@shcca
Copy link

shcca commented Jul 10, 2020

I'm trying to build a Koa.js server. TypeScript works, but esbuild throws an error.

https://github.com/shcca/esbuild-koa

npm init

When using TypeScript directly:

  1. npx tsc server.ts --outDir tsc-test --esModuleInterop
  2. node tsc-test/server.js -> localhost:3000 -> Hello World

When using esbuild:

  1. npx esbuild --bundle server.ts --outdir=esbuild-test --platform=node
node_modules/debug/src/node.js:29:30: error: Could not resolve "supports-color"
var supportsColor = require('supports-color');
                              ~~~~~~~~~~~~~~~~
node_modules/any-promise/register.js:24:14: warning: This call to "require" will not be bundled because the argument is not a string literal
    var lib = require(implementation)
              ~~~~~~~
1 warning and 1 error

I can get esbuild to work by running npm i supports-color. Although the 1 warning remains.

It's possible I'm missing something (still learning), so apologies if that's the case. Not sure what's happening.

@evanw
Copy link
Owner

evanw commented Jul 10, 2020

Ah, I see. The call to require() is inside a try/catch statement. I should silence this error in that case and automatically turn this into an external import since the code is almost definitely trying to conditionally require something.

@evanw evanw closed this as completed in 7f3f6db Jul 10, 2020
@evanw
Copy link
Owner

evanw commented Jul 10, 2020

The error will be fixed in the next release. However, you can work around the issue for now by passing --external:supports-color to esbuild.

@evanw
Copy link
Owner

evanw commented Jul 11, 2020

This should now be fixed as of version 0.6.0.

@wcastand
Copy link

Hi, using version : "esbuild": "^0.8.23" and still get the error with "koa": "^2.13.0"
the code i try to build:

import Koa from 'koa'
import cors from '@koa/cors'
import bodyparser from 'koa-bodyparser'

const app = new Koa()
app.use(cors())
app.use(bodyparser())

app.use(async (ctx) => {
  ctx.body = 'Hello World'
})

app.listen(8080)

This is the output on the terminal:

yarn run v1.22.5
$ ./node_modules/.bin/esbuild src/index.ts --bundle  --platform=node --outfile=dist/index.js
 > node_modules/any-promise/register.js: warning: This call to "require" will not be bundled because the argument is not a string literal (surround with a try/catch to silence this warning)
    24 │     var lib = require(implementation)
       ╵               ~~~~~~~

1 warning
✨  Done in 0.17s.

If i add --external:koa, it seems to be still a workaround but it means it's not bundled right and will look inside the node_modules?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants