aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/docs/src/plugins/loadersPlugin.ts
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/docs/src/plugins/loadersPlugin.ts')
-rw-r--r--subprojects/docs/src/plugins/loadersPlugin.ts72
1 files changed, 72 insertions, 0 deletions
diff --git a/subprojects/docs/src/plugins/loadersPlugin.ts b/subprojects/docs/src/plugins/loadersPlugin.ts
new file mode 100644
index 00000000..28474511
--- /dev/null
+++ b/subprojects/docs/src/plugins/loadersPlugin.ts
@@ -0,0 +1,72 @@
1/*
2 * SPDX-FileCopyrightText: 2024 The Refinery Authors
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6
7import type { Plugin } from '@docusaurus/types';
8
9export default function loadersPlugin(): Plugin {
10 return {
11 name: 'refinery-loaders-plugin',
12 configureWebpack(config) {
13 let svgoDisabled = false;
14 const rules = [...(config.module?.rules ?? [])];
15 rules.forEach((rule) => {
16 // Compare with
17 // https://github.com/facebook/docusaurus/blob/73016d4936164ba38d4b86ec2aa8c168b5904a21/packages/docusaurus-utils/src/webpackUtils.ts#L128-L166
18 if (
19 typeof rule !== 'object' ||
20 rule === null ||
21 !('test' in rule) ||
22 !(rule.test instanceof RegExp) ||
23 !rule.test.test('.svg') ||
24 !('oneOf' in rule)
25 ) {
26 return;
27 }
28 const {
29 oneOf: [svgLoader],
30 } = rule;
31 if (
32 typeof svgLoader !== 'object' ||
33 svgLoader === null ||
34 !('use' in svgLoader) ||
35 typeof svgLoader.use !== 'object' ||
36 svgLoader.use === null ||
37 !(0 in svgLoader.use)
38 ) {
39 return;
40 }
41 const {
42 use: [loader],
43 } = svgLoader;
44 if (
45 typeof loader !== 'object' ||
46 loader === null ||
47 !('options' in loader)
48 ) {
49 return;
50 }
51 loader.options = {
52 ...(typeof loader.options === 'object' ? loader.options : {}),
53 // Disable SVGO, because it interferes styling figures exported from Refinery with CSS.
54 svgo: false,
55 svgoConfig: undefined,
56 };
57 svgoDisabled = true;
58 });
59 if (!svgoDisabled) {
60 throw new Error('Failed to disable SVGO.');
61 }
62 return {
63 mergeStrategy: {
64 'module.rules': 'replace',
65 },
66 module: {
67 rules,
68 },
69 };
70 },
71 };
72}