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 to ES Modules #2981

Open
Rigo-m opened this issue Nov 17, 2021 · 12 comments
Open

Add support to ES Modules #2981

Rigo-m opened this issue Nov 17, 2021 · 12 comments

Comments

@Rigo-m
Copy link

Rigo-m commented Nov 17, 2021

What are you trying to achieve?

Use Sharp in a nuxt nitro environment

What would you expect the API to look like?

Sharp should export an .mjs file so that it can be used in environments that doesn't support .cjs

What alternatives have you considered?

None but any suggestion is welcomed

@lovell
Copy link
Owner

lovell commented Nov 17, 2021

Do you know if the wrapper approach outlined in https://nodejs.org/api/packages.html#approach-1-use-an-es-module-wrapper will work with Nuxt Nitro?

@Rigo-m
Copy link
Author

Rigo-m commented Nov 18, 2021

It should work, I think. Otherwise, you can use https://github.com/unjs/mlly to port sharp into ESM 😛

@pi0 any suggestions?

@pi0
Copy link
Contributor

pi0 commented Nov 18, 2021

@Rigo-m Sharp can be imported inside Node.js ESM files and also working fine with Nuxt Nitro (the only limitation is you cannot deploy to Workers because of Node.js bindings). BTW you might like to use ipx which is based on Sharp but easier to integrate. Check this example: https://codesandbox.io/s/quirky-currying-3259v?file=/server/api/ipx.ts

@lovell While Node.js ESM can work with CJS libraries and bundlers like webpack/rollup allow mixed syntax (that's how nitro workers) it would be nice if Sharp also adds exports field. This can make support less hacky and more ESM native. If you want me can make a PR to improve packages' ESM support. Some of popular packages even considered being esm-only.

@lovell
Copy link
Owner

lovell commented Nov 18, 2021

Thanks for confirming, a PR to add a thin ESM wrapper and suitable exports to allow dual-usage would be most welcome. If it's possible to add a unit test for the wrapper, that would be great too.

And thank you Pooya for all your work on Nuxt and the Vue ecosystem!

@pi0
Copy link
Contributor

pi0 commented Apr 19, 2022

Hi @lovell. Just pinging to say still would love to contribute to this for ESM support! @danielroe is also an ESM expert who might help on this as well :)

@robinscholz
Copy link

@pi0 Do you happen to have an example with sharp and nuxt nitro somewhere? Running into problems, where sharp is a dependency of a package that is imported for a server side api in nuxt 3.

@pi0
Copy link
Contributor

pi0 commented May 16, 2022

Usage is in sandbox comment above but official Nuxt3 integration is coming soon with Image module.

@shtse8
Copy link

shtse8 commented Jul 13, 2022

looking forward to this support

@spartanz51
Copy link

import { createCommonJS } from 'mlly'
const { __dirname, __filename, require } = createCommonJS(import.meta.url)
import sharp from 'sharp'

@TimisRobert
Copy link

import { createCommonJS } from 'mlly'
const { __dirname, __filename, require } = createCommonJS(import.meta.url)
import sharp from 'sharp'

I'm trying to make it work with esbuild, but I always get __dirname is not defined :(

@vikyw89

This comment was marked as off-topic.

@maddsua

This comment was marked as off-topic.

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

No branches or pull requests

9 participants