Is there a possibility of converting react css modules to inline css on production

788 views Asked by At

This may be an unusual question. I want to explore the possibility of converting CSS/SCSS files into inline with HTML on production mode.

import styles from "./index.module.scss";

function Welcome(props) {
 return <h1 className={styles.title}>Hello, Doe</h1>;
}

==== This will result something like this ======

  <html>
    <Head>
      <link rel="stylesheet" href="/_next/static/style.css" />
    </Head>
    <body>
      <h1 className="style_title3dwe">Hello, Doe</h1>
    </body>
  </html>

==== What i expect have on production is ======

 <html>
    <Head>
      <style>
         .style_title3dwe { font-size: var(--fs1); }
      </style>
    </Head>
    <body>
      <h1 className="style_title3dwe">Hello, Doe</h1>
    </body>
  </html>
1

There are 1 answers

0
Garvae On

style-loader documentation may help you

You can pass the injectType you need:

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: [
          // The `injectType`  option can be avoided because it is default behaviour
          { loader: "style-loader", options: { injectType: "styleTag" } },
          "css-loader",
        ],
      },
    ],
  },
};

injectType can be one of the following values::

type injectType =
  | "styleTag"
  | "singletonStyleTag"
  | "autoStyleTag"
  | "lazyStyleTag"
  | "lazySingletonStyleTag"
  | "lazyAutoStyleTag"
  | "linkTag";

P.S. In my current environment without setting up the style-loader, it works exactly as you described - added <style> tags inside <head> Codesandbox