No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

101 líneas
2.1 KiB

  1. //@ts-check
  2. /** @typedef {import('webpack').Configuration} WebpackConfig **/
  3. 'use strict';
  4. const path = require('path');
  5. const CopyPlugin = require('copy-webpack-plugin');
  6. const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin');
  7. module.exports =
  8. /**
  9. * @param {{ useOptimization?: boolean; squoosh?: boolean } | undefined } env
  10. * @param {{ mode: 'production' | 'development' | 'none' | undefined }} argv
  11. * @returns { WebpackConfig }
  12. */
  13. function (env, argv) {
  14. const mode = argv.mode || 'none';
  15. const basePath = path.join(__dirname, 'src', 'webviews', 'apps');
  16. env = {
  17. useOptimization: false,
  18. squoosh: false,
  19. ...env,
  20. };
  21. /** @type ImageMinimizerPlugin.Generator<any> */
  22. // @ts-ignore
  23. let imageGeneratorConfig = env.squoosh
  24. ? {
  25. type: 'asset',
  26. implementation: ImageMinimizerPlugin.squooshGenerate,
  27. options: {
  28. encodeOptions: {
  29. webp: {
  30. // quality: 90,
  31. lossless: 1,
  32. },
  33. },
  34. },
  35. }
  36. : {
  37. type: 'asset',
  38. implementation: ImageMinimizerPlugin.imageminGenerate,
  39. options: {
  40. plugins: [
  41. [
  42. 'imagemin-webp',
  43. {
  44. lossless: true,
  45. nearLossless: 0,
  46. quality: 100,
  47. method: mode === 'production' ? 4 : 0,
  48. },
  49. ],
  50. ],
  51. },
  52. };
  53. /** @type WebpackConfig['plugins'] */
  54. const plugins = [
  55. new CopyPlugin({
  56. patterns: [
  57. {
  58. from: path.posix.join(basePath.replace(/\\/g, '/'), 'images', 'settings', '*.png'),
  59. to: __dirname.replace(/\\/g, '/'),
  60. },
  61. ],
  62. }),
  63. ];
  64. if (!env.useOptimization) {
  65. plugins.push(
  66. new ImageMinimizerPlugin({
  67. deleteOriginalAssets: true,
  68. generator: [imageGeneratorConfig],
  69. }),
  70. );
  71. }
  72. /** @type WebpackConfig */
  73. const config = {
  74. name: 'images',
  75. context: basePath,
  76. entry: {},
  77. mode: mode,
  78. plugins: plugins,
  79. };
  80. if (env.useOptimization) {
  81. config.optimization = {
  82. minimize: true,
  83. minimizer: [
  84. new ImageMinimizerPlugin({
  85. deleteOriginalAssets: true,
  86. generator: [imageGeneratorConfig],
  87. }),
  88. ],
  89. };
  90. }
  91. return config;
  92. };