consolidate.js icon indicating copy to clipboard operation
consolidate.js copied to clipboard

Imports choking esbuild --bundle

Open cfv1984 opened this issue 4 years ago • 6 comments

Hi! For a toy project of mine I used koa-views, which uses consolidate, and noticed that the way consolidate does requires chokes up esbuild --bundle, causing me to have to manually mark every single template engine I'm not using as external, which in esbuild means it won't care for them.

This however is quite a chore and if there's ever a change in the supported engines will mean I'll have to update this list by hand which is ... not ideal.

One way I thought could potentially work -and will likely test for viability shortly- is abstracting the require calls such that the bundling tools no longer get confused about all those conditional calls?

I created https://github.com/evanw/esbuild/issues/2033 over at esbuild so someone can at some point have a shot at solving it, but also wanted to let y'all know this is a thing that happens.

Cheers!

cfv1984 avatar Feb 18 '22 14:02 cfv1984

An easy way to avoid this problem with esbuild is to move each conditional require() call inside the try/catch statement:

 exports.velocityjs.render = function(str, options, cb) {
   return promisify(cb, function(cb) {
-    var engine = requires.velocityjs || (requires.velocityjs = require('velocityjs'));
     try {
+      var engine = requires.velocityjs || (requires.velocityjs = require('velocityjs'));
       options.locals = options;
       cb(null, engine.render(str, options).trimLeft());
     } catch (err) {
       cb(err);
     }
   });
 };

This signals to esbuild that the require() call is expected to be able to fail and the failure should be handled at run-time instead of at compile-time.

evanw avatar Feb 18 '22 19:02 evanw

PR welcome

niftylettuce avatar Mar 14 '22 23:03 niftylettuce

@niftylettuce please i made the PR ? Thanks

jacargentina avatar Jun 07 '22 03:06 jacargentina

@niftylettuce any chance to merge my fix?

jacargentina avatar Jan 23 '23 21:01 jacargentina

@jacargentina we are merging your fix into our new repo 🙏

We have forked this repository for maintenance and released it under @ladjs/consolidate, see https://github.com/ladjs/consolidate.js. We have merged PR's and updated it for email-templates. Please click the "Watch" button to get notified of all releases at https://github.com/ladjs/consolidate.js. Thank you 🙏

Screen Shot 2023-06-08 at 3 05 12 PM

PR welcome at the new repo once new release is published today!

titanism avatar Jun 08 '23 20:06 titanism

v1.0.0 of consolidate has been released (and mirrored to @ladjs/consolidate) 🎉

watch/follow https://github.com/ladjs/consolidate for updates and to submit future issues and PR's

titanism avatar Jun 09 '23 00:06 titanism