Giter VIP home page Giter VIP logo

Comments (6)

evanw avatar evanw commented on April 28, 2024 6

Yes, I would like to do this. I will likely add a context parameter so you can configure whether your build is targeting the browser or node, and modify the behavior of this feature depending on context.

from esbuild.

evanw avatar evanw commented on April 28, 2024 5

I just added some basic support for this via --platform=node. The path passed to require() must still be a string, but if it matches one of node's builtin modules it will be passed through to node's require().

from esbuild.

evanw avatar evanw commented on April 28, 2024

I'm intending esbuild to be a bundler for the web, not for node. Here's my answer for a similar question on issue #5 (comment):

Good question.

The output of esbuild's --bundle option is intended to be used for running in the browser. Not only does it require all of your source code to be in files on disk, but it also uses substitutions in the browser field in package.json files to bundle browser-specific versions of your npm libraries. So it won't work with any npm libraries that use standard node modules.

In my experience, projects that run in node keep the individual source files separate on disk and use require() calls to load the files at run-time instead of bundling them all into a single file at build time. What's your use case for bundling your node project into a single file? Or are you still targeting the browser instead of node and just using some npm libraries that aren't browser-friendly?

I'm curious what your use case is for using a bundler with a node app.

from esbuild.

billymcintosh avatar billymcintosh commented on April 28, 2024

Compatibility and portability are the main use cases. Tree shaking is a bonus.

Apologies for not seeing that. I read a few issues but missed that.

Thanks for the quick response!

from esbuild.

tmikaeld avatar tmikaeld commented on April 28, 2024

Unfortunately, many packages include node packages but don't use them (Node & Browser compatible).

Which is why other bundler like webpack show a warning instead of an outright error.

Could this be made so that it shows an error and returns the missing node modules as undefined?

from esbuild.

jpadilla avatar jpadilla commented on April 28, 2024

Another interesting pattern to possibly support is require inside a try/catch like this one:

https://github.com/visionmedia/debug/blob/db306db99e7822d355724698990d335927563210/src/node.js#L25-L112

from esbuild.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.