Giter VIP home page Giter VIP logo

Comments (3)

evanw avatar evanw commented on April 28, 2024 9

the main.js also exported a function that i cannot use from Node.js it should've been exported.

The reason why this wasn't working before was because esbuild actually generated output in the IIFE (immediately-invoked function expression) format, not the CommonJS format that node uses, since I've been focusing on running code in the browser.

I added a new --format flag to control the output format and added support for the CommonJS output format. You can enable that with --format=cjs. However, if you want this to run in node you'll probably want to use --platform=node which automatically implies --format=cjs. Bundling with --platform=node enables referencing builtin node libraries and if you use any node modules that have both browser and node variants, switches esbuild over to bundling the node variants.

(Another comment: the bundle is pretty big compared to what rollup produces for the code i mentioned do we really need all that stuff?)

I assume you're talking about the extra module loading code esbuild sticks at the top. No, that stuff isn't necessarily needed. I'm trying to have esbuild support bundling both ES6 and CommonJS modules instead of something like Rollup, which only natively supports ES6 modules. That extra stuff is for CommonJS support.

I'm working towards a state where that extra stuff is stripped out when it's not needed. Believe me, it bugs me too. But I haven't yet done the additional work to detect when it's not needed and strip it away. I'll definitely get to this at some point.

(Mostly as a reminder for me later) I recently made some progress towards this state by rearranging how the bundle is structured so that helper code is now in multiple top-level functions instead of in a single multi-purpose function nested inside a closure. The next step is to detect which of those helper functions are unused and strip the unused ones away, which will only leave the ones that are actually needed, if any. I also need to detect if all code reachable from the entry point is valid ES6 and, if so, skip embedding the code inside a CommonJS module map.

from esbuild.

ravener avatar ravener commented on April 28, 2024 1

@evanw any ideas about this? I also can't use react with multiple files it generates broken code for the importing logic.

from esbuild.

amatiasq-heydoc avatar amatiasq-heydoc commented on April 28, 2024 1

I tried --platform=node and didn't work but explicit --format=cjs did the job, thanks!

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.