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

Feature: ESM module #253

Open
AshotN opened this issue Apr 19, 2023 · 7 comments
Open

Feature: ESM module #253

AshotN opened this issue Apr 19, 2023 · 7 comments

Comments

@AshotN
Copy link
Contributor

AshotN commented Apr 19, 2023

Is your feature request related to a problem? Please describe.
I am attempting to move my code base off of CJS and onto ESM.

Describe the solution you'd like
For feathers-swagger to support both ESM and CJS with conditional exports

Describe alternatives you've considered
Default exports are compatible across ESM and CJS, but it is kind of a pain as far as developer experience goes

Additional context
I recently updated my package to support both, as an example AshotN/adminjs-feathers@69a5360

@Mairu
Copy link
Collaborator

Mairu commented Apr 20, 2023

feathers-swagger is quite an old module written in plain javascript not using any transpiler.

Can you explain to me the pain of importing cjs modules into esm code?

@Mairu
Copy link
Collaborator

Mairu commented Apr 22, 2023

@AshotN Can you test the branch https://github.com/feathersjs-ecosystem/feathers-swagger/tree/esm if it does work like that?

@AshotN
Copy link
Contributor Author

AshotN commented Apr 24, 2023

feathers-swagger is quite an old module written in plain javascript not using any transpiler.

Can you explain to me the pain of importing cjs modules into esm code?

Having to import like this

import feathersSwagger from 'feathers-swagger'
const { createSwaggerServiceOptions } = feathersSwagger

@AshotN Can you test the branch https://github.com/feathersjs-ecosystem/feathers-swagger/tree/esm if it does work like that?

Mostly everything worked, besides the functions are not available when default importing.

Have to do this

import swagger, { customMethodsHandler, swaggerUI } from 'feathers-swagger'

because

swagger.swaggerUI is undefined

@Mairu
Copy link
Collaborator

Mairu commented Apr 24, 2023

Mostly everything worked, besides the functions are not available when default importing.

Have to do this

import swagger, { customMethodsHandler, swaggerUI } from 'feathers-swagger'

because

swagger.swaggerUI is undefined

This was on purpose, I thought this is how it should be to allow "tree shaking" even if it is not a real thing on the backend, but then it is a breaking change, mhh.

Btw in my Typescript projects import { createSwaggerServiceOptions } from 'feathers-swagger'; already worked before, why do you have to split this? -> https://github.com/Mairu/feathersjs-swagger-tests/blob/feathers-v5-koa-typebox/src/services/users/users.ts#L4

@AshotN
Copy link
Contributor Author

AshotN commented Apr 24, 2023

That works because you are not using ESM.

https://github.com/Mairu/feathersjs-swagger-tests/blob/feathers-v5-koa-typebox/tsconfig.json#L7

If swagger.swaggerUI is meant to be undefined. Then the types should be fixed for it

@AwesomeBobX64
Copy link

Chiming in on this old issue. I'm trying to use esbuild for my feathersjs app and feathers-swagger is trying to import @featuresjs/express/rest in a way maybe it was not intended to:


✘ [ERROR] Could not resolve "@feathersjs/express/rest"

    node_modules/feathers-swagger/lib/custom-methods.js:13:46:
      13 │   const { HTTP_METHOD, httpMethod } = require('@feathersjs/express/rest');
         ╵                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "@feathersjs/express/rest" as external to exclude it from the bundle, which
  will remove this error and leave the unresolved path in the bundle. You can also surround this
  "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.

Could this possibly be fixed by the same issue or is it unrelated?

@Mairu
Copy link
Collaborator

Mairu commented May 2, 2024

Hi @AwesomeBobX64, sorry for the late response.

I don't think it is related. This is an export only available in feathers v4 and feathers-swagger supports v4 and v5 and checks on runtime which feathers version is installed by checking the available dependency.

And there is a try/catch checking done before in the if block.

if (checkImport('@feathersjs/express/rest', 'HTTP_METHOD')) {
const { activateHooks } = require('@feathersjs/feathers');
const { HTTP_METHOD, httpMethod } = require('@feathersjs/express/rest');

exports.checkImport = function checkImport (packageName, child) {
try {
const rest = require(packageName);
/* __istanbul ignore else: for @feathers/express versions < 4 */
if (rest[child]) {
return true;
}
} catch (e) {}
/* __istanbul ignore next: for @feathers/express versions < 4 */
return false;
};

Were you able to mark it as external to resolve your issue?

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

3 participants