图片处理

使用loader

  • file-loader
  • url-loader

    file-loader

  • [ext]: 扩展名

  • [name]: 文件名
  • [hash]: 每次打包的hash
  • [contentHash]: 根据内容变动的hash ```javascript const path = require(‘path’) /**
    • [ext]: 扩展名
    • [name]: 文件名
    • [hash]: 文件内容
    • [contentHash]:
    • [hash:]
    • [path]: */ module.exports = { entry: ‘./src/index.js’, output: { filename: ‘main.js’, path: path.resolve(__dirname, ‘dist’) }, module: { rules: [ {
      1. test: /\.(png|svg|gif|jpe?g)$/,
      2. use: [
      3. {
      4. loader: 'file-loader',
      5. options: {
      6. name: 'img/[name].[hash:6].[ext]',
      7. // outputPath: 'img'
      8. }
      9. }
      10. ]
      } ] } }
  1. <a name="yYXjI"></a>
  2. ##### 注意:
  3. 在css中引入的静态资源,file-loader 在webpack5后到处的文件如果是require导入的如果不去配置esModule: false ,则需.default 导出,这个问题会导致css-loader 加载资源的时候出现问题
  4. ```javascript
  5. {
  6. test: /\.(png|svg|gif|jpe?g)$/,
  7. use: [
  8. {
  9. loader: 'file-loader',
  10. options: {
  11. esModule: false // 不转为 esModule
  12. }
  13. }
  14. ]
  15. }

所以需要再css-loader中设置esModule: false

  1. module.exports = {
  2. entry: './src/index.js',
  3. output: {
  4. filename: 'main.js',
  5. path: path.resolve(__dirname, 'dist')
  6. },
  7. module: {
  8. rules: [
  9. {
  10. test: /\.css$/,
  11. use: [
  12. 'style-loader',
  13. {
  14. loader: 'css-loader',
  15. options: {
  16. importLoaders: 1,
  17. esModule: false // 就是这里的
  18. }
  19. },
  20. 'postcss-loader'
  21. ]
  22. },
  23. {
  24. test: /\.less$/,
  25. use: [
  26. 'style-loader',
  27. 'css-loader',
  28. 'postcss-loader',
  29. 'less-loader'
  30. ]
  31. },
  32. {
  33. test: /\.(png|svg|gif|jpe?g)$/,
  34. use: [
  35. {
  36. loader: 'file-loader',
  37. options: {
  38. name: 'img/[name].[hash:6].[ext]',
  39. }
  40. }
  41. ]
  42. }
  43. ]
  44. }
  45. }

url-loader

file-loader就是将文件复制过去,url-loader 包含了file-loader,配置了limit之后大于limit的才会用file-loader,小于的会被打包到js内去

  1. const path = require('path')
  2. module.exports = {
  3. entry: './src/index.js',
  4. output: {
  5. filename: 'main.js',
  6. path: path.resolve(__dirname, 'dist')
  7. },
  8. module: {
  9. rules: [
  10. {
  11. test: /\.css$/,
  12. use: [
  13. 'style-loader',
  14. {
  15. loader: 'css-loader',
  16. options: {
  17. importLoaders: 1,
  18. esModule: false
  19. }
  20. },
  21. 'postcss-loader'
  22. ]
  23. },
  24. {
  25. test: /\.less$/,
  26. use: [
  27. 'style-loader',
  28. 'css-loader',
  29. 'postcss-loader',
  30. 'less-loader'
  31. ]
  32. },
  33. {
  34. test: /\.(png|svg|gif|jpe?g)$/,
  35. use: [
  36. {
  37. loader: 'url-loader',
  38. options: {
  39. name: 'img/[name].[hash:6].[ext]',
  40. limit: 25 * 1024
  41. }
  42. }
  43. ]
  44. }
  45. ]
  46. }
  47. }

asset模块

asset模块是webpack5提供的

asset/resource file-loader
asset/inline url-loader
asset url-loader : limit

asset 打包地址的话,分两个属性
全局
全局控制

  1. output: {
  2. filename: 'main.js',
  3. path: path.resolve(__dirname, 'dist'),
  4. assetModuleFilename: "img/[name].[hash:4][ext]"
  5. },

局部

  1. {
  2. test: /\.(png|svg|gif|jpe?g)$/,
  3. type: 'asset/resource',
  4. generator: {
  5. filename: "img/[name].[hash:4][ext]"
  6. }
  7. },

asset 如果想达到url-loader + limit的效果的话,需要配置parser

  1. {
  2. test: /\.(png|svg|gif|jpe?g)$/,
  3. type: 'asset',
  4. generator: {
  5. filename: "img/[name].[hash:4][ext]"
  6. },
  7. parser: {
  8. dataUrlCondition: {
  9. maxSize: 30 * 1024
  10. }
  11. }
  12. }

整体代码

  1. const path = require('path')
  2. module.exports = {
  3. entry: './src/index.js',
  4. output: {
  5. filename: 'main.js',
  6. path: path.resolve(__dirname, 'dist'),
  7. // assetModuleFilename: "img/[name].[hash:4][ext]"
  8. },
  9. module: {
  10. rules: [
  11. {
  12. test: /\.css$/,
  13. use: [
  14. 'style-loader',
  15. {
  16. loader: 'css-loader',
  17. options: {
  18. importLoaders: 1,
  19. esModule: false
  20. }
  21. },
  22. 'postcss-loader'
  23. ]
  24. },
  25. {
  26. test: /\.less$/,
  27. use: [
  28. 'style-loader',
  29. 'css-loader',
  30. 'postcss-loader',
  31. 'less-loader'
  32. ]
  33. },
  34. // {
  35. // test: /\.(png|svg|gif|jpe?g)$/,
  36. // type: 'asset/resource',
  37. // generator: {
  38. // filename: "img/[name].[hash:4][ext]"
  39. // }
  40. // },
  41. // {
  42. // test: /\.(png|svg|gif|jpe?g)$/,
  43. // type: 'asset/inline'
  44. // }
  45. {
  46. test: /\.(png|svg|gif|jpe?g)$/,
  47. type: 'asset',
  48. generator: {
  49. filename: "img/[name].[hash:4][ext]"
  50. },
  51. parser: {
  52. dataUrlCondition: {
  53. maxSize: 30 * 1024
  54. }
  55. }
  56. }
  57. ]
  58. }
  59. }

字体处理

font 资源处理就使用asset/resource 直接拷贝

  1. {
  2. test: /\.(ttf|woff2?)$/,
  3. type: 'asset/resource',
  4. generator: {
  5. filename: 'font/[name].[hash:3][ext]'
  6. }
  7. }

整体代码

  1. const path = require('path')
  2. module.exports = {
  3. entry: './src/index.js',
  4. output: {
  5. filename: 'main.js',
  6. path: path.resolve(__dirname, 'dist'),
  7. // assetModuleFilename: "img/[name].[hash:4][ext]"
  8. },
  9. module: {
  10. rules: [
  11. {
  12. test: /\.css$/,
  13. use: [
  14. 'style-loader',
  15. {
  16. loader: 'css-loader',
  17. options: {
  18. importLoaders: 1,
  19. esModule: false
  20. }
  21. },
  22. 'postcss-loader'
  23. ]
  24. },
  25. {
  26. test: /\.less$/,
  27. use: [
  28. 'style-loader',
  29. 'css-loader',
  30. 'postcss-loader',
  31. 'less-loader'
  32. ]
  33. },
  34. {
  35. test: /\.(png|svg|gif|jpe?g)$/,
  36. type: 'asset',
  37. generator: {
  38. filename: "img/[name].[hash:4][ext]"
  39. },
  40. parser: {
  41. dataUrlCondition: {
  42. maxSize: 30 * 1024
  43. }
  44. }
  45. },
  46. {
  47. test: /\.(ttf|woff2?)$/,
  48. type: 'asset/resource',
  49. generator: {
  50. filename: 'font/[name].[hash:3][ext]'
  51. }
  52. }
  53. ]
  54. }
  55. }