0

In Electron-React-Webpack app I'm getting this error message:

web:1 Refused to apply style from 'http://localhost:3000/assets/css/icons/iconfont.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled

enter image description here

enter image description here

enter image description here

enter image description here

src/app$ nano index_web.html

src/assets/css/icons$ ls -lah
total 244K
drwxrwxr-x 2 raphy raphy 4,0K feb 14 18:02 .
drwxrwxr-x 4 raphy raphy 4,0K feb 14 18:02 ..
-rw-rw-r-- 1 raphy raphy  92K feb 14 18:02 iconfont.css
-rw-rw-r-- 1 raphy raphy 143K feb 14 18:02 iconfont.woff2


.webpack/assets/icons$ ls -lah
total 244K
drwxrwxr-x 2 raphy raphy 4,0K feb 15 10:31 .
drwxrwxr-x 4 raphy raphy 4,0K feb 15 10:31 ..
-rw-rw-r-- 1 raphy raphy  92K feb 15 10:31 iconfont.css
-rw-rw-r-- 1 raphy raphy 143K feb 15 10:31 iconfont.woff2

What's wrong with my configuration? :

webpack.plugins.js :

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CopyPlugin = require("copy-webpack-plugin")
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');


module.exports = [
  new ForkTsCheckerWebpackPlugin(),

  new CopyPlugin({
    patterns: [
      {
        from: path.resolve(__dirname, "./src/data"),
        to: path.resolve(__dirname, ".webpack/data")
      },

      {
        from: path.resolve(__dirname, "./src/assets/css"),
        to: path.resolve(__dirname, ".webpack/assets")
      }

    ]
  }),

  // https://reposhub.com/javascript/css/webpack-contrib-sass-loader.html
  // https://www.developerhandbook.com/webpack/how-to-configure-scss-modules-for-webpack/

  new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: process.env.NODE_ENV !== 'production' ? '[name].css' : '[name].[hash].css',
      chunkFilename: process.env.NODE_ENV !== 'production' ? '[id].css' : '[id].[hash].css'

  })

];

In webpack.rules.js :

  {
    test: /\.pcss$/,
    use: [
      {
        loader: 'style-loader',
      },
      {
        loader: 'postcss-loader',
        options: {
          sourceMap: true,
        },
      },
    ],
  },

  {
    test: /\.(?:ico|gif|png|jpg|jpeg|webp)$/,
    use: 'url-loader',
  },

  {
    test: /\.s(a|c)ss$/,
    exclude: /\.module.(s(a|c)ss)$/,
    use: [
      process.env.NODE_ENV !== 'production'
        ? 'style-loader' // fallback to style-loader in development. Creates `style` nodes from JS strings
        : MiniCssExtractPlugin.loader,
      'css-loader', // Translates CSS into CommonJS
      {
        loader: 'sass-loader', // Compiles Sass to CSS
        options: {
          sourceMap: process.env.NODE_ENV !== 'production'
        }
      }
    ]
  },

In the html file:

<link rel="stylesheet" href="../assets/css/icons/iconfont.css" />

In package.json :

"devContentSecurityPolicy"

Update 1)

Following the indications found here : Stylesheet not loaded because of MIME type

I've removed the .. of the relative path:

From

<link type="stylesheet" href="../assets/css/icon/iconfont.css" />

To

<link type="stylesheet" href="/assets/css/icon/iconfont.css" />

The error message disappeared, but I'm not that sure the css file is correctly loaded ....

How to make the app correctly loading and display the css ?

Raphael10
  • 2,508
  • 7
  • 22
  • 50
  • can you check in the browser what comes up if you navigate directly to the css-file? do you see the css-file? this error often comes up because the server returns some 404-HTML, which isn't CSS. – niorad Feb 15 '23 at 09:28
  • Hi ! @niorad I updated the post above with some screenshots and folder's content. It seems that it does not find the `css` file, even if it is actually present – Raphael10 Feb 15 '23 at 09:46
  • @niorad What does that "Status Code 404" (screenshot above) mean? – Raphael10 Feb 15 '23 at 11:29
  • 1
    404 = file not found – niorad Feb 15 '23 at 14:31

1 Answers1

0

I removed the link rel in the HTML file:

<link rel="stylesheet" href="../assets/css/icons/iconfont.css" />

and in React page I imported the css file:

import '../../../../assets/css/icons/iconfont.css'

Now the css seems working

Raphael10
  • 2,508
  • 7
  • 22
  • 50