处理样式资源


以scss 为例

需要的依赖

  • style-loader
  • css-loader
  • postcss-loader
  • sass-loader

下载loader

  1. yarn add style-loader css-loader sass-loader sass -D

webpack.config.js

  1. module: {
  2. rules: [
  3. {
  4. test: /\.css$/,
  5. use: [
  6. "style-loader",
  7. "css-loader",
  8. ],
  9. },
  10. {
  11. test: /\.s[ac]ss$/i,
  12. use: [
  13. "style-loader",
  14. "css-loader",
  15. {
  16. loader: "sass-loader",
  17. },
  18. ],
  19. },
  20. ],
  21. },

css的兼容性


需要的依赖

  1. postcss-loader
  2. posctcss
  3. pocstcss-preset-env
    1. yarn add posctcss-loader postcss postcss-preset-env -D

    postcss.config.js

    1. module.exports = {
    2. plugins: ['postcss-preset-env'],
    3. };

    .browserslistsrc

    ```javascript

last 2 version

1% IE 10 # sorry

  1. <a name="C6bsf"></a>
  2. ### webpapck.config.js
  3. ```typescript
  4. module: {
  5. rules: [
  6. {
  7. test: /\.css$/,
  8. use: [
  9. "style-loader",
  10. "css-loader",
  11. "postcss-loader"
  12. ],
  13. },
  14. {
  15. test: /\.s[ac]ss$/i,
  16. use: [
  17. "style-loader",
  18. "css-loader",
  19. "postcss-loader"
  20. {
  21. loader: "sass-loader",
  22. options: {
  23. // Prefer `dart-sass`
  24. implementation: require("sass"),
  25. },
  26. },
  27. ],
  28. },
  29. ],
  30. },

资源处理的顺序


注意(importLoaders)

  1. @import './test.css'
  2. .title{
  3. color:#ffffff;
  4. }

以上代码中test.css的样式并没有触发postcss的兼容
需要使用importLoaders,去再从触发loader

  1. module: {
  2. rules: [
  3. {
  4. test: /\.css$/,
  5. use: [
  6. "style-loader",
  7. {
  8. loader:"css-loader",
  9. options:{
  10. importLoaders:1
  11. }
  12. },
  13. "postcss-loader"
  14. ],
  15. },
  16. {
  17. test: /\.s[ac]ss$/i,
  18. use: [
  19. "style-loader",
  20. {
  21. loader:"css-loader",
  22. options:{
  23. importLoaders:2
  24. }
  25. },
  26. "postcss-loader"
  27. {
  28. loader: "sass-loader",
  29. options: {
  30. // Prefer `dart-sass`
  31. implementation: require("sass"),
  32. },
  33. },
  34. ],
  35. },
  36. ],
  37. },

提取css


需要使用MiniCssExtractPlugin插件

引用提取

  1. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  2. module: {
  3. rules: [
  4. {
  5. test: /\.css$/,
  6. use: [
  7. "style-loader",
  8. "css-loader",
  9. "postcss-loader"
  10. ],
  11. },
  12. {
  13. test: /\.s[ac]ss$/i,
  14. use: [
  15. MiniCssExtractPlugin.loader,
  16. "css-loader",
  17. "postcss-loader",
  18. {
  19. loader: "sass-loader",
  20. options: {
  21. // Prefer `dart-sass`
  22. implementation: require("sass"),
  23. },
  24. },
  25. ],
  26. },
  27. ],
  28. },
  29. plugins: [
  30. new MiniCssExtractPlugin({ filename: "index.css" }),
  31. ],
  32. };

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

用过使用 optimization.splitChunks.cacheGroups 选项,所有的 CSS 可以被提取到一个 CSS 文件中。

  1. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  2. module.exports = {
  3. optimization: {
  4. splitChunks: {
  5. cacheGroups: {
  6. styles: {
  7. name: "styles",
  8. type: "css/mini-extract",
  9. chunks: "all",
  10. enforce: true,
  11. },
  12. },
  13. },
  14. },
  15. plugins: [
  16. new MiniCssExtractPlugin({
  17. filename: "[name].css",
  18. }),
  19. ],
  20. module: {
  21. rules: [
  22. {
  23. test: /\.css$/,
  24. use: [MiniCssExtractPlugin.loader, "css-loader"],
  25. },
  26. ],
  27. },
  28. };

基于入口提取 CSS

你可以基于 webpack 的入口名称提取 CSS。 当你使用路由动态加载但是想要通过入口加载对应的 CSS 文件时这将会非常有用。 这样也避免了 ExtractTextPlugin 造成的 CSS 重复复制问题。

  1. const path = require("path");
  2. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  3. module.exports = {
  4. entry: {
  5. foo: path.resolve(__dirname, "src/foo"),
  6. bar: path.resolve(__dirname, "src/bar"),
  7. },
  8. optimization: {
  9. splitChunks: {
  10. cacheGroups: {
  11. fooStyles: {
  12. type: "css/mini-extract",
  13. name: "styles_foo",
  14. chunks: (chunk) => {
  15. return chunk.name === "foo";
  16. },
  17. enforce: true,
  18. },
  19. barStyles: {
  20. type: "css/mini-extract",
  21. name: "styles_bar",
  22. chunks: (chunk) => {
  23. return chunk.name === "bar";
  24. },
  25. enforce: true,
  26. },
  27. },
  28. },
  29. },
  30. plugins: [
  31. new MiniCssExtractPlugin({
  32. filename: "[name].css",
  33. }),
  34. ],
  35. module: {
  36. rules: [
  37. {
  38. test: /\.css$/,
  39. use: [MiniCssExtractPlugin.loader, "css-loader"],
  40. },
  41. ],
  42. },
  43. };

压缩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="Y7reN"></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. };

TreeShaking

安装purgecss-webpack-plugin

  1. const PurgecssWebpackPlugin =require('purgecss-webpack-plugin ')
  2. const glob = require('glob')
  3. plugins:[
  4. new PurgecssWebpackPlugin({
  5. paths: glob.sync(`${path.resolve('./src')}/**/*`, { nodir: true }),
  6. safelist:function(){
  7. return {
  8. standard:["body","html"]
  9. }
  10. }
  11. })
  12. ]