Webpack Mini CSS Extract Plugin - 提取 CSS 有什么意义?

标签 webpack webpack-style-loader

我正在使用 Mini CSS Extract Plugin在我的 dist 中将我的 CSS 拆分成它自己的文件我的 webpack 项目的文件夹。

我的 webpack.config.js文件如下所示:

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

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "style.css"
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader"
        ]
      }
    ]
  }
};

当我npm run build ,这将构建文件 style.css在我的 dist文件夹,与我的 style.css 相同文件在我的 src文件夹。我必须包括
<head>
  ...
  <link rel="stylesheet" type="text/css" href="style.css" />
</head> 

index.html的头部在 dist文件夹以使其工作。所以我的问题是 - 像这样提取 CSS 有什么意义? 如果style.css src 中的文件文件夹与 style.css 相同dist 中的文件文件夹,我必须包括
<link rel="stylesheet" type="text/css" href="style.css" />

在我的 HTML 的头部,我不明白这样做的好处是什么,即使 Webpack Documentation状态

Note that you can, and in most cases should, split your CSS for better load times in production.

最佳答案

这允许缓存 CSS。
您还可以获得无样式内容 (FOUC) 的 Flash。 FOUC 的发生是因为浏览器需要一段时间来加载 JavaScript,并且样式才会在那时被应用。
通过让浏览器单独管理它,将 CSS 分离到它自己的文件可以避免这个问题。

关于Webpack Mini CSS Extract Plugin - 提取 CSS 有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49330009/

相关文章:

.net - Azure 部署 .NET 应用部署脚本

webpack - 导入光滑轮播主题CSS会在Webpack构建上引发错误

javascript - 从使用 React 和 Webpack 构建的网站的 url 中删除 .html 扩展名

node.js - 使用 webpack 构建 (React) 同构 web 应用程序的服务器部分,包括 CSS 样式加载器

css - 无法让 CSS 模块在 Webpack 中工作

css - Webpack - 如何进行简单的 css 缩小和连接

webpack - 使用 webpack 在特定文件夹中加载 css url() 文件

css - Reactjs webpack 配置问题

laravel - 使用 Mix/Webpack 将 quill.js 导入 Laravel

javascript - 在 react 中使用导入的 css 文件的 css 样式上的属性值无效