1

我正在使用入门 webpack 页面中的设置:

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',
      },
    ],
  },
};

如果我包含import './style.css';src/index.js. 在生成的内部,dest/index.html我得到提取的 CSS 样式生成为<link href="main.css" rel="stylesheet">.

现在我想要的是删除import './style.css';顶部的那一行src/index.js并使用而不是<link rel="stylesheet" type="text/css" href="style.css">我将放置在模板中的那一行src/index.html

执行此操作时, generateddest/index.html会正确获取该行<link rel="stylesheet" type="text/css" href="b88d04fba731603756b1.css">dest/b88d04fba731603756b1.css生成文件,但它的内容是错误的,因为我得到的是这个而不是真正的 css 样式:

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

有没有办法将html-loader插件与 一起使用MiniCssExtractPlugin,这样我就不需要在 js 文件中导入 css 而是在 html 模板中导入它?

4

0 回答 0