aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/responsiveLoaderPlugin.ts
blob: 6334c4ce3193239e09f104a0d6741caad43c8674 (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
40
41
42
43
44
45
/*
 * Copyright (c) 2016, Jeremy Stucki
 * Copyright (c) Facebook, Inc. and its affiliates.
 * 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: (_config, isServer) => ({
      mergeStrategy: {
        'module.rules': 'prepend',
      },
      // Configuration based on
      // https://github.com/dazuaz/responsive-loader/blob/ef2c806fcd36f06f6be8a0b97e09f40c3d86d3ac/README.md
      module: {
        rules: [
          {
            test: /\.(png|jpe?g)$/,
            resourceQuery: /[?&]rl$/,
            use: [
              {
                loader: 'responsive-loader',
                options: {
                  adapter: sharp,
                  format: 'webp',
                  // See
                  // https://github.com/facebook/docusaurus/blob/c745021b01a8b88d34e1d772278d7171ad8acdf5/packages/docusaurus-plugin-ideal-image/src/index.ts#L62-L66
                  emitFile: !isServer,
                  name: 'assets/images/[name].[hash:hex:7].[width].[ext]',
                },
              },
            ],
            type: 'javascript/auto',
          },
        ],
      },
    }),
  };
}