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

ESM Support #2423

Open
frencojobs opened this issue Aug 2, 2021 · 10 comments
Open

ESM Support #2423

frencojobs opened this issue Aug 2, 2021 · 10 comments

Comments

@frencojobs
Copy link

frencojobs commented Aug 2, 2021

Now that most of the libraries are already in ESM, I think it's time Feathers also release an ESM build. It should be pretty easy just to change the module from commonjs to ES2020 a few other changes mentioned in https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c.

"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */

The advantage is then I could just do

import { Forbidden } from '@feathersjs/errors'

instead of

import errors from '@feathersjs/errors'
const { Forbidden } = errors;
@daffl
Copy link
Member

daffl commented Aug 2, 2021

Is this available without a flag in all currently supported NodeJS versions? I was thinking of adding it as another build but if that is the case I'd much rather make it the default that's published.

@frencojobs
Copy link
Author

frencojobs commented Aug 3, 2021

Yep, Node 10 which doesn't support ESM was already dropped as end-of-life. So, the answer is yes.

But, not by default, users have to opt in via "type": "module" flag in package.json.

@frencojobs
Copy link
Author

frencojobs commented Aug 3, 2021

without a flag

Sorry, I glossed over this part. So the answer is no I guess. So adding it as another build might be better for now. But you can configure package.json to contain both CommonJS and ESM modules. You might want to use a tool like Rollup or Webpack for that.

@joezappie
Copy link
Contributor

I'd find this very useful too. Starting a new app but don't want to use typescript for it. I would like to use import/export though so it would be nice to be able to generate the app as an esm project.

@daffl
Copy link
Member

daffl commented Aug 16, 2021

This will be the default in v5 and the new CLI currently under development in #2425

@joezappie
Copy link
Contributor

Awesome! Thanks for the info

@steffanhalv
Copy link

steffanhalv commented Jun 10, 2022

@daffl Can you please add feathers.esm.js to dist folder on build?
https://unpkg.com/browse/@feathersjs/[email protected]/dist/

Then we can import feathers client using es modules from CDN. Ex.

<script type="module">
  import feathers from 'https://unpkg.com/@feathersjs/[email protected]/dist/feathers.esm.js'
  const app = feathers()
</script>

Workaround

Currently the index.js file is of type UMD and can be loaded using this workaround:

./umdloader.js

export default async (url, module = {exports:{}}) =>
  (Function('module', 'exports', await (await fetch(url)).text()).call(module, module, module.exports), module).exports

./index.html

<script type="module">
  import loadUMD from './umdloader.js'
  loadUMD('https://unpkg.com/@feathersjs/[email protected]/dist/feathers.esm.js').then(feathers => {
    const app = feathers()
  })
</script>

Ref. https://stackoverflow.com/a/64721256/2298745

@vixalien
Copy link

vixalien commented Aug 4, 2022

I also believe you should add an exports field to the package.json to differentiate the different build assets (browser, esm, cjs etc). I believe

@daffl
Copy link
Member

daffl commented Aug 5, 2022

We have been trying to do this in #2665 for the Feathers core packages but so far have not gotten it to work.

@steffanhalv
Copy link

Looks like it is @feathersjs/[email protected] which are causing the problem:

> @feathersjs/[email protected] compile:esm
> shx rm -rf esm/ && tsc --target es2020 --module es2020 --outDir esm/ --moduleResolution node

> @feathersjs/[email protected] prepublish /home/runner/work/feathers/feathers/packages/socketio-client
> npm run compile

lerna ERR! lifecycle "prepublish" errored in "@feathersjs/socketio-client", exiting 2
npm ERR! code 2
npm ERR! path /home/runner/work/feathers/feathers
npm ERR! command failed
npm ERR! command sh -c lerna bootstrap

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2022-06-13T21_13_28_365Z-debug-0.log
Error: Process completed with exit code 2.

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

No branches or pull requests

5 participants