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: Append file extension based on runtime #936

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

bombillazo
Copy link

Fixes #935

Adds a new options to output to specify file extensions to generate

Copy link

stackblitz bot commented Aug 14, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

changeset-bot bot commented Aug 14, 2024

⚠️ No Changeset found

Latest commit: 8512ed3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Aug 14, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hey-api-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 15, 2024 3:20am

@mrlubos
Copy link
Member

mrlubos commented Aug 14, 2024

Hey @bombillazo, which extension do you need to run this with your project? Can you not detect Deno runtime from within code? Not a fan of configuration, this should be automatically detected imo. We'll also need to add tests for Deno runtime as everything runs inside Node.js right now

@bombillazo
Copy link
Author

Hey @mrlubos , whichever the extension of the file is. Im not sure how the runtime is detected, I can check on the Deno docs.

@bombillazo
Copy link
Author

I agree autodetected would be ideal, although it will prevent the capacity to generate Deno-compatible code from node or make node compatible code from Deno.

If it's too much overhead for Deno, we can keep discussing and I'll just modify the code generated post-gen on our project for now.

@mrlubos
Copy link
Member

mrlubos commented Aug 14, 2024

Yeah that would be the first thing to resolve. We want to support Bun too (I am not sure if anything is missing right now, haven't looked into it yet). I am surprised you don't need to abstract platform-specific modules such as node:fs, how come?

Also, I am 100% sure there are missed imports, for example services import from types (EDIT: okay not 100% sure, maybe you got them all haha). In my opinion this should be abstracted inside the TypeScriptFile class and let it handle extensions. I don't worry too much about supporting the old code, i.e. Handlebars templates. But it needs to work seamlessly with the new clients without extra configuration, I think that's all achievable

@bombillazo
Copy link
Author

Deno has come a long way to resolve imports from npm:, node: etc out of the box, I run the createClient function from a ts file with deno run and import like this:

image

And it just works, I can generate code from deno no issues.

@mrlubos
Copy link
Member

mrlubos commented Aug 14, 2024

@bombillazo yeah there's a reason I didn't tackle it yet. I don't want to hastily add features because people will start using them and then modifying any mistakes becomes painful. Are you generating Deno-compatible code from Node.js? If so, why?

@bombillazo
Copy link
Author

bombillazo commented Aug 14, 2024

@mrlubos The only reason I generated from node is because I use patch-package which only works for node, I forked this repo, build the new version and copied the dist to my other project to test the code generation. If it works I create a patch so anyone in our repo can use it. I have not found a way to do that in Deno.

Generation currently works for me in both Deno and Node and produce the same outputs

@bombillazo bombillazo changed the title Feature: Add extension option to client Feature: Append file extension based on runtime Aug 15, 2024
@bombillazo
Copy link
Author

@mrlubos I updated the PR to reflect your feedback of making it dynamic

"global.d.ts",
"./src/**/*.ts",
"rollup.config.ts",
"rollup.dts.config.ts"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to add this file?

import { ensureDirSync } from './utils';

const require = createRequire(import.meta.url);

export const clientModulePath = () => {
const config = getConfig();
return config.client.bundle ? './client' : config.client.name;

return appendExt(config.client.bundle ? './client' : config.client.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add extension to node_modules imports too? This may generate code like '@hey-api/client-fetch.ts'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, only for relative/absolute paths or importing specific files.

I see, Id have to check then how to differentiate if its coming from core or an external package...

@@ -0,0 +1,15 @@
// global.d.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file for the utils/runtime module?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if not TypeScript (node) thinks it is an undeclared type since it is not aware of Deno and Bun (rightfully so)

@simonnepomuk
Copy link

Hi guys,

thanks for the effort! Just chiming in that the you also need file extensions when compiling a project with tsc and the following tsconfig.json:

    "module": "NodeNext",
    "strict": true,
    "target": "ES2023",
    "moduleResolution": "NodeNext"

But instead of .ts it needs to be .js. I would imagine that this is not an uncommon use case. Would be happy to assist :)

@jgoux
Copy link

jgoux commented Oct 17, 2024

It would be better to have it configurable without runtime detection. In my case I'm running my project with node --experimental-strip-types and the TypeScript option "allowImportingTsExtensions": true, so I would need to manually indicate that I want .ts extensions in my imports and exports.

@bombillazo
Copy link
Author

It would be better to have it configurable without runtime detection. In my case I'm running my project with node --experimental-strip-types and the TypeScript option "allowImportingTsExtensions": true, so I would need to manually indicate that I want .ts extensions in my imports and exports.

Yup, I agree, in our case it was due to building it for Deno, which requires extensions, but we're running it from node.

@mrlubos
Copy link
Member

mrlubos commented Oct 17, 2024

@bombillazo Would you be able to make the tests pass?

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

Successfully merging this pull request may close these issues.

Enable autogen import/export files with file extension
4 participants