esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Feature Request: minify `function createStore(...[store, options])` -> `function createStore(store, options)`

Open cyan-2048 opened this issue 6 months ago • 3 comments

repl: https://esbuild.github.io/try/#dAAwLjI1LjUALS10YXJnZXQ9ZXM2IC0tbWluaWZ5PXRydWUAZnVuY3Rpb24gY3JlYXRlU3RvcmUoLi4uW3N0b3JlLCBvcHRpb25zXSkge30

function createStore(...[store, options]) and function createStore(store, options) are logically equivalent, I can't find additional info on the purpose why people do it like this:

function createStore(...args) {
    var [store, options] = args;
}

this could be a typescript workaround?

relevant code links: https://github.com/solidjs/solid/blob/3d3207dd3aeb84c2a38377cf9f3b895995c2d969/packages/solid/store/src/store.ts#L502 https://github.com/nodejs/readable-stream/blob/88df21041dc26c210fab3e074ab6bb681a604b8e/src/util/inspect.js#L14

cyan-2048 avatar Jun 02 '25 01:06 cyan-2048

function createStore(...[store, options]) and function createStore(store, options) are logically equivalent

Actually, it's slightly different.

function createStore(...[store, options]) {}
console.log(createStore.length) // 0
function createStore(store, options) {}
console.log(createStore.length) // 2

sapphi-red avatar Jun 02 '25 03:06 sapphi-red

Actually Solid.js did this for TypeScript workaround, the length is not checked.

I have no idea about why nodejs/readable-stream did this.

hyrious avatar Jun 02 '25 03:06 hyrious

I never really understood the .length checks on functions (I assume esbuild never cared about it either), this is really just an issue for me because ...[] isn't valid syntax for Firefox 48 (es6, no esm) Image

cyan-2048 avatar Jun 02 '25 05:06 cyan-2048

Thanks for mentioning the length property. That is actually something that esbuild cares about (in the absence of other concerns), as it's sometimes used by certain frameworks despite being a somewhat obscure feature.

However, the syntax not working correctly in Firefox is a separate concern from minification. Destructuring only works correctly in Firefox 53+, and esbuild doesn't yet support transforming destructuring for older target environments. The existing open issue for that work is #3743.

evanw avatar Jun 04 '25 19:06 evanw