MIME type ('text/html') is not a supported stylesheet MIME type

前端 未结 4 1625
囚心锁ツ
囚心锁ツ 2021-02-19 16:08

I have tried almost every solution for the problem which includes. giving type for use of app.use(expre

4条回答
  •  广开言路
    2021-02-19 16:30

    I believe that the issue is due to the HtmlWebpackPlugin not providing the mimetype for the css file that has been injected with the MiniCssExtractPlugin. I've managed to solve the problem by using the HtmlWebpackLinkTypePlugin which should insert the mimetype into the css file.

    /// top of file
    const LinkTypePlugin = require('html-webpack-link-type-plugin').HtmlWebpackLinkTypePlugin;
    
    ....
    
    const plugins = [
      new CleanWebpackPlugin({
        root: __dirname,
        verbose: true,
        dry: false
      }),
      new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"production"' }),
      new MiniCssExtractPlugin({ filename: "style.css", allChunks: false }),
      new CopyWebpackPlugin([
        { from: './src/index.html', to: 'index.html' },
      ]),
      new webpack.ProvidePlugin({
        Promise: 'es6-promise-promise',
        'React': 'react'
      }),
      new HtmlWebpackPlugin({
        template: './src/index.html'
      }),
      new LinkTypePlugin({
        '*.css' : 'text/css'
      })
    ];
    

    This should make your injected stylesheet line look like this:

    
    

    Just a note that the *.css rule is a glob, so if your css file is hosted in a directory under your root, you will need to add something like **/*.css as your rule.

    I hope this answers your question.

提交回复
热议问题