Dec
7
26 minutes ago
Activity icon
issue

sinclairzx81 issue evanw/esbuild

sinclairzx81
sinclairzx81

Conditional Import depending on Environment

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 the ws package (albeit externally), even if the require('ws') call is wrapped in conditional block.

const IsomorphicWebSocket: typeof WebSocket = isBrowser() ? WebSocket : require('ws').WebSocket
//
//                       this should not execute in browser environments ^ 
       

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.

Dec
6
1 day ago
started
started time in 31 minutes ago
started
started time in 1 hour ago
started
started time in 2 hours ago
Activity icon
issue

gajus issue comment evanw/esbuild

gajus
gajus

tsconfig `paths` are not transformed if not bundling

$ cat tsconfig.json 
{
  "compilerOptions": {
    # ...
    "paths": {
      "lib/*": ["lib/*"]
    }
  },
}

$ echo "import logger from 'lib/logger'" | esbuild --format=cjs
# ...
const logger = __toModule(require("lib/logger"));

^ the import/require is left as-is (as opposed to being transformed to ./lib/logger), so will fail if run, say in Node.js

Was thinking about whether it's esbuild's responsibility to do the transform, and I feel it is: as a ts-to-js transformer, it should perform any transforms implied in tsconfig.json, including path transforms

gajus
gajus

Just create a simple loader, e.g.

const path = require('path');

/**
 * @see https://github.com/evanw/esbuild/issues/394
 */
module.exports = function (content) {
  const relativePath = path.relative(
    path.dirname(this.resourcePath),
    path.resolve(__dirname, '../src')
  );

  return content.replaceAll(
    `from "@/`,
    `from "${relativePath ? relativePath + '/' : './'}`
  );
};
Activity icon
issue

loynoir issue evanw/esbuild

loynoir
loynoir

[Feature] support `createRequire`

import { createRequire } from 'module'
const require = createRequire(import.meta.url)

/**
 * @type {typeof ReadableStream}
 */
let RS: typeof ReadableStream
if (typeof ReadableStream !== 'undefined') {
  RS = ReadableStream
} else {
  try {
    RS = require('node:stream/web').ReadableStream
  } catch {
    RS = require('web-streams-polyfill/dist/ponyfill.js').ReadableStream
  }
}

export default RS
$ esbuild --bundle XXX.mts
The package "module" wasn't found on the file system but is built into node. Are you trying to
bundle for node? You can use "--platform=node" to do that, which will remove this error.
$ esbuild src/index.mts --bundle --platform=node
... web-streams-polyfill/dist/ponyfill.js is not bundled ...
$ # remove `createRequire`, let `require` global
web-streams-polyfill/dist/ponyfill.js is bundled

But, reference to a global variable require within ESM, I think it's not in standard?

Activity icon
fork

lcsouzamenezes forked evanw/esbuild

⚡ An extremely fast JavaScript and CSS bundler and minifier
lcsouzamenezes MIT License Updated
fork time in 10 hours ago
started
started time in 10 hours ago
started
started time in 11 hours ago
started
started time in 11 hours ago
Previous