Skip to content

fix: prepend assetsDir to bare CSS filenames in dynamicLoadingCss - #750

Open
jeff-ofobrukweta wants to merge 1 commit into
originjs:mainfrom
jeff-ofobrukweta:fix/css-assets-dir-bare-filename
Open

jeff-ofobrukweta wants to merge 1 commit into
originjs:mainfrom
jeff-ofobrukweta:fix/css-assets-dir-bare-filename

Conversation

@jeff-ofobrukweta

Copy link
Copy Markdown

Summary

dynamicLoadingCss() (the runtime helper injected into remoteEntry.js for exposed modules) reads build.assetsDir into an assetsDir variable, but never actually uses it when composing the stylesheet <link> href for an exposed module's CSS. It joins base straight onto the bare CSS filename, dropping the assets directory the file is actually emitted into.

The CSS filenames fed into this array are always bare filenames (no path segments) — see basename(cssBundle.fileName) at expose-production.ts:225 and :264 — so whenever build.assetsDir is anything other than the project root (i.e. basically always, since Vite defaults it to "assets"), every request for a federated module's CSS 404s in production.

Fix

Prepend assetsDir to cssPath before it's cleaned/joined into the href, but only when cssPath has no / in it, so any already-qualified path (e.g. an absolute/CDN-hosted CSS URL) is left untouched.

 const cleanBaseUrl = trimmer.trailing(baseUrl);
-const cleanCssPath = trimmer.leading(cssPath);
+const cssPathWithAssetsDir = assetsDir && !cssPath.includes('/') ? assetsDir + '/' + cssPath : cssPath;
+const cleanCssPath = trimmer.leading(cssPathWithAssetsDir);
 const cleanCurUrl = trimmer.trailing(curUrl);

Verification

  • Confirmed via live browser testing in a production app using this plugin: with a non-default/nested build.assetsDir, every federated screen rendered completely unstyled with CSS requests 404ing; all resolved after this fix.
  • pnpm --filter @originjs/vite-plugin-federation run build succeeds and the generated dist/index.js/dist/index.mjs output the corrected href-composition logic.

This is a small, narrowly-scoped one-line-of-logic fix to a real production bug in the current release (1.4.1) with no newer version available.

dynamicLoadingCss() reads build.assetsDir into an `assetsDir` variable
but never used it when composing the stylesheet href for an exposed
module's CSS -- it joined `base` straight onto the bare CSS filename
(the runtime array is always populated with basename()'d entries, see
expose-production.ts:225 and :264), dropping the assets directory the
file is actually emitted into. Every request for a federated module's
CSS then 404s in production whenever build.assetsDir differs from the
project root (e.g. any non-default assetsDir, or a nested one).

Only prepend assetsDir when cssPath has no path separator, so already
-qualified paths (e.g. CDN-hosted CSS) are left untouched.
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.

1 participant