Skip to content

Conversation

@HiDeoo
Copy link
Member

@HiDeoo HiDeoo commented Jul 25, 2025

Description

The linked issue and its reproduction showcase build errors with the Cloudflare adapter due to some Node.js builtins ending up in the final bundle. Using updated dependencies seems to no longer cause this issue with some modules being automatically externalized by Vite but still logging some warnings, for example:

[WARN] [vite] [plugin vite:resolve] Automatically externalized node built-in module "node:url" imported from "node_modules/@astrojs/starlight/utils/collection.ts". Consider adding it to environments.ssr.external if it is intended.

While this may work now, this PR aims to fix such issues and prevent regressions in the future by adding a Vite plugin (copied from Astro and slightly tweaked) to one of our e2e fixtures that checks that the final bundle does not have a dependencies on Node.js builtins due to Starlight and throws an error if it does.

Regarding the issue, 3 main reasons for the issue were identified:

  • In Aside: Support custom icons #2261, we added support for custom icons in asides and we throw an error if the icon does not exist. The code throwing the error was located in the aside remark plugin and imported in the <Aside> component. This caused some Node.js builtins used in the remark plugin to end up in the final bundle. To fix this, the shared error was moved to a dedicated file.
  • The collection.ts file which contains various helpers to get paths to collections used in Starlight and used a lot throughout the codebase, contained 1 function relying on Node.js builtins which ended up leaking due to many places importing this file without using the function. To fix this, the function was moved to a dedicated file.
  • The absolutePathToLang helper shared between many integrations was recently updated in Fix absolutePathToLang() issue #3298 to use a Node.js builtin (pathToFileURL) to convert a path to a URL and avoid issues with paths with some encoded characters, e.g. a space. This helper used by Expressive Code can end up in the final bundle due to it's usage in the preprocessor config which is also exposed in a virtual module through packages/starlight/internal.ts for the <Code> component. To fix this, the helper has been updated to no longer be in charge of figuring out the docs collection path, and instead the POSIX path to this collection is passed down to this helper.

An additional test for these changes is also to install and build the documentation using the Cloudflare adapter and making sure that no log warning is emitted about Node.js builtins being automatically externalized.

Remaining tasks

  • Add changeset (will do that later, have to run for now 🏃‍➡️)

@changeset-bot
Copy link

changeset-bot bot commented Jul 25, 2025

🦋 Changeset detected

Latest commit: 2168fc1

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

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight 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 🌟 core Changes to Starlight’s main package label Jul 25, 2025
@netlify
Copy link

netlify bot commented Jul 25, 2025

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 2168fc1
🔍 Latest deploy log https://app.netlify.com/projects/astro-starlight/deploys/6888976f075c81000826cc9d
😎 Deploy Preview https://deploy-preview-3341--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 96 (🔴 down 4 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Looking good @HiDeoo! Left a couple of notes and I guess this needs a changeset?

Comment on lines 26 to 28
export function getCollectionPath(collection: StarlightCollection, srcDir: URL) {
return getCollectionUrl(collection, srcDir).pathname;
}
Copy link
Member

Choose a reason for hiding this comment

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

Wonder if anyone might mistakenly think this returns a file system “path” they can use?

Oh… and just checked, looks like this is completely unused? Was going to suggest renaming/documenting, but I guess we can just delete it! 😤

Suggested change
export function getCollectionPath(collection: StarlightCollection, srcDir: URL) {
return getCollectionUrl(collection, srcDir).pathname;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Great catch, totally missed that during the refactor 🙈

To explain why this helper is no longer needed after this PR:

  • After splitting collection helpers into 2 files (using fs and not using fs), the getCollectionPath helper was only used in the absolutePathToLang helper.
  • As this was an issue to receive an URL-encoded pathname in the absolutePathToLang helper (which was recently introduced in Fix absolutePathToLang() issue #3298), it was refactored to now accept the docs content collection path as an argument and compare POSIX paths.
  • This new argument is now passed from the new and explicit getCollectionPosixPath helper making getCollectionPath obsolete.

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Perfect — thanks for cleaning that up @HiDeoo! Great call making it a dedicated test suite too.

@delucis delucis added the 🌟 patch Change that triggers a patch release label Jul 29, 2025
@delucis delucis merged commit 10f6fe2 into withastro:main Jul 29, 2025
15 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🌟 core Changes to Starlight’s main package 🌟 patch Change that triggers a patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Astro5 + Starlight + Cloudflare] Cannot build with Starlight and Cloudflare adapter

2 participants