Skip to content

fix(build options): wrong output #414

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

Closed
wants to merge 1 commit into from
Closed

Conversation

2234839
Copy link

@2234839 2234839 commented Mar 1, 2021

No description provided.

@alias-mac
Copy link
Contributor

Can you expand on what you are trying to do here?

When I use commonJS with:

const mongodbQueue = require('@openwar/mongodb-queue');

mongodbQueue is the function that I expose. I want to enforce that only this is exposed and nothing else will.
The reason why I don't want named exports is due to interoperability with other tools (like babel).
See more in the rollup issue thread.

If you can provide a reproducible issue using https://codesandbox.io/ would be great. Thanks!

@alias-mac alias-mac self-requested a review March 2, 2021 08:00
@alias-mac alias-mac added the bug Something isn't working label Mar 2, 2021
@2234839
Copy link
Author

2234839 commented Mar 2, 2021

Can you expand on what you are trying to do here?

When I use commonJS with:

const mongodbQueue = require('@openwar/mongodb-queue');

mongodbQueue is the function that I expose. I want to enforce that only this is exposed and nothing else will.
The reason why I don't want named exports is due to interoperability with other tools (like babel).
See more in the rollup issue thread.

If you can provide a reproducible issue using https://codesandbox.io/ would be great. Thanks!

I am trying to solve the problem shown below, according to my attempt to use named the compiled code will not become undefined here

图片

https://codesandbox.io/s/wonderful-kirch-hb4j3?file=/src/app.controller.ts

@alias-mac
Copy link
Contributor

alias-mac commented Mar 3, 2021

Thanks @2234839! This is a known issue due to nodejs imports vs es6 imports (with other tools).

When consuming it using vanilla nodejs (no other tools or transpilers) it works well as you saw, but becomes a problem when you ask tools typescript or tools that expect es6 syntax.

A quick fix for the code sample you provided would be to do:

import * as mongoDbQueue from '@openwar/mongodb-queue';

But a better fix is to enable esModuleInterop in your typescript config, like:

{
  "compilerOptions": {
    // other options you have already
    "esModuleInterop": true
  }
}

This is a known issue with other libraries too:
https://stackoverflow.com/questions/56238356/understanding-esmoduleinterop-in-tsconfig-file

I don't want to sacrifice the easy of use in vanilla nodejs by adding named exports because with this change normal nodejs users would have to write code like:

const mongoDbQueue = require('@openwar/mongodb-queue').default;

(notice the default at the end of require), which is quite ugly and not expected for regular consumers of libraries for nodejs.
The problem is really that typescript and they assume that in their docs:

By default (with esModuleInterop false or not set) TypeScript treats CommonJS/AMD/UMD modules similar to ES6 modules. In doing this, there are two parts in particular which turned out to be flawed assumptions:

This is what is causing the problem for you, but setting that flag to true it will work as expected.

Another option would be for you to consume the module as provided in the package.json:

"module": "dist/mongodb-queue.es.js",
"jsnext:main": "dist/mongodb-queue.es.js",

I know that not all tools are ready for that and it will depend on case by case. This library is ready to be consumed as esm tho.
You can follow the progress of typescript adopting the "module" in package.json.

@2234839
Copy link
Author

2234839 commented Mar 3, 2021

Thanks a lot @alias-mac. Configuring tsconfig "esModuleInterop": true solved my problem perfectly

@2234839 2234839 closed this Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants