After webpack is packaged, the CSS style is not referenced.

my project directory structure

.
 dist
  index.html
  main.bundle.js
 node_modules
 package-lock.json
 package.json
 src
  css
   style.css
  index.html
  js
      main.js
 webpack.config.js

src index.html files

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Webpack DEMO</title>
  <link rel="stylesheet" href="./css/style.css">
</head>
<body>
  <h1 class="title">Webpack DEMO</h1>

  <script src="./js/main.js"></script>
</body>
</html>

src style.css files

*{margin:0;padding:0;}
html, body {
  height:100%;
}
body {
  background-color:-sharp000;
}
.title {
  display:inline-block;
  margin:30px;
  line-height:1.5;
  font-size:36px;
  color:-sharpFFF;
}

webpack.config.js the contents of the file are as follows

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

module.exports = {
  entry:"./src/js/main.js",
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "dist")
  },
  module: {
    rules: [
      {
        test:/\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader
          },
          "style-loader",
           "css-loader"
        ]
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin("dist"),
    new webpack.HotModuleReplacementPlugin(),
    new HtmlWebpackPlugin({
      hash: true
    }),
    new MiniCssExtractPlugin({
      filename: "./dist/[name].[hash].css"
    })
  ],
  devServer: {
    contentBase: path.resolve(__dirname, "src"),
    hot: true,
    inline:true
  },
};

generate index.html files

in the dist directory after packaging.
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
  </head>
  <body>
  <script type="text/javascript" src="main.bundle.js?66b744cd3fcfe1839a99"></script></body>
</html>

Why is the style referenced in the original page missing after packaging?

what I want is that after each package, like the JS script, the style is automatically introduced with hash . How can this be achieved?

Dec.12,2021

loader is similar to pipeline operations, where each loader performs specific tasks and provides input and output.
according to the code you mentioned, the loder is executed in the following order: css-loader --> style-loader --> MiniCssExtractPlugin.loader .

css-loader : use import and require in the js code to import the css file, and if the css file contains @ import and url () these two statements need to be processed by css-loader to output the parsing result.

style-loader : create the style dom element with the input result, and the code to create the dom will be packaged in the js bundle.

MiniCssExtractPlugin.loader : separate the input result into a css file and compress it.

it can be understood that style-loader and MiniCssExtractPlugin.loader only receive the input provided by the previous loader, but do not provide output for the next loader. So after style-loader is executed, MiniCssExtractPlugin.loader does not detach and compress the css file.

use style-loader in development mode and MiniCssExtractPlugin.loader in production environment to detach css files.

here are the changes made to webpack.conf.js:

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

// 
module.exports = (env, arg) => {
  // 
  const devMode = argv.mode === 'development';

  let config = {
    entry:'./src/js/main.js',
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
        {
            test:/\.css$/,
            use: [
            {
                loader: devMode ? 'style-loader': MiniCssExtractPlugin.loader
            },
            'css-loader'
            ]
        }
        ]
    },
    plugins: [
        new CleanWebpackPlugin('dist'),
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({
        hash: true
        }),
        new MiniCssExtractPlugin({
          filename: devMode ? '[name].css' : '[name].[hash].css',
          chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
        })
    ],
    devServer: {
        contentBase: path.resolve(__dirname, 'src'),
        hot: true,
        inline:true
    }
  }

  return config
};
  • The problem of css scope in vue

    css file mode import . index.less is introduced into vue. After being packaged in this way, all css are global styles. Is there any way to solve this problem? if style tags are officially recommended to add scope, css encounters a problem that css...

    Mar.12,2021
Menu