-
Notifications
You must be signed in to change notification settings - Fork 59
CSS from federated modules are not loading #154
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
Comments
Check |
What did you setup yours with? The default is |
Are you exposing remote after doing build/preview or in dev mode? |
@rodoabad have you solved this issue? |
@rubiks-cube I'm running @gioboa I haven't resolved this issue. Another issue I'm having and is probably another issue is for some reason, some random tests fail if the build is with |
in preview for remote adding |
@rubiks-cube Let me try that right now. I literally got to office and starting my IDE. LOL. Will bring you results as I go through them. |
Let me share our config. HOST // CORE
import {defineConfig} from 'vite';
import {federation} from '@module-federation/vite';
import react from '@vitejs/plugin-react-swc';
export default defineConfig(({mode}) => {
return {
+ base: 'http://localhost:8080/',
build: {
cssCodeSplit: false, // Error: Unable to preload CSS
minify: false,
modulePreload: false, // Prevents 403/404 errors when preloading in network tab
target: 'esnext'
},
plugins: [
federation({
name: 'host',
remotes: {
PKGA: {
type: 'module', // Set to `var` for Webpack and Rspack federated modules
name: 'PKGA',
entry: mode === 'production' ? '/assets/pkg-a/remote.js' : 'http://localhost:8081/remote.js'
},
PKGB: {
type: 'module', // Set to `var` for Webpack and Rspack federated modules
name: 'PKGB',
entry: mode === 'production' ? '/assets/pkg-b/remote.js' : 'http://localhost:8082/remote.js'
},
},
filename: 'remote.js',
shared: ['react', 'react-dom', 'react-intl', 'react-router-dom']
}),
react()
]
}
}); REMOTES // PKG A
import {defineConfig} from 'vite';
import {federation} from '@module-federation/vite';
import react from '@vitejs/plugin-react-swc';
export default defineConfig({
+ base: 'http://localhost:8081/',
build: {
cssCodeSplit: false, // Error: Unable to preload CSS
minify: false,
modulePreload: false, // Prevents 403/404 errors when preloading in network tab
target: 'esnext'
},
plugins: [
federation({
exposes: {
'./app': './src/app.tsx'
},
filename: 'remote.js',
name: 'PKGA',
shared: ['react', 'react-dom', 'react-intl', 'react-router-dom']
}),
react()
]
}); // PKG B
import {defineConfig} from 'vite';
import {federation} from '@module-federation/vite';
import react from '@vitejs/plugin-react-swc';
export default defineConfig(async ({command}) => ({
+ base: 'http://localhost:8082/',
build: {
cssCodeSplit: false, // Error: Unable to preload CSS
minify: false,
modulePreload: false, // Prevents 403/404 errors when preloading in network tab
target: 'esnext'
},
plugins: [
federation({
exposes: {
'./app': './src/app.tsx'
},
filename: 'remote.js',
name: 'PKGB',
shared: ['react', 'react-dom', 'react-intl', 'react-router-dom']
}),
react()
],
test: {
coverage: {
enabled: true
},
css: {
modules: {
classNameStrategy: 'non-scoped'
}
},
environment: 'happy-dom',
globals: true,
include: ['**/__tests__/unit/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
setupFiles: [
'__tests__/unit/__helpers__/setup-test-framework.ts'
]
}
})); @rubiks-cube With this setup, PKG A and PKG B CSS still doesn't get loaded. |
|
Update! Still doesn't work, @rubiks-cube. I basically replaced |
can u check in network tab in devtools from which location browser is trying to load remote app css? |
can u confirm if PKG A dist folder has CSS files and these CSS files are getting imported in JS files in dist folder? |
![]() @rubiks-cube It's there. |
is |
@rubiks-cube Yup! If I go to |
For me same thing was happening css was loading fine for remote in isolation but not when used inside host. After debugging I find out host was trying to load remote css from its own hostname, updating base fixed for me. Can you check which JS file in dist is importing this CSS file and try debugging that JS file in devtools if it is making request for that CSS? |
Can you share your repo @rubiks-cube ? |
@rubiks-cube , @gioboa I think I HAVE AN IDEA. The suggestion to look at the Note: BTW, if you're not seeing "/assets", I change assets directory to jus ![]() |
Sorry but I am working in private repo of my org which I can't share here |
So yeah, I think that's the problem. If we're not building a |
For me I am not building a |
Nah bruv. Whatever config I have. That's it. |
Im also having problems with the css not being injectedon origin js i didnt have this problem |
@rull3211 do you have a public repo to share? |
@gioboa i can try to make one, its an org repo but i can export it :) |
@gioboa https://github.com/rull3211/demo-cssbug hey this repo reporduces it :) the remote running by it self successfully injects the styling but when fetched with host the stylesheet isnt invoked. I have tried with running the remote both with the server.js and same with the host i have run it both with server, preview and with run dev none of the alternatives work. I have also tried with and without the baseurl. all of the alternatives have worked with origin js if u are wondering. :) If ypu need anything from me you can also contact me on Discord. i appreciate the help cheers! :) |
I tested your app and the problem here is the hot module reload of the remote app. |
Im a bit confused now i dont exactly understand. i have the problem when i serve the host and remote with the node server too o.O what am i doing wrong? OHH snap! i havent tried that lol. yeahh when i serve the remote with npm run dev then the css works. but when i serve it after building the project then the css doesnt get sent with the component |
I'll send you a message on discord |
I seem to have the same problem. Running the vite dev server For now i solved it using this plugin (function() {
"use strict";
try {
if (typeof document != "undefined") {
var elementStyle = document.createElement("style");
elementStyle.appendChild(document.createTextNode(`--- content of build style.css ---`));
document.head.appendChild(elementStyle);
}
} catch (e) {
console.error("vite-plugin-css-injected-by-js", e);
}
})();
import { init } from "./remoteEntry.js";
init(); It would be great if this library could ensure styles are loaded - also preferebly via an option to append styles to the |
@rasmus0201 make sure to remove cssCodeSplitting from your config if you have it that helped for me.
|
@rodoabad Did you solve the original issue? |
@rull3211 I'm not using My vite config looks something like:
Hope this make it clearer and is aligned with the original issue (I think it does, see also this reply #154 (comment)) Appreciate the help! |
@rasmus0201 This is exactly what I highlighted in my previous posts. The host does not care about the other |
@rodoabad Can you share a basic repository with your issue please? I'll look at it |
@gioboa have you tried https://github.com/rull3211/demo-cssbug/tree/main from @rull3211 ? That repo and the repo I'll make will most likely have the same config. It's as basic as it gets. I'll try and make one still though if you want. |
@gioboa I'm also happy to share a repository, if you still need it! Thank you for looking into this issue :) |
@rasmus0201 yep, share your repo please. |
Rull 3211 here from workprofile @michael-gee in your vite.config remove the line cssCodeSplit |
ahh ok, thank you @danielBence - my fix was setting the |
@gioboa I've created a repo here https://github.com/rasmus0201/demo-mf-style-missing STR:
Note that if you run Note that the stylesheet is being compiled to the dist folder, but module federation doesn't reference it anywhere, it would be nice if it could load the stylesheet into the Thank you! |
Same thing here, can't make the style to work. It seems everything that is in the App.css will work, what's in index.css will not. I tried different option with cssCodeSplit and not, with setting different base path, with running dev and building and preview it. Nothing will work. By the way, I got Tailwind by safelisting used class from remote into the host, but now if I want to add a prefix to the remote tailwind classes, it will not work, hence I am trying to have a simple css example work first. |
Got it working. The reason CSS was missing is that Tailwing CSS was setup in index.css and not in the app.css. And as the first one is not getting into the code shared by the remote, but the later does. So I moved @tailwind utilities; in App.css. Left everything else in index.css, as for example @tailwind base will reset the CSS and the host is going to lose some overwrites, if MFE is loaded. Now I get all used Tailwind classes in to the built css that is shared. For collision I used a solution combining a postCss plugin called postcss-prefixer and an utility function that will add a prefix to the css classes in code. (taken from here: https://malcolmkee.com/blog/using-tailwindcss-with-module-federation/ ) |
We are seeing this issue in our code base as well. Tried all solutions mentioned here, but still no luck. |
@Judd2000 build your project and check your 'build/dist' folde, check the css files in there and verify if your css classes are inside. I will start with that. |
Hi, Judd2000 works with me. I had a Demo for a different issue and I have updated it so you can see the reproduction here. If you run You can check the dist directory for each app and find in the assets directory there is a css file that contains the tailwind styles, just search for Also I have tried the above suggestions and have not been able to find a solution that works to resolve this for us. |
Same for me L.E i managed to make it work. similar to some of the comments above: i added the base config and removed cssCodeSplit from each remote |
I tried the module-federation/vite plugin because the @originjs/vite-plugin-federation started to have an serious issue with the cmdk package. At the beginning I also encountered the missing style issue. I solved it by:
No rollupOptions or cssInjectedByJsPlugin needed. But hopefully there wont be a need for this hack in the future. |
Okay, I thought I tried what @PaRoxUs suggested but apparently I did something wrong. Trying again I actually have got things working on my project with just step 1 and 2. Having the import of the styles.css then in a component that is imported with federation. |
I have the same problem, in my case P.S. I also do migration from |
Yeah, you do not need to create an extra component if you import the style to another component you host. I use a StyleProvider component where I import the styles to also wrap components and pages so that the correct styles get applied and to make sure portals are rendered within the styling scope. |
related #257 |
still waiting for the solution |
Just want to share a solution for my case below:
My solution
plugins: {/* ... */},
build:{
/* build otpions*/
},
experimental: {
renderBuiltUrl(filename, { hostType }) {
if (hostType === "js") {
// For JS files, use runtime code similar to getPublicPath
return {
runtime: `window.__REMOTE_HOST__? (window.__REMOTE_HOST__ + "${filename}") : "/${filename}"`,
};
}
// For other file types (css, html), use relative paths
return { relative: true };
},
},
....
|
I tried multiple permutations from this thread and eventually got it work with these build config:
make sure you remove |
If you have CSS files in your federated module, they do not get loaded in run time (although they do get built).
The text was updated successfully, but these errors were encountered: