提取css资源


使用插件mini-css-extract-plugin

  1. 需要使用MiniCssExtractPlugin.loader 处理css 资源
  2. MiniCssExtractPlugin 导出

    按引用提取

    1. const path = require("path");
    2. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    3. module.exports = {
    4. target: "web",
    5. mode: "development",
    6. entry: "./src/index.js",
    7. output: {
    8. filename: "build.js",
    9. path: path.resolve(__dirname, "dist"),
    10. },
    11. module: {
    12. rules: [
    13. {
    14. test: /\.css$/,
    15. use: [MiniCssExtractPlugin.loader,, "css-loader"],
    16. },
    17. {
    18. test: /\.s[ac]ss$/i,
    19. use: [
    20. MiniCssExtractPlugin.loader,,
    21. "css-loader",
    22. {
    23. loader: "sass-loader",
    24. options: {
    25. // Prefer `dart-sass`
    26. implementation: require("sass"),
    27. },
    28. },
    29. ],
    30. },
    31. ],
    32. },
    33. plugins: [
    34. new htmlWebpackPlugin({
    35. template: "publish/index.html",
    36. }),
    37. new CleanWebpackPlugin(),
    38. new MiniCssExtractPlugin({ filename: "index.css" }),
    39. ],
    40. };

    提取所有的 CSS 到一个文件中

    用过使用 optimization.splitChunks.cacheGroups 选项,所有的 CSS 可以被提取到一个 CSS 文件中。 ```typescript const MiniCssExtractPlugin = require(“mini-css-extract-plugin”);

module.exports = { optimization: { splitChunks: { cacheGroups: { styles: { name: “styles”, type: “css/mini-extract”, chunks: “all”, enforce: true, }, }, }, }, plugins: [ new MiniCssExtractPlugin({ filename: “[name].css”, }), ], module: { rules: [ { test: /.css$/, use: [MiniCssExtractPlugin.loader, “css-loader”], }, ], }, };

  1. <a name="P3IL5"></a>
  2. ### 基于入口提取 CSS
  3. 你可以基于 webpack 的入口名称提取 CSS。 当你使用路由动态加载但是想要通过入口加载对应的 CSS 文件时这将会非常有用。 这样也避免了 ExtractTextPlugin 造成的 CSS 重复复制问题。
  4. ```typescript
  5. const path = require("path");
  6. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  7. module.exports = {
  8. entry: {
  9. foo: path.resolve(__dirname, "src/foo"),
  10. bar: path.resolve(__dirname, "src/bar"),
  11. },
  12. optimization: {
  13. splitChunks: {
  14. cacheGroups: {
  15. fooStyles: {
  16. type: "css/mini-extract",
  17. name: "styles_foo",
  18. chunks: (chunk) => {
  19. return chunk.name === "foo";
  20. },
  21. enforce: true,
  22. },
  23. barStyles: {
  24. type: "css/mini-extract",
  25. name: "styles_bar",
  26. chunks: (chunk) => {
  27. return chunk.name === "bar";
  28. },
  29. enforce: true,
  30. },
  31. },
  32. },
  33. },
  34. plugins: [
  35. new MiniCssExtractPlugin({
  36. filename: "[name].css",
  37. }),
  38. ],
  39. module: {
  40. rules: [
  41. {
  42. test: /\.css$/,
  43. use: [MiniCssExtractPlugin.loader, "css-loader"],
  44. },
  45. ],
  46. },
  47. };

压缩css资源


压缩静态css资源

使用css-minimizer-plugin
css-minimizer-plugin就是使用cssnano工具来优化压缩css

  1. 需要开启minimize
    1. minimize => 告知 webpack 使用 TerserPlugin 或其它在 optimization.minimizer定义的插件压缩 bundle
  2. 使用CssMinimizerPlugin ```typescript const MiniCssExtractPlugin = require(“mini-css-extract-plugin”); const CssMinimizerPlugin = require(“css-minimizer-webpack-plugin”);

module.exports = { module: { rules: [ { test: /.s?css$/, use: [MiniCssExtractPlugin.loader, “css-loader”, “sass-loader”], }, ], }, optimization: { minimize:true, minimizer: [ …, new CssMinimizerPlugin(), ], }, };

  1. <a name="Aiioh"></a>
  2. ### 压缩Style标签内的 css
  3. 使用插件 html-webpack-plugin<br />属性的minify用 html-minifier-terser 压缩html,其属性minifyCss 使用clean-css 压缩style标签内的css
  4. ```typescript
  5. const HtmlWebpackPlugin = require('html-webpack-plugin');
  6. const path = require('path');
  7. module.exports = {
  8. entry: 'index.js',
  9. output: {
  10. path: path.resolve(__dirname, './dist'),
  11. filename: 'index_bundle.js',
  12. },
  13. plugins: [new HtmlWebpackPlugin({
  14. template:path.resolce(__dirname,'public/index.html'),
  15. minify:{
  16. minifyCSS:true
  17. }
  18. })],
  19. };