stylex icon indicating copy to clipboard operation
stylex copied to clipboard

[Webpack plugin] CSS file is not getting attached to html template.

Open omkar-nath opened this issue 2 years ago • 5 comments

The problem I am not using create-react-app and trying with webpack configuration in a dummy react project. I am trying StyleX with my webpack configured dummy react application. My webpack config looks like below.

const StylexPlugin = require("@stylexjs/webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
function config(env, argv) {
  return {
    entry: {
      main: "./src/index.js",
    },
    output: {
      path: path.resolve(__dirname, "build"),
      filename: "[name].js",
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader",
            options: {
              presets: ["@babel/preset-env", "@babel/preset-react"],
            },
          },
        },

        {
          test: /\.css$/i,
          use: [MiniCssExtractPlugin.loader, "css-loader"],
        },
      ],
    },

    plugins: [
      // Ensure that the stylex plugin is used before Babel
      new StylexPlugin({
        filename: "omkar.css",
        // get webpack mode and set value for dev
        dev: argv.mode === "development",

        // Use statically generated CSS files and not runtime injected CSS.
        // Even in development.
        // optional. default: 'x'
        classNamePrefix: "x-om",
        // Required for CSS variable support
        unstable_moduleResolution: {
          // type: 'commonJS' | 'haste'
          // default: 'commonJS'
          type: "commonJS",
          // The absolute path to the root directory of your project
          rootDir: path.join("__dirname"),
        },
      }),
      new MiniCssExtractPlugin(),
      new HtmlWebpackPlugin({
        inject: "head",
        template: path.join(__dirname, "public", "index.html"),
      }),
    ],
  };
}
module.exports = config;

When I am doing production build. I can see seperate CSS file generated but it is not attached to the index file . Am I missing something?

Expected behavior

The html file under build should be having css file attached to it.

Steps to reproduce Clone: https://github.com/omkar-nath/styleX-react/blob/master/webpack.config.js Run: npm run build

Environment (include versions). Did this work in previous versions?

omkar-nath avatar Dec 12 '23 07:12 omkar-nath

When I am doing production build. I can see seperate CSS file generated but it is not attached to the index file . Am I missing something?

The StyleX Webpack plugin only emits a CSS file. It doesn't inject it into the HTML file for you. This is intentional because we expect you to be using a component framework to render your markup and not use a static HTML file.

nmn avatar Dec 12 '23 08:12 nmn

hey @nmn, I've a dumb question. Since stylex is emitting CSS file and not injected to static HTML file. Does this mean, only next.js and similar frameworks are supported?

@omkar-nath to solve your problem, you have to manually update your source index.html file to include css link . Example:

<head>
  <link rel="stylesheet" type="text/css" href="./stylex.css" />
</head>

techdiary avatar Dec 22 '23 07:12 techdiary

Since stylex is emitting CSS file and not injected to static HTML file. Does this mean, only next.js and similar frameworks are supported?

Since we're so new our bundler integrations are still experimental, but you should really be able to use StyleX in any JS-based component framework. This includes things like create-react-app or client-rendered apps with Solid, Vue or Svelte.

For now you'll have to find ways to integrate the compiler yourself. We're working with the community to make the bundler plugins better.

For now, the recommendation is to add the link tag manually as in your comment.

nmn avatar Dec 22 '23 11:12 nmn

Thank you Naman for clarifying. I'll try to look into html-webpack-plugin and see if it can create the link to the stylesx css file.

techdiary avatar Dec 25 '23 13:12 techdiary

Even though this is closed, and I understand this is not really an issue with stylex, I was nevertheless wondering how this could be worked around in the case where the name contains [contenthash], something that cannot be hardcoded, and that is necessary to play nice with caching layers one might run into.

deadalnix avatar Feb 08 '24 11:02 deadalnix