shellscape / jsx-email

Build emails with a delightful DX
https://jsx.email
MIT License
961 stars 31 forks source link

ESM build is not working #38

Closed kasperpeulen closed 10 months ago

kasperpeulen commented 11 months ago

Expected Behavior

I should be able to run jsx-email in node in projects that are "type:module":

{
  "type": "module",
  "dependencies": {
    "@jsx-email/all": "^2.0.1"
  }
}

Actual Behavior

I get an error:

file:///Users/kasperpeulen/code/modern-web-app/bla.js:1
import {renderAsync} from '@jsx-email/all';
        ^^^^^^^^^^^
SyntaxError: Named export 'renderAsync' not found. The requested module '@jsx-email/all' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@jsx-email/all';
const {renderAsync} = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Node.js v18.18.0

Additional Information

I think this can be fixed by making sure that the ESM build outputs .mjs files.

image

I tried renaming the esm output files locally in node_modules and that worked.

shellscape commented 11 months ago

This is not the fault of jsx-email. We export both ESM and CJS. Please fill out the entire issue template next time.

kasperpeulen commented 11 months ago

Added a repro @shellscape

shellscape commented 11 months ago

Your reproduction doesn't show an error on my end. Looks like it's working fine.

Screenshot 2023-10-12 at 9 15 00 AM
kasperpeulen commented 11 months ago

@shellscape I added now a stackblitz start command (node index.js), you should see:

image
shellscape commented 10 months ago

OK thanks we'll take a look

shellscape commented 10 months ago

I've managed to reproduce this locally. ESM is such a pain. It may be a bit before we roll out a fix for this, it requires altering how our build process works significantly. If you can use TypeScript, I'd recommend doing so.

kasperpeulen commented 10 months ago

I think if you want to use typescript, the steps could be.

  1. Place an extra package.json file in the dist/es directory. With as only content:
    {
    "type": "module",
    }

    This avoid having to manually rename js files to mjs files.

  2. Change the moduleResolution to node16
  3. Add .js file extensions to all relative imports in the source code. For example: export * from './body.js';
shellscape commented 10 months ago

alright, we're going to completely change up our build process in the next few days which should resolve this.

shellscape commented 10 months ago

the next few days turned into a week. haven't forgotten about it, just got swamped with work. going to run some tests this evening.

shellscape commented 10 months ago

@kasperpeulen should be resolved now. cheers

alexanderson1993 commented 10 months ago

Hey, huge thanks for putting in the work to do this. You just unblocked me shipping the next version of my personal newsletter platform. Can't wait for my emails to look ✨ beautiful ✨!