0

I am working on a personal ReactJS project and have created two projects using Module Federation.

In the main project, I added some CSS which is affecting the remote project. After Googling, I found that a better way to handle this is by using Shadow DOM, which isolates the remote component.

However, while implementing the Shadow DOM, I'm unable to inject or apply SCSS styles to the elements.

I am using Create React App with CRACO, and I have included the craco.config.js file below.

Does anyone have any ideas on how to inject SCSS?

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './components/App';
import './index.scss';
// Get the root container
const container = document.getElementById('root') as HTMLElement;
// Create the Shadow Root
const shadowRoot = container.attachShadow({ mode: 'open' });
// Create an app container inside Shadow DOM
const shadowAppContainer = document.createElement('div');
shadowRoot.appendChild(shadowAppContainer);
// Mount React into the shadow root
const root = ReactDOM.createRoot(shadowAppContainer);
root.render(
 <React.StrictMode>
 <App />
 </React.StrictMode>
);

Craco.config.js:

module.exports = function () {
 const env = process.env.ENV || 'local';
 return {
 plugins: [
 {
 plugin: require('./build/craco'),
 },
 ],
 style: {
 sass: {
 loaderOptions: {
 // Prefer 'sass' (dart-sass) over 'node-sass' if both packages are installed.
 implementation: require('sass'),
 // Workaround for this bug: https://github.com/webpack-contrib/sass-loader/issues/804
 webpackImporter: false,
 },
 },
 },
 webpack: {
 plugins: {
 remove: ['ModuleScopePlugin'],
 },
 },
 };
};
Peter B
24.7k5 gold badges42 silver badges94 bronze badges
asked May 15, 2025 at 10:33
4
  • I'm not to familiar with these import "style.scss" imports, but what exactly would that do? Append the style to the page? There is no object you load here, so there is no way to make it apply to the ShadowRoot from this point. Now if the imported object is <style> tag you could append it, or if it's a CSSStyleSheet you could adopt it in your ShadowRoot. But from this it seems the style might just be part of the document, which would not cascade into your ShadowRoot. Commented May 15, 2025 at 11:14
  • Don't use "Create React App", it's EOL. A better alternative is Vite. Especially if this is a newer project. Commented May 15, 2025 at 11:39
  • What it does is inject all the CSS into the <head> of the document so that it can be used globally. However, since Shadow DOM does not inherit styles from the main document, those styles are not applied inside the shadow root. Do you happen to know if there's a way to inject the styles directly into the shadow DOM? @somethinghere Commented May 15, 2025 at 14:05
  • 1
    It really depends on what the import returns, if it uses some HTML spec (like say import Style from "style.css" with { type: "stylesheet" } then the browser usually returns a CSSStyleSheet which you can adopt in your shadowRoot with .shadowRoot.adoptedStyleSheets = [ Style ]. But I'm not all to familiar with how all the webpack/rollup/react stuff handles this... Commented May 16, 2025 at 8:05

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.