-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
fix(types): correct CommonJS export in @fastify/otel typings #30
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add some tests for it? Check test/index.test-d.ts
@@ -16,5 +16,4 @@ declare class FastifyOtelInstrumentation<Config extends FastifyOtelInstrumentati | |||
plugin (): (instance: FastifyInstance, opts: FastifyOtelOptions, done: (err?: Error) => void) => void | |||
} | |||
|
|||
export default FastifyOtelInstrumentation | |||
export { FastifyOtelInstrumentation } | |||
export = FastifyOtelInstrumentation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot!
Is this enough? Should we export the default as other plugins do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recall spotting other issues on other plugins, but also I import them into register in an fp so maybe I missed the issues. If it's an issue here, then it's also presumably an issue elsewhere. There might be other places where this is addressed, would need to investigate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add some tests using eg tsd?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mucked around with the tests, and I simply am not familiar enough with TSD or CJS/ESM nuance tests to know how to actually test for this.
Yes, I will add some when I find time on an evening. Anyone feel free to beat me to it. |
@@ -16,5 +16,4 @@ declare class FastifyOtelInstrumentation<Config extends FastifyOtelInstrumentati | |||
plugin (): (instance: FastifyInstance, opts: FastifyOtelOptions, done: (err?: Error) => void) => void | |||
} | |||
|
|||
export default FastifyOtelInstrumentation | |||
export { FastifyOtelInstrumentation } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no attempt at named exports afaict:
Line 410 in 1f850f6
module.exports = FastifyOtelInstrumentation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bcomnes can you add the named export support?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added named exports and a default export.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's possible to support named mjs exports correctly when assigning module.exports like this. MJS resolves this as { default, and 'module.exports' }.
The typescript typings were exporting using the ESM style exports, but this is a CJS module, so the exports were not resolving correctly. This switches the type export to use `export = ` syntax instead which works for ESM consumers as well as CJS consumers. Fixes fastify#24 Signed-off-by: Bret Comnes <166301+bcomnes@users.noreply.github.com>
Any suggestions on how to test this? |
I got the named exports working with the top level class. I'll send an alternative request in shortly. |
Closing this PR out in favor of https://github.com/fastify/otel/pull/43/files |
Fixes #24
module.exports
in mjs and named exports were not working before. Now they are importable in cjs and mjs as one would expect.export =
because this is a type file for a cjs module so we cannot useexport foo
orexport default
anywhere without introducing a mismatch between the module system being used in the implementation and types.fastify
devDependency
should not be imported inindex.d.ts
#32to a fastify-mocks.d.ts file to also use named exports but to also discourage their import from consumers, just in case their editor tries to import a fastify type incorrectly out of this module. In general mocking these types seems like it's asking for trouble from a maintenance perspective. Fastify was previously an unlisted peer dependency (presumably because listed peer dependencies can be a nightmare) just as it is in nearly every other fastify plugin. Mocking these types will require ongoing one-off maintenance and will also potentially mask breaking issues as they materialize. Maybe someone more familiar with the deliberate strategy of using unlisted peer dependencies can chime in.
Checklist
npm run test
andnpm run benchmark
and the Code of conduct