aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/responsiveLoaderPlugin.ts
blob: 3dc198e36bb0f592fb85980dc31f6ffb22b1aad8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Copyright (c) 2016, Jeremy Stucki
 * Copyright (c) 2024 Kristóf Marussy <kristof@marussy.com>
 *
 * SPDX-License-Identifier: MIT AND BSD-3-Clause
 */

import type { Plugin } from '@docusaurus/types';
import sharp from 'responsive-loader/sharp';

export default function responsiveLoaderPlugin(): Plugin {
  return {
    name: 'marussy-responsive-loader-plugin',
    configureWebpack: () => ({
      mergeStrategy: {
        'module.rules': 'prepend',
      },
      // Configuration based on
      // https://github.com/dazuaz/responsive-loader/blob/ef2c806fcd36f06f6be8a0b97e09f40c3d86d3ac/README.md
      module: {
        rules: [
          {
            test: /\.(png|jpe?g)$/,
            use: [
              {
                loader: 'responsive-loader',
                options: {
                  adapter: sharp,
                  format: 'webp',
                },
              },
            ],
            type: 'javascript/auto',
          },
        ],
      },
    }),
  };
}