-
-
Notifications
You must be signed in to change notification settings - Fork 65
expose named export #231
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
expose named export #231
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.
I dont want to close it without discussion but also dont want that somebody merges this without thinking the implications.
This plugin uses fastify-plugin. Fastify-plugin itself provides a named export.
So you can do
const { fastifyCors } = require('@fastify/cors')
So i would not merge this, as we should already provide a named export.
@Uzlopak we should at least adjust TS types and readme to indicate that is possible then |
If you want to earn some brownie points than it would be good if we can fix the typings so that latest tsd passes and nodenext is supported. See the other open PR. :) |
Ah wait. I thought @akphi answered... But well. Still the same. We should fix nodenext |
@Uzlopak what do you suppose we need for I have tried locally to hack the
that makes me believe my change to the |
If you dont want to help to get #224 done, it is ok. Yes, just do the typescrpt change and also add a typing test :) |
@Uzlopak hm, compared to #230 I'm trying to make this work with the named import case for Even after patching the export { fastifyCors }; it doesn't work, I got the error like mentioned in that PR:
I believe I still need to make the changes I made in |
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.
Just a remark
@@ -220,7 +220,15 @@ function isRequestOriginAllowed (reqOrigin, allowedOrigin) { | |||
} | |||
} | |||
|
|||
module.exports = fp(fastifyCors, { | |||
const _fastifyCors = fp(fastifyCors, { |
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.
revert tiese changes, and I will approve and merge it ;)
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.
This addresses the issue I found in akphi/issue-repo#11 in my project using NodeNext
. Is there something I miss here, but what you said about fastify-plugin
just doesn't work for our Typescript project. I can help investigate, but I believe this is the fix
Do we have an underlying issue in fastify-plugin? |
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.
lgtm
We should not merge this. |
Please, use the "Request changes" review to block or I may miss it 😓 Do you think the named export could be added to |
I already did use Request Changes option. We have already named export in fastify-plugin. So the question is, if it is actually properly working or not. I have the feeling that the issue is typescript and not Javascript. |
I'll try to setup just a JS script with ESmoule |
Could you link it? |
Named Export, e.g. .default Export, e.g. Default Export, e.g. |
I tested with akphi/issue-repo#11 import { fastify } from "fastify";
import { fastifyCors } from "@fastify/cors";
const PORT = 9999;
const server = fastify({
logger: true,
});
server.register(fastifyCors, {
methods: ["OPTIONS"],
origin: [/localhost/],
credentials: true,
});
server.listen(
{
port: PORT,
},
(error, address) => {
if (error) {
throw error;
}
}
); If you run the script
I have tested by poking into |
It is a TS problem, because TS will read the file and see if it suitable. Of cause, the changes in |
But I faced the problem when I run |
Strange, it certainly something changes in node. From reading the document, it's seems like the current import of commonjs will go through a static analysis and it only permit some of the syntax. |
In the latest version of
|
@climba03003 does this justify my fix? Is there anything else you need? |
Sry |
Yes, but the current |
OK, the issue is now clear to me. Let me try to summarise it. When Node.js uses ESM context (so you are running an entry point with |
It will support if you // supported in NodeJS and TypeScript
module.exports = plugin
module.exports.default = plugin // here may not be needed, but maybe TypeScript need it
module.exports.plugin = plugin
// only supported in TypeScript
module.exports = fp(plugin) I have tried various old version of |
Yes! I've just played a little bit with a repro on my machine and we need to rely on that Node.js static analysis on CJS imports. Are we saying that we should "just" fix |
They are different problem. In here, we need to support native
|
Yes, I am aware of it. Probably I've written the reply too fast and with few details. First, we should add explicit triplets to fastify the plugins we maintain to make Node.js' ESM context happy while leaving the TS exports as ESM triplets to make TS happy. |
Nothing missing. For a complete support for all combination of both // index.js
function plugin() {}
// CJS export
module.exports = plugin
// TypeScript ESM default export, not confirmed
module.exports.default = plugin
// CJS and ESM named export
module.exports.plugin = plugin
// index.d.ts
// type for plugin
type Plugin = FastifyPluginCallback<plugin.PluginOptions>
// required for CJS
namespace plugin {
// other export of typings
export interface PluginOptions {}
// CJS and ESM named export
export const plugin: Plugin
// ESM default export
export { default as plugin }
}
// actual plugin usage
declare function plugin(...params: Parameters<Plugin>): : ReturnType<Plugin>
// CJS module.exports
export = plugin
|
I am still not convinced about Exporting namespace will break |
No, it is supported by
|
I am talking about the types here (so what the compiler sees thanks to the type declarations), not the runtime (so |
Yes, it is supported. You can use // tsconfig.json
{
"compilerOptions": {
"esModuleInterop": false
}
}
// index.ts
import FastifyCookie from '@fastify/cookie';
import Fastify from 'fastify';
const fastify = Fastify()
fastify.register(FastifyCookie)
;(async function() {
await fastify.ready()
})() |
@climba03003 awesome. We should write up somewhere all of these findings to remember them in the future! |
@climba03003 In order to make it work, I needed to slightly adjust your types:
|
This is the explanation of what happened here (and in other Fastify packages) and why we need to start using the above types in the future! |
Issue has been open in fastify/fastify#4349 |
Two remarks:
|
Is this still relevant afterr #224 ? |
Yes, keeps the changes inside |
@climba03003 I have updated the PR |
I am not convinced but I dont want to be the blocker
Checklist
I'm not as ambitious as #224, I'm just trying to make sure
named
exports work for@fastify/cors
. I'm following what we already did in https://github.com/fastify/fastifyhttps://github.com/fastify/fastify/blob/acba25acc3e97e3598daf79537a39a5272dba7b8/fastify.js#L783-L796
npm run test
andnpm run lint
and the Code of conduct