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

Resolve components by module ID during compilation #3300

Merged
merged 14 commits into from
May 12, 2022
Merged

Conversation

tony-sull
Copy link
Contributor

Changes

This updates Astro to resolve .astro components by Vite's module ID, fixing an issue using .astro and framework components in shared NPM packages

Details

During the compilation step, Astro was parsing the file path for a component and expecting astro components to always live in the project's root directory.

This caused a few weird corner cases for NPM libraries that use both .astro and framework components internally, ex: a theme that is mostly .astro with a few internal react components for interactivity.

Testing

Added a test suite that uses components from a separate package in the monorepo. Without the fix this test suite fails to build when the component files can't be found

Docs

None, but I will open a separate PR to update our component starter once this fix is published

@changeset-bot
Copy link

changeset-bot bot commented May 5, 2022

🦋 Changeset detected

Latest commit: e523a0c

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

This PR includes changesets to release 1 package
Name Type
astro 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 pkg: astro Related to the core `astro` package (scope) test labels May 5, 2022
@tony-sull tony-sull marked this pull request as draft May 5, 2022 16:11
@tony-sull tony-sull force-pushed the fix/hydration-in-npm branch 3 times, most recently from 4359e72 to 2282d0b Compare May 6, 2022 15:36
viteTransform: TransformHook;
}

async function compile({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(opinionated) using a single object prop to clean up the method props

@tony-sull tony-sull force-pushed the fix/hydration-in-npm branch from 95a8e27 to afaa67e Compare May 6, 2022 21:58
@tony-sull tony-sull marked this pull request as ready for review May 6, 2022 21:58
@aFuzzyBear
Copy link
Contributor

Thank you @tony-sull really appreciate this fix dude

@delucis
Copy link
Member

delucis commented May 6, 2022

Dang, was really hopeful this might also solve #3053, but it doesn’t look like it does. Great work nevertheless!

"@astrojs/preact": "workspace:*",
"@astrojs/react": "workspace:*",
"@astrojs/svelte": "workspace:*",
"@test/component-library-shared": "workspace:*",
Copy link
Contributor

@bholmesdev bholmesdev May 6, 2022

Choose a reason for hiding this comment

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

I'm always a little weary of workspace:* as a way to test external dependencies. Is it possible to generate a tarball from component-library-shared via pnpm pack, and reference that file from here? This would 100% mock an external npm dependency

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'm not actually sure if tarballs work properly in vite 🤔 I don't use pnpm pack often so I may have it misconfigured, but I tried this locally and Vite blew up with errors failing to resolve anything inside the tarball dependency

link:../component-library-shared would work for the tests, but I'm not sure the details of how that's different from workspace:*

@bholmesdev
Copy link
Contributor

Also, does this resolve any reported bugs? Didn't see any in the description so double checking!

@tony-sull
Copy link
Contributor Author

@bholmesdev I still need to make a pass through all the open issues to see if any seem related. I can't help but think this was the underlying cause of a few strange bugs, but I ran into this when working on the docs template and forgot to file my own issue to track before diving into the fix

@tony-sull
Copy link
Contributor Author

Dang, was really hopeful this might also solve #3053, but it doesn’t look like it does. Great work nevertheless!

I was hoping the same 🙃 Hopefully this gets us closer though, I'd really like to see that issue fixed soon as well!

@tony-sull tony-sull force-pushed the fix/hydration-in-npm branch from afaa67e to e523a0c Compare May 10, 2022 16:24
@FredKSchott FredKSchott removed the test label May 10, 2022
@matthewp matthewp merged commit b463ddb into main May 12, 2022
@matthewp matthewp deleted the fix/hydration-in-npm branch May 12, 2022 16:04
@github-actions github-actions bot mentioned this pull request May 12, 2022
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* WIP: adding test coverage

* test fixes

* moving the shared lib up a directory to reproduce the bug

* fix: transform with the module ID instead of parsing the filepath

* adding the shared lib to the workspaces list

* fix: client-only assets now get the full URL from vite

* why is this needed for windows?

* WIP: using /@fs to handle windows filepaths

* fix: remove /@fs from hoisted script imports

* nit: removing unused imports

* fix: strip off the path root when mapping client:only styles

* had to reverse the `/@fs` handling to work on windows and unix

* chore: adding comments to explain the fix

* chore: adding changeset
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)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Client hydration not working in components installed via npm
6 participants