esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

settings / plugins for wrapped (lazyload) required in nodejs

Open xpr0gamers opened this issue 1 year ago • 1 comments

Hello everyone,

is there a way via settings or a custom plugin, to handle wrapped requires? I use a npm package who have this coding: image

The implemantation of the function "lazyload" looks like this: image

Thanks.

xpr0gamers avatar Jan 15 '24 08:01 xpr0gamers

One possible way is to rewrite these lazyLoad('path') to lazyLoad(() => require('path')). The later form can be statically analyzed by esbuild to make it able to bundle the file pointed by 'path'. You can do the rewrite using plugins.

On the other hand, many Node.js packages are not written in a bundler-friendly way where they may use non-analyzable imports and other code patterns. In which case I would just recommend you to externalize them using --packages=external.

hyrious avatar Jan 15 '24 09:01 hyrious

Closing this question as it has been answered. That's a great answer. Either of those approaches are good ways to solve the problem.

evanw avatar Jun 26 '24 17:06 evanw