0

I am using webpack to bundle my web application project. The webpack configuration looks as following:

const HtmlWebPackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const autoprefixer = require('autoprefixer');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  entry: ["./src/index.js"],
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.[hash].js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"]
          }
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader",
            options: {
              minimize: true
            }
          }
        ]
      },
      {
        test: /\.(sass|scss)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "style.css"
            }
          },
          { loader: "extract-loader" },
          { loader: "css-loader",
            options: {
              minimize: true
            }
          },
          {
            loader: "postcss-loader",
            options: {
              plugins: () => [autoprefixer({ grid: false })]
            }
          },
          {
            loader: "sass-loader",
            options: {
              includePaths: ["./node_modules"]
            }
          }
        ]
      },
      {
        test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name][hash].[ext]",
              outputPath: "fonts/"
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./public/index.html",
      filename: "./index.html"
    }),
    new CopyWebpackPlugin([
      {
        from: "public"
      }
    ]),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
  ]
};

The SCSS module, I am using file-loader to generate style.css file. The content of the index.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <link rel="stylesheet" href="main.css">

    <title>Cockpit</title>

</head>

<body class="mdc-typography">


    <button class="mdc-button">
        Button
    </button>

</body>

</html>  

As you can see, the stylesheet file is hardcoded, but I want that it inserts automatically into index.html. How to do it with html-webpack-plugin?

softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

0

Try using HtmlWebpackIncludeAssetsPlugin.

plugins: [
  new HtmlWebPackPlugin({
    template: "./public/index.html",
    filename: "./index.html"
  }),
  new CopyWebpackPlugin([
    {
      from: "public"
    }
  ]),
  new HtmlWebpackIncludeAssetsPlugin({
    assets: ['style.css'],
    append: true
  }),
  new MiniCssExtractPlugin({
    filename: "[name].css",
    chunkFilename: "[id].css"
  })
]

Many, many more options are possible, see here: https://github.com/jharris4/html-webpack-include-assets-plugin

MiB
  • 390
  • 1
  • 2
  • 15