Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

99 рядки
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; useSharpForImageOptimization?: 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. useSharpForImageOptimization: true,
  18. ...env,
  19. };
  20. /** @type ImageMinimizerPlugin.Generator<any> */
  21. // @ts-ignore
  22. let imageGeneratorConfig = env.useSharpForImageOptimization
  23. ? {
  24. type: 'asset',
  25. implementation: ImageMinimizerPlugin.sharpGenerate,
  26. options: {
  27. encodeOptions: {
  28. webp: {
  29. lossless: true,
  30. },
  31. },
  32. },
  33. }
  34. : {
  35. type: 'asset',
  36. implementation: ImageMinimizerPlugin.imageminGenerate,
  37. options: {
  38. plugins: [
  39. [
  40. 'imagemin-webp',
  41. {
  42. lossless: true,
  43. nearLossless: 0,
  44. quality: 100,
  45. method: mode === 'production' ? 4 : 0,
  46. },
  47. ],
  48. ],
  49. },
  50. };
  51. /** @type WebpackConfig['plugins'] */
  52. const plugins = [
  53. new CopyPlugin({
  54. patterns: [
  55. {
  56. from: path.posix.join(basePath.replace(/\\/g, '/'), 'media', '*.png'),
  57. to: path.posix.join(__dirname.replace(/\\/g, '/'), 'dist', 'webviews'),
  58. },
  59. ],
  60. }),
  61. ];
  62. if (!env.useOptimization) {
  63. plugins.push(
  64. new ImageMinimizerPlugin({
  65. deleteOriginalAssets: true,
  66. generator: [imageGeneratorConfig],
  67. }),
  68. );
  69. }
  70. /** @type WebpackConfig */
  71. const config = {
  72. name: 'images',
  73. context: basePath,
  74. entry: {},
  75. mode: mode,
  76. plugins: plugins,
  77. };
  78. if (env.useOptimization) {
  79. config.optimization = {
  80. minimize: true,
  81. minimizer: [
  82. new ImageMinimizerPlugin({
  83. deleteOriginalAssets: true,
  84. generator: [imageGeneratorConfig],
  85. }),
  86. ],
  87. };
  88. }
  89. return config;
  90. };