[使用Webpack将样式表链接动态添加到html

问题描述 投票:1回答:1

想在使用mini-css-extract-plugin后运行webpack,以便在html文件的标题中创建适当的样式表链接,类似于html-webpack-plugin添加适当的JS包(脚本标记的方式)转换成html。是否可以让webpack +插件执行此操作,还是我必须手动粘贴样式表标签(在html模板中)?

正在运行webpack 4.28

webpack html-webpack-plugin mini-css-extract-plugin
1个回答
0
投票

我相信此webpack配置应能解决您的问题。

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

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new HtmlWebpackPlugin({
        template: './src/index.html',
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css',
    }),
  ],
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
            { loader: MiniCssExtractPlugin.loader },
            'css-loader',
            'sass-loader'
        ],
      },
    ],
  },
};
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.