allynsweet / E2E-Mailbox

A way to check emails your system sends from integration tests.
33 stars 2 forks source link

node 20+ esm style exports #5

Open tom2strobl opened 1 month ago

tom2strobl commented 1 month ago

Hi there!

could you add exports for esm style loading (type: "module" in package.json)

import E2EMailbox from 'e2e-mailbox'
new E2EMailbox()
^ This expression is not constructable.
  Type 'typeof import("/<somedir>/node_modules/e2e-mailbox/lib/index")' has no construct signatures.ts(2351)

fuse.js does it like this in their package.json for example:

{
  "exports": {
    ".": {
      "types": "./dist/fuse.d.ts",
      "import": "./dist/fuse.mjs",
      "require": "./dist/fuse.cjs"
    },
    "./min": {
      "types": "./dist/fuse.d.ts",
      "import": "./dist/fuse.min.mjs",
      "require": "./dist/fuse.min.cjs"
    }
  }
}

Thanks!

allynsweet commented 1 month ago

Hey, I'll look into this improvement this week, seems like a simple enough addition. Thanks!

myieye commented 2 weeks ago

Would also appreciate this 👍

allynsweet commented 2 weeks ago

I just published version 1.1.6 that maps import/require exports to their proper module similar to the way @tom2strobl shared from fuse.js. If you guys could try it out and let me know if that's what you were looking for, I'd appreciate it!

myieye commented 1 week ago

Thanks for the effort 💪

I got:

(node:27280) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
D:\code\languageforge-lexbox\frontend\node_modules\.pnpm\e2e-mailbox@1.1.6\node_modules\e2e-mailbox\dist\esm\index.js:1
import DeveloperMailService from './services/developerMailService';
^^^^^^

SyntaxError: Cannot use import statement outside a module

If I change the extension to .mjs, I get: Error: Cannot find module 'D:\code\languageforge-lexbox\frontend\node_modules\.pnpm\e2e-mailbox@file+..+..+e2e-mailbox\node_modules\e2e-mailbox\dist\esm\services\developerMailService' imported from D:\code\languageforge-lexbox\frontend\node_modules\.pnpm\e2e-mailbox@file+..+..+e2e-mailbox\node_modules\e2e-mailbox\dist\esm\index.mjs

I noticed that fuse.js is using webpack to pack everything into index.mjs. Presumably that would help. 🤷