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
Conditional Import depending on Environment #1829
Comments
|
@hyrious Hi, I'm actually using the esbuild api directly as part of a build process. I think the issue may be that; while esbuild does exclude the
This error is thrown before any of my code is run, suggesting it is part of the esbuild module resolver infrastructure. Ideally, id like to conditionally resolve Maybe there is a better way to handle this? |
It shouldn't happen. Does your |
@hyrious Hi, false alarm 🤦♂️ Found the issue, but didn't turn out the be the The package im currently trying to build includes both import { WebSocketServer } from 'ws' // nope
export class Service {
...
public listen(port) {
const wss = new WebSocketServer({ port })
}
} Which I've temporarily resolved with... import type { WebSocketServer } from 'ws' // fix
export class Service {
...
public listen(port) {
const WsWebSocketServer = require('ws').WebSocketServer as typeof WebSocketServer
const wss = new WsWebSocketServer({ port })
}
} I'm not sure I want the server implementation included in the client code, but this workaround does solve the issue in the interim. Looks like esbuild is working as expected here, so will close off this issue. Thanks for your help! |
Hi, I'm just looking for bit of guidance on how to conditionally import node npm packages based on the JavaScript environment (either Node or the Browser). The package I'm trying to conditionally import is the
ws
WebSocket package.When running as
--platform node
, things are fine. However when running--platform browser
with--external ws
specified, esbuild still seems to try resolve thews
package (albeit externally), even if therequire('ws')
call is wrapped in conditional block.Is there a way to have esbuild completely ignore the
ws
package when running--external ws
? Any info on how to approach this in esbuild would be great.The text was updated successfully, but these errors were encountered: