Skip to content

CSS of MIFE not getting imported in root app #87

@imsatyam2111

Description

@imsatyam2111

Describe the bug

I am migrating my CRA + Craco microfrontend app to vite using vite-plugin-single-spa. I have converted the root app as well in vite using the same plugin.

The problem I am facing is css is not being imported when loading my microfrontend into the root app with default built settings.

Project type: [ ] Root Config [✅ ] Micro-frontend/parcel
Project Framework: [ ✅] React [ ] Svelte [ ] Vue [ ] Preact [ ] Other:

To Reproduce

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Also include Vite's configuration (the contents of vite.config.js/ts).

import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import vitePluginSingleSpa from "vite-plugin-single-spa";
import tsconfigPaths from "vite-tsconfig-paths";

export default ({ mode }) => {
  // load app-level env vars to node-level env vars.
  process.env = { ...process.env, ...loadEnv(mode, process.cwd() )};

  return defineConfig({
    plugins: [
      react(),
      tsconfigPaths(),
      vitePluginSingleSpa({
        type: "mife",
        serverPort: 4101,
        spaEntryPoints: "src/app-login.jsx",
      }),
    ],
    base: process.env.VITE_BASE_URL,
    // build: {
    //   rollupOptions: {
    //     output: {
    //       manualChunks: undefined,
    //       inlineDynamicImports: true,
    //     }
    //   }
    // }
  });
}

app-login.jsx

import React from 'react';
import ReactDOMClient from 'react-dom/client';
import singleSpaReact from 'single-spa-react';
import { cssLifecycleFactory } from 'vite-plugin-single-spa/ex';

import App from './App'

const lc = singleSpaReact({
    React,
    ReactDOMClient,
    rootComponent: App,
    errorBoundary() {
        return <div>Error</div>
    }
});

const cssLc = cssLifecycleFactory('app-login');
export const bootstrap = [cssLc.bootstrap, lc.bootstrap];
export const mount = [cssLc.mount, lc.mount];
export const unmount = [cssLc.unmount, lc.unmount];

Expected behavior

CSS should get imported when integrated in root App

Screenshots

dist folder with default build settings
image

browser console
JS got fetched
image

No css for MIFE
image

Browser list:

Browser Name Browser Version Platform
Acme Browser (example row) v1.0.0 Windows x64

Additional context

Root app vite.config.js

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import vitePluginSingleSpa from "vite-plugin-single-spa";

export default defineConfig({
  plugins: [
    react(),
    vitePluginSingleSpa({
      type: "root",
      imo: "2.4.2",
    }),
  ],
});

The interesting thing is when I am adding following piece in the MIFE vite.config.js file, css does get imported in the root app. The difference I see is the default build settings divide the js and css files in chunks, whereas the following build configs, builds the app in a single js file and a single css file.

build: {
   rollupOptions: {
     output: {
          manualChunks: undefined,
          inlineDynamicImports: true,
     }
  }
}

dist folder screenshot
image

Browser console:
CSS does get imported
image

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions