@docusaurus/plugin-svgr 3.7.0 → 3.8.0-canary-6327

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -13,11 +13,7 @@ function pluginSVGR(_context, options) {
13
13
  return {
14
14
  name: 'docusaurus-plugin-svgr',
15
15
  configureWebpack: (config, isServer) => {
16
- return {
17
- module: {
18
- rules: [(0, svgrLoader_1.createLoader)({ isServer, svgrConfig: options.svgrConfig })],
19
- },
20
- };
16
+ (0, svgrLoader_1.enhanceConfig)(config, { isServer, svgrConfig: options.svgrConfig });
21
17
  },
22
18
  };
23
19
  }
@@ -5,10 +5,10 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import type { SVGRConfig } from './options';
8
- import type { RuleSetRule } from 'webpack';
8
+ import type { Configuration } from 'webpack';
9
9
  type Params = {
10
10
  isServer: boolean;
11
11
  svgrConfig: SVGRConfig;
12
12
  };
13
- export declare function createLoader(params: Params): RuleSetRule;
13
+ export declare function enhanceConfig(config: Configuration, params: Params): void;
14
14
  export {};
package/lib/svgrLoader.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.createLoader = createLoader;
9
+ exports.enhanceConfig = enhanceConfig;
10
10
  const utils_1 = require("@docusaurus/utils");
11
11
  // TODO Docusaurus v4: change these defaults?
12
12
  // see https://github.com/facebook/docusaurus/issues/8297
@@ -31,7 +31,7 @@ const DefaultSVGRConfig = {
31
31
  svgoConfig: DefaultSVGOConfig,
32
32
  titleProp: true,
33
33
  };
34
- function createSVGRLoader(params) {
34
+ function createSVGRRule(params) {
35
35
  const options = {
36
36
  ...DefaultSVGRConfig,
37
37
  ...params.svgrConfig,
@@ -41,22 +41,34 @@ function createSVGRLoader(params) {
41
41
  options,
42
42
  };
43
43
  }
44
- function createLoader(params) {
44
+ function enhanceConfig(config, params) {
45
45
  const utils = (0, utils_1.getFileLoaderUtils)(params.isServer);
46
- return {
46
+ const rules = config?.module?.rules;
47
+ const existingSvgRule = (() => {
48
+ const rule = rules.find((r) => String(r.test) === String(utils.rules.svgs().test));
49
+ if (!rule) {
50
+ throw new Error("Docusaurus built-in SVG rule couldn't be found. The SVGR plugin can't enhance it.");
51
+ }
52
+ return rule;
53
+ })();
54
+ const newSvgRule = {
47
55
  test: /\.svg$/i,
48
56
  oneOf: [
49
57
  {
50
- use: [createSVGRLoader(params)],
58
+ use: [createSVGRRule(params)],
51
59
  // We don't want to use SVGR loader for non-React source code
52
60
  // ie we don't want to use SVGR for CSS files...
53
61
  issuer: {
54
62
  and: [/\.(?:tsx?|jsx?|mdx?)$/i],
55
63
  },
56
64
  },
57
- {
58
- use: [utils.loaders.url({ folder: 'images' })],
59
- },
65
+ existingSvgRule,
60
66
  ],
61
67
  };
68
+ // This is annoying, but we have to "wrap" the existing SVG rule
69
+ // Adding another extra SVG rule (first or last) will not "override"
70
+ // the default: both rules will be applied (from last to bottom) leading to
71
+ // conflicting behavior.
72
+ const index = rules.indexOf(existingSvgRule);
73
+ rules[index] = newSvgRule;
62
74
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-svgr",
3
- "version": "3.7.0",
3
+ "version": "3.8.0-canary-6327",
4
4
  "description": "SVGR plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -18,10 +18,10 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@docusaurus/core": "3.7.0",
22
- "@docusaurus/types": "3.7.0",
23
- "@docusaurus/utils": "3.7.0",
24
- "@docusaurus/utils-validation": "3.7.0",
21
+ "@docusaurus/core": "3.8.0-canary-6327",
22
+ "@docusaurus/types": "3.8.0-canary-6327",
23
+ "@docusaurus/utils": "3.8.0-canary-6327",
24
+ "@docusaurus/utils-validation": "3.8.0-canary-6327",
25
25
  "@svgr/core": "8.1.0",
26
26
  "@svgr/webpack": "^8.1.0",
27
27
  "tslib": "^2.6.0",
@@ -34,5 +34,5 @@
34
34
  "engines": {
35
35
  "node": ">=18.0"
36
36
  },
37
- "gitHead": "dd59750c16fe6908a26f18806a54d4c3dbe6db43"
37
+ "gitHead": "d50539dea74625c994ac3f78926ed7b37d704c84"
38
38
  }
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {createLoader} from './svgrLoader';
8
+ import {enhanceConfig} from './svgrLoader';
9
9
  import type {LoadContext, Plugin} from '@docusaurus/types';
10
10
  import type {PluginOptions, Options} from './options';
11
11
 
@@ -16,11 +16,7 @@ export default function pluginSVGR(
16
16
  return {
17
17
  name: 'docusaurus-plugin-svgr',
18
18
  configureWebpack: (config, isServer) => {
19
- return {
20
- module: {
21
- rules: [createLoader({isServer, svgrConfig: options.svgrConfig})],
22
- },
23
- };
19
+ enhanceConfig(config, {isServer, svgrConfig: options.svgrConfig});
24
20
  },
25
21
  };
26
22
  }
package/src/svgrLoader.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  import {getFileLoaderUtils} from '@docusaurus/utils';
9
9
 
10
10
  import type {SVGRConfig, SVGOConfig} from './options';
11
- import type {RuleSetRule} from 'webpack';
11
+ import type {Configuration, RuleSetRule} from 'webpack';
12
12
 
13
13
  // TODO Docusaurus v4: change these defaults?
14
14
  // see https://github.com/facebook/docusaurus/issues/8297
@@ -37,7 +37,7 @@ const DefaultSVGRConfig: SVGRConfig = {
37
37
 
38
38
  type Params = {isServer: boolean; svgrConfig: SVGRConfig};
39
39
 
40
- function createSVGRLoader(params: Params): RuleSetRule {
40
+ function createSVGRRule(params: Params): RuleSetRule {
41
41
  const options: SVGRConfig = {
42
42
  ...DefaultSVGRConfig,
43
43
  ...params.svgrConfig,
@@ -48,22 +48,42 @@ function createSVGRLoader(params: Params): RuleSetRule {
48
48
  };
49
49
  }
50
50
 
51
- export function createLoader(params: Params): RuleSetRule {
51
+ export function enhanceConfig(config: Configuration, params: Params): void {
52
52
  const utils = getFileLoaderUtils(params.isServer);
53
- return {
53
+
54
+ const rules = config?.module?.rules as RuleSetRule[];
55
+
56
+ const existingSvgRule: RuleSetRule = (() => {
57
+ const rule = rules.find(
58
+ (r) => String(r.test) === String(utils.rules.svgs().test),
59
+ );
60
+ if (!rule) {
61
+ throw new Error(
62
+ "Docusaurus built-in SVG rule couldn't be found. The SVGR plugin can't enhance it.",
63
+ );
64
+ }
65
+ return rule;
66
+ })();
67
+
68
+ const newSvgRule: RuleSetRule = {
54
69
  test: /\.svg$/i,
55
70
  oneOf: [
56
71
  {
57
- use: [createSVGRLoader(params)],
72
+ use: [createSVGRRule(params)],
58
73
  // We don't want to use SVGR loader for non-React source code
59
74
  // ie we don't want to use SVGR for CSS files...
60
75
  issuer: {
61
76
  and: [/\.(?:tsx?|jsx?|mdx?)$/i],
62
77
  },
63
78
  },
64
- {
65
- use: [utils.loaders.url({folder: 'images'})],
66
- },
79
+ existingSvgRule,
67
80
  ],
68
81
  };
82
+
83
+ // This is annoying, but we have to "wrap" the existing SVG rule
84
+ // Adding another extra SVG rule (first or last) will not "override"
85
+ // the default: both rules will be applied (from last to bottom) leading to
86
+ // conflicting behavior.
87
+ const index = rules.indexOf(existingSvgRule);
88
+ rules[index] = newSvgRule;
69
89
  }