evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
37.99k stars 1.14k forks source link

Should injections come before the esbuild runtime code? TypeError: __getOwnPropDescs is not a function #2631

Open ComLock opened 1 year ago

ComLock commented 1 year ago

I'm getting:

TypeError: __getOwnPropDescs

Because the esbuild runtime code https://github.com/evanw/esbuild/blob/master/internal/runtime/runtime.go contains

var __getOwnPropDescs = Object.getOwnPropertyDescriptors;

and the java nashorn engine doesn't have Object.getOwnPropertyDescriptors.

I tried using --inject to add the a Polyfill for Object.getOwnPropertyDescriptors, but it turns out the injections are added after the esbuild runtime code.

I worked around this problem by using npm-build-tools to concat in the polyfill after using esbuild, but it's not "pretty" :)

This is somewhat related to: https://github.com/evanw/esbuild/issues/1892

Perhaps some smartness or ability to NOT inject the esbuild runtime code is the solution.

hyrious commented 1 year ago

What about using banner?

ComLock commented 1 year ago

Using banner to inject multiline polyfills, requires a very long command line, and doesn't look nice.

Maybe making an esbuild banner plugin that can inject files is a way to go.

--

Sidenote: In my setup I use esbuild to bundle, and then swc to transpile that bundle into commonjs.

At that point I guess it would come down to who's the fastest, and/or creates the smallest files.

hyrious commented 1 year ago

If esbuild made commonjs

It does, try --format=cjs. REPL

To keep the ESM semantic of all exports are immutable, esbuild would create a wrapper __export(...) to make it, which is not small at all. FYI tsup uses rollup to run on the esm output of esbuild to get the cjs bundle, which should be the smallest.

ComLock commented 1 year ago

I'm acutally using --format=cjs, but

Transforming destructuring to the configured target environment ... is not supported yet

ComLock commented 1 year ago

This plugin was promising, but it's seems it doesn't work in combination with bundle: true

https://github.com/sanyuan0704/esbuild-plugin-swc

ComLock commented 1 year ago

Transforming const to the configured target environment ("es5") is not supported yet REPL

ComLock commented 1 year ago

So the issue for me then isn't the format, but the target :) I've been confusing the two.