Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for native ESM modules #2137

Open
Urigo opened this issue Mar 16, 2020 · 1 comment
Open

Add support for native ESM modules #2137

Urigo opened this issue Mar 16, 2020 · 1 comment

Comments

@Urigo
Copy link

@Urigo Urigo commented Mar 16, 2020

Hi and thank you for the great library and endless support!

It would be great to have this library export native named exports so we could import it natively on new Node versions.

Here is an example of how you would do it now, without that support: Urigo/typescript-node-es-modules-example@9830417#diff-f41e9d04a45c83f3b6f6e630f10117feR10-R11

@sebiniemann
Copy link

@sebiniemann sebiniemann commented May 19, 2020

This would be great 👍 Especially now that NodeJS also supports ESM.

Switching from require/module.exports to import/export and the generation of a CommonJS variant from it is certainly the most future-oriented solution, but seems to involve some adjustments, due to some (unconventional) module management.

I see many require('...') calls inside of functions, which are assumed to run synchronously. One could replace these with dynamic imports (in case the delayed import was really necessary -- like with process.env.NODE_PG_FORCE_NATIVE), but this will also introduce asynchronous behaviour, that needs to be addressed.

In some cases, this also introduced circular dependencies, like pg\lib\index.js -> pg-pool\index.js -> pg\lib\index.js.

A more complicated case is something like

if (...) {
// ...
} else {
  Object.defineProperty(module.exports, 'native', {
    // ...
    get() {
      Object.defineProperty(module.exports, 'native', {
        // ...
      })
      // ...
    },
  })
}

where the export definition is not only delayed, but also later overwritten.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.