question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webpack html-loader and MiniCssExtractPlugin

See original GitHub issue

I’m using setup from getting-started webpack page:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  devtool: 'inline-source-map',
  devServer: {
    contentBase: './dist',
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Development',
      template: 'src/index.html'
    }),
    new MiniCssExtractPlugin(),
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true //clean dist folder before each build
  },
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: 'html-loader',
      },
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif|ico)$/i,
        type: 'asset/resource',
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
      },
    ],
  },
};

And this one works OK if I include import './style.css'; at the top of src/index.js. Inside of the produced dest/index.html I get the line where the extracted CSS style is generated as <link href="main.css" rel="stylesheet">.

Now what I want is to remove that line import './style.css'; at the top of src/index.js and use instead of that one <link rel="stylesheet" type="text/css" href="style.css"> that I will place inside the template src/index.html.

When doing this, generated dest/index.html gets correctly the line <link rel="stylesheet" type="text/css" href="b88d04fba731603756b1.css"> and the file dest/b88d04fba731603756b1.css is generated, but it’s content is wrong as I get this instead of real css styles:

// extracted by mini-css-extract-plugin
export {};

Is there a way to use html-loader plugin together with MiniCssExtractPlugin, so that I do not need to import css inside js files but instead import it inside html template?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
bojanv55commented, Jul 16, 2021
  • Operating System: Windows 10
  • Node Version: v14.15.0
  • NPM Version:6.14.8
  • webpack Version:^5.44.0
  • mini-css-extract-plugin Version:^2.1.0
  • html-loader Version:^2.1.2

Expected Behavior

file dest/index.html is generated and links to <link rel="stylesheet" type="text/css" href="b88d04fba731603756b1.css"> and inside that css file there is following content

.loader{
 background-color: red;
}

Actual Behavior

file dest/index.html is generated and links to <link rel="stylesheet" type="text/css" href="b88d04fba731603756b1.css"> and inside that css file there is following content

// extracted by mini-css-extract-plugin
export {};

Code

Gist

0reactions
KleinChiucommented, Jul 19, 2022

my solution, partial of my webpack.config.js to exclude prime vue from my project

      {
        test: /\.css$/,
        exclude: /prime(vue|icons).+\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"],
      },
      {
        test: /prime(vue|icons).+\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: { emit: false, esModule: false },
          },
          "css-loader",
        ],
      },
Read more comments on GitHub >

github_iconTop Results From Across the Web

MiniCssExtractPlugin - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
webpack html-loader and MiniCssExtractPlugin - Stack Overflow
I'm using setup from getting-started webpack page: const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); ...
Read more >
How to use the mini-css-extract-plugin function in mini ... - Snyk
To help you get started, we've selected a few mini-css-extract-plugin examples, based on popular ways it is used in public projects.
Read more >
Webpack: Part-3 - Suprabha Supi - Medium
Html-loader ; File-loader; Clean-webpack; Multiple Entrypoints & Vendor.js ... It can be doable using plugin called mini-css-extract-plugin.
Read more >
mini-css-extract-plugin - npm
Start using mini-css-extract-plugin in your project by running `npm i ... Repository. github.com/webpack-contrib/mini-css-extract-plugin ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found