Imports choking esbuild --bundle
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!
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.
PR welcome
@niftylettuce please i made the PR ? Thanks
@niftylettuce any chance to merge my fix?
@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 🙏
PR welcome at the new repo once new release is published today!
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