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

Make astro package play nice with node16 module resolution #4182

Merged
merged 5 commits into from
Aug 25, 2022

Conversation

Alxandr
Copy link
Contributor

@Alxandr Alxandr commented Aug 6, 2022

Projects using node16 module resolution in typescript uses the new
exports and imports configuration from typescript to find definition
files. This mirrors how nodejs resolves the files. If a package contains
an exports map in the package.json, typescript will ignore the "types"
field (not sure how it plays with typesVersions). This moves the typings
hirearchy of definition files into the same hierarchy that astro
produces output files in, so that typescript can discover them.

Changes

Projects using node16 module resolution in typescript uses the new
exports and imports configuration from typescript to find definition
files. This mirrors how nodejs resolves the files. If a package contains
an exports map in the package.json, typescript will ignore the "types"
field (not sure how it plays with typesVersions). This moves the typings
hirearchy of definition files into the same hierarchy that astro
produces output files in, so that typescript can discover them.

Testing

I've used pnpm link locally to do import type { AstroIntegration } from 'astro'; - it does not work if I just use the astro package from NPM.

Docs

This should only be visible in that people who use the new module resolution can now get typings - and also, the file hierarchy of the dist folder in the package is modified in that type-declarations now live next to their js counterparts.

Fixes: #4172

Projects using node16 module resolution in typescript uses the new
exports and imports configuration from typescript to find definition
files. This mirrors how nodejs resolves the files. If a package contains
an exports map in the package.json, typescript will ignore the "types"
field (not sure how it plays with typesVersions). This moves the typings
hirearchy of definition files into the same hierarchy that astro
produces output files in, so that typescript can discover them.

Fixes: withastro#4172
@changeset-bot
Copy link

changeset-bot bot commented Aug 6, 2022

🦋 Changeset detected

Latest commit: 0714a8e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
astro Minor
@e2e/astro-component Patch
@e2e/error-react-spectrum Patch
@e2e/error-sass Patch
@e2e/errors Patch
@e2e/hydration-race Patch
@e2e/lit-component Patch
@e2e/preact-component Patch
@e2e/react-component Patch
@e2e/solid-component Patch
@e2e/solid-recurse Patch
@e2e/svelte-component Patch
@e2e/e2e-tailwindcss Patch
@e2e/ts-resolution Patch

Not sure what this means? Click here to learn what changesets are.

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

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Aug 6, 2022
@FredKSchott
Copy link
Member

This all makes sense to me! Thanks for the investigation and the thorough PR.

As Nate said in the issue I’d also want a +1 from out resident TS expert @Princesseuh before merging.

]
}
},
"exports": {
".": "./astro.js",
".": {"default":"./astro.js", "types": "./dist/@types/astro.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.

Note: the types condition should come before default. (typescript docs)

Copy link
Contributor Author

@Alxandr Alxandr Aug 8, 2022

Choose a reason for hiding this comment

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

This is JSON, the order is arbitrary. I see the example in the docs has it before, but that's just an example, and nowhere in the text I read that it should come before (not as far as I can understand at least) - if I'm reading this wrong, please point out where I'm misreading.

Also do note that this is actually working, as I'm running locally with a pnpm patch to make astro work for me.

[Edit]
Note; I'm not opposed to re-order. If you want it in the other order that's fine (and I don't really care). If however typescript requires the types-field to be first, I would like to know, so I know not to make the error again. If it does not, I would also like to know that I can just order them however I like for other projects.

Copy link
Member

Choose a reason for hiding this comment

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

It's shown in this code example in the linked docs:

image

and in the announcement post:

image

Disclaimer: I've created publint, so I'm picky with this 🙂

Copy link
Member

@Princesseuh Princesseuh Aug 8, 2022

Choose a reason for hiding this comment

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

It doesn't really matter in this case, but Node's documentation also mention that default should always be last:

Screenshot_20220808-075052.jpg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I love being picky as long as there's clear rules, and in this case there seems to be. I think it's a bad idea to have order-dependent JSON, so I'd argue node made a bad decision here, but it's what we got to live with, I guess. I'll update the pr when I'm back at my computer.

Copy link
Member

Choose a reason for hiding this comment

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

Oh wow, I didn't know about these rules! That's super annoying, but yeah it would be great to make sure we're following them exactly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the order.

Copy link
Member

@Princesseuh Princesseuh left a comment

Choose a reason for hiding this comment

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

Thank you for making this PR, support for moduleResolution: 'node16' is definitely something we want in Astro! In addition to the comment about the order of the types, the path to the types should be updated in astro-jsx.d.ts.

Additionally, an important note about this: While this unblock usage inside .ts(x) files, moduleResolution: 'node16' is currently unsupported in our language server so inside .astro file, even if you set it to node16, node will be instead forced on.

Hopefully this is not a dealbreaker for you to use Astro, we're definitely planning to support node16 as soon as possible, but we're currently focused on other tasks.

Apart from that, it looks good to me. It's a bit hard to tell on such a large change, but I don't see any reasons it would break anything. However, just to be sure we don't break everything right before the release of 1.0 (tomorrow!), we'll wait after the release to merge this in, hope that's okay.

@Alxandr
Copy link
Contributor Author

Alxandr commented Aug 10, 2022

I'm working around the issue of astro-files not caring about node16 by moving code to .tsm files and just importing them in astro-files - and for the most part that works (though it's definitely been less than optimal). I'm assuming the greater ecosystem will eventually migrate most of the things to work with node16, but it will definitely take time.

Copy link
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

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

Marking this a blocked for now as we're still discussing whether this should be a minor or patch release.

@matthewp matthewp added the semver: minor Change triggers a `minor` release label Aug 23, 2022
@matthewp
Copy link
Contributor

This is marked as being a minor release, so it can go out the next time we do one of those.

@matthewp matthewp merged commit fcc36ac into withastro:main Aug 25, 2022
@Alxandr Alxandr deleted the fix/type-hierarchy branch August 25, 2022 20:41
@astrobot-houston astrobot-houston mentioned this pull request Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) semver: minor Change triggers a `minor` release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Astro does not work in projects with "moduleResolution": "Node16"
6 participants