Skip to content

Stub out .astro imports in client modules#14751

Merged
delucis merged 4 commits intomainfrom
chris/stub-astro-on-client
Nov 11, 2025
Merged

Stub out .astro imports in client modules#14751
delucis merged 4 commits intomainfrom
chris/stub-astro-on-client

Conversation

@delucis
Copy link
Copy Markdown
Member

@delucis delucis commented Nov 11, 2025

Changes

  • In picocolors is being bundled into the browser #14692 (comment) a user pointed out that they expected Astro to automatically “know” not to include .astro components in client code, allowing a mixed barrel file that exported both Astro and JSX components (this works in build currently but not dev since Replace kleur with picocolors #14598). (I’m not sure if that should be supported, but people are doing it, and on the surface these imports are used in the same place in the same way, so I was curious if we could allow it for now.)
  • This PR stubs out the compiled Astro component when imported in non-SSR contexts. In dev, it exports a default function (similar to compiled .astro) that throws an error if used. In prod, it just exports an empty object.
  • This also has the advantage of skipping some pointless compiler calls in dev mode if people are doing this.

Testing

I added a test for dev and build behaviour.

I also tested manually by creating a barrel file in the framework-react example to re-export a React component and a simple Astro component:

export { default as Counter } from './Counter';
export { default as Example } from './Example.astro';

I also excluded picocolors from optimized deps in the example config because Vite automatically optimizes this in the monorepo, but not in user projects:

	vite: {
		optimizeDeps: {
			exclude: ['picocolors'],
		},
	},

I then imported both components from that barrel file and rendered them. Using client:load on the React component.

Before changing astro, the example project fails to hydrate due to trying to load picocolors (i.e. server code). With the change from this PR in place, everything works as expected in the example, with no hydration issues caused by server code being present during dev.

Docs

n/a — bug fix. Although maybe we should consider removing the example in the “Publish to NPM” guide that explicitly shows the mixed Astro/React barrel file code if we’re unsure that’s a best practice.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Nov 11, 2025

🦋 Changeset detected

Latest commit: 4960fc3

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

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

@delucis delucis marked this pull request as draft November 11, 2025 11:26
@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Nov 11, 2025
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Nov 11, 2025

CodSpeed Performance Report

Merging #14751 will not alter performance

Comparing chris/stub-astro-on-client (4960fc3) with main (74c8852)1

Summary

✅ 6 untouched

Footnotes

  1. No successful run was found on main (5bc37fd) during the generation of this report, so 74c8852 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@delucis delucis marked this pull request as ready for review November 11, 2025 12:12
@github-actions

This comment was marked as spam.

@delucis delucis merged commit 18c55e1 into main Nov 11, 2025
45 of 47 checks passed
@delucis delucis deleted the chris/stub-astro-on-client branch November 11, 2025 20:23
This was referenced Nov 11, 2025
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.

2 participants