Comments (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.
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.
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 thebrowser
field inpackage.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.
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.
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.
Another interesting pattern to possibly support is require
inside a try/catch like this one:
from esbuild.
Related Issues (20)
- ERROR: Expected "*/" to terminate multi-line comment HOT 2
- Expected ";" but found "const" - typescript transformer HOT 2
- Option to disable injected comment of input path file HOT 2
- Additional directories to watch HOT 3
- esbuild does not fail with undefined functions HOT 5
- Segmentation fault when using with vite and sveltekit project HOT 1
- Glob imports must match exactly
- Pass argument to IIFE HOT 2
- Esbuild causing bundle breakage when applied twice to the same bundle
- Splitting results in `Dynamic require of "<filename>" is not supported` when targeting `chrome62`
- [Feature Request] Add built-in HTML loader HOT 1
- Imports from stdio entrypoint don't have a namespace set
- DCE is not removed when value is from a const in another module.
- Get metafile info from a `watch()` rebuild HOT 2
- removes the backslash "\" when minify option is false HOT 4
- [Sourcemap] Private properties don't get sourcemapped correctly HOT 3
- ENV Secrets accessible from js code in a React + Rails APP. HOT 7
- [Feature] Ability to save charts from Bundle Size Analyzer as svg files
- TypeError: Cannot set property X of #<Object> which has only a getter HOT 1
- Line limit option (lineLimit) breaks code HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esbuild.