vite icon indicating copy to clipboard operation
vite copied to clipboard

CSS from federated modules are not loading

Open rodoabad opened this issue 1 year ago • 59 comments

If you have CSS files in your federated module, they do not get loaded in run time (although they do get built).

rodoabad avatar Oct 10 '24 14:10 rodoabad

Check base value in your vite config. Faced similar issue this fixed for me.

rubiks-cube avatar Oct 10 '24 17:10 rubiks-cube

Check base value in your vite config. Faced similar issue this fixed for me.

What did you setup yours with? The default is / which is why I just left mine as is.

rodoabad avatar Oct 10 '24 20:10 rodoabad

Are you exposing remote after doing build/preview or in dev mode?

rubiks-cube avatar Oct 10 '24 20:10 rubiks-cube

@rodoabad have you solved this issue?

gioboa avatar Oct 12 '24 07:10 gioboa

@rubiks-cube I'm running vite preview on both host and remote.

@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 federation and if I remove it from plugins, it passes. I will try and create a test repository outside of our private organization .

rodoabad avatar Oct 14 '24 13:10 rodoabad

in preview for remote adding base to hostname for e.g. http://localhost:5173/ resolved my issue for css

rubiks-cube avatar Oct 14 '24 14:10 rubiks-cube

in preview adding base to hostname for e.g. http://localhost:5173/ resolved my issue for css

@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.

rodoabad avatar Oct 14 '24 14:10 rodoabad

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.

rodoabad avatar Oct 14 '24 14:10 rodoabad

entry: mode === 'production' ? '/assets/pkg-a/remote.js' : 'http://localhost:8081/remote.js' in preview (production mode) host will fetch remoteentry from '/assets/pkg-a/remote.js'. I think in this case css for remote is trying to load from http://localhost:8080/. Can you add hostname for remote in which preview is running like you have done for development mode in host config?

rubiks-cube avatar Oct 15 '24 18:10 rubiks-cube

entry: mode === 'production' ? '/assets/pkg-a/remote.js' : 'http://localhost:8081/remote.js' in preview (production mode) host will fetch remoteentry from '/assets/pkg-a/remote.js'. I think in this case css for remote is trying to load from http://localhost:8080/. Can you add hostname for remote in which preview is running like you have done for development mode in host config?

Update! Still doesn't work, @rubiks-cube. I basically replaced entry with just http://localhost:8081/remote.js and http://localhost:8082/remote.js respectively for PKG A and PKG B, since I'm running local servers for preview.

rodoabad avatar Oct 15 '24 19:10 rodoabad

can u check in network tab in devtools from which location browser is trying to load remote app css?

rubiks-cube avatar Oct 15 '24 19:10 rubiks-cube

can u check in network tab in devtools from which location browser is trying to load remote app css?

It's not requesting any of the CSS files.

demo

Notice in PKG A and PKG B the CSS is messed up? That's the missing CSS not loading.

PKG A for example is supposed to look like this.

Screenshot 2024-10-15 at 2 20 14 PM

rodoabad avatar Oct 15 '24 19:10 rodoabad

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 avatar Oct 15 '24 19:10 rubiks-cube

Screenshot 2024-10-15 at 2 38 13 PM

@rubiks-cube It's there.

rodoabad avatar Oct 15 '24 19:10 rodoabad

is style-CpnI5EpU.css being imported in any of JS files in this dist folder?

rubiks-cube avatar Oct 15 '24 19:10 rubiks-cube

@rubiks-cube Yup! Screenshot 2024-10-15 at 2 42 52 PM

If I go to localhost:8081/pkg-a I see the CSS loading.

rodoabad avatar Oct 15 '24 19:10 rodoabad

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?

rubiks-cube avatar Oct 15 '24 19:10 rubiks-cube

Can you share your repo @rubiks-cube ? This will definitely help

gioboa avatar Oct 15 '24 20:10 gioboa

@rubiks-cube , @gioboa I think I HAVE AN IDEA. The suggestion to look at the dist to actually check if it's being loaded was an awesome suggestion. Because, when I looked, it was being loaded in the development index.html file for PKG A. That file is never loaded in CORE. CORE has it's own index.html file.

Note: BTW, if you're not seeing "/assets", I change assets directory to jus ..

Screenshot 2024-10-15 at 3 04 12 PM

rodoabad avatar Oct 15 '24 20:10 rodoabad

Sorry but I am working in private repo of my org which I can't share here

rubiks-cube avatar Oct 15 '24 20:10 rubiks-cube

So yeah, I think that's the problem. If we're not building a lib, Vite needs an index.thml and if we are importing CSS, it will add that style tag in the index.html of the package. However, since it's federated, it won't be using the index.html from PKG A. It will however use the index.html from CORE. But Core is being built elsewhere and has no idea it needs to add that CSS file, unless PKG A will add it later on.

rodoabad avatar Oct 15 '24 20:10 rodoabad

For me I am not building a lib but still css doesn't get injected by default in index.html. are u using some plugin that is doing that?

rubiks-cube avatar Oct 15 '24 20:10 rubiks-cube

For me I am not building a lib but still css doesn't get injected by default in index.html. are u using some plugin that is doing that?

Nah bruv. Whatever config I have. That's it.

rodoabad avatar Oct 15 '24 20:10 rodoabad

Im also having problems with the css not being injectedon origin js i didnt have this problem

rull3211 avatar Oct 22 '24 06:10 rull3211

@rull3211 do you have a public repo to share?

gioboa avatar Oct 22 '24 07:10 gioboa

@gioboa i can try to make one, its an org repo but i can export it :)

rull3211 avatar Oct 22 '24 07:10 rull3211

@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 npm run preview:server and with running just the normal preview.

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! :)

rull3211 avatar Oct 22 '24 11:10 rull3211

I tested your app and the problem here is the hot module reload of the remote app. If I change the scss of the remote and then I refresh the host everything is working fine. The remote app is working great with HMR too. so it's a different issue.

gioboa avatar Oct 22 '24 12:10 gioboa

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

rull3211 avatar Oct 22 '24 12:10 rull3211

I'll send you a message on discord

gioboa avatar Oct 22 '24 12:10 gioboa