tj / consolidate.js

Template engine consolidation library for node.js
3.48k stars 357 forks source link

feat!: Added support for ECMAScript Modules (ESM) #338

Closed yanneves closed 2 years ago

yanneves commented 3 years ago

BREAKING CHANGE: Requires Node version >= 12

This library's coding causes issues with tools like esbuild and vite due to the way it relies on dynamic require() statements. I'm proposing a shift to dynamic imports to introduce support for ES Modules.

The bulk of the changes move imports like this:

var engine = requires.pug || (requires.pug = require('pug'))
// ...

to this:

use('pug').then(engine => {
  // ...
})

Where use() is a utility function that handles import, caching, and additional options, and returns a promise.

Besides bundling a shim for Promise.any(), this runs native. We could consider including more polyfills or building with Babel to support older Node versions. This supersedes #294.

yanneves commented 3 years ago

Noticed build failures are due to npm@^7 compatibility issues in the dev dependency tree - I'll try and take closer look 👌

niftylettuce commented 2 years ago

If you want to be a maintainer and use an ESM version with major semver bump, I am open to adding you.

Submit a new PR and let me know