Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

100 righe
2.1 KiB

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