88import { getFileLoaderUtils } from '@docusaurus/utils' ;
99
1010import type { SVGRConfig , SVGOConfig } from './options' ;
11- import type { RuleSetRule } from 'webpack' ;
11+ import type { Configuration , RuleSetRule } from 'webpack' ;
1212
1313// TODO Docusaurus v4: change these defaults?
1414// see https://github.com/facebook/docusaurus/issues/8297
@@ -37,7 +37,7 @@ const DefaultSVGRConfig: SVGRConfig = {
3737
3838type Params = { isServer : boolean ; svgrConfig : SVGRConfig } ;
3939
40- function createSVGRLoader ( params : Params ) : RuleSetRule {
40+ function createSVGRRule ( params : Params ) : RuleSetRule {
4141 const options : SVGRConfig = {
4242 ...DefaultSVGRConfig ,
4343 ...params . svgrConfig ,
@@ -48,22 +48,42 @@ function createSVGRLoader(params: Params): RuleSetRule {
4848 } ;
4949}
5050
51- export function createLoader ( params : Params ) : RuleSetRule {
51+ export function enhanceConfig ( config : Configuration , params : Params ) : void {
5252 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 = {
5469 test : / \. s v g $ / i,
5570 oneOf : [
5671 {
57- use : [ createSVGRLoader ( params ) ] ,
72+ use : [ createSVGRRule ( params ) ] ,
5873 // We don't want to use SVGR loader for non-React source code
5974 // ie we don't want to use SVGR for CSS files...
6075 issuer : {
6176 and : [ / \. (?: t s x ? | j s x ? | m d x ? ) $ / i] ,
6277 } ,
6378 } ,
64- {
65- use : [ utils . loaders . url ( { folder : 'images' } ) ] ,
66- } ,
79+ existingSvgRule ,
6780 ] ,
6881 } ;
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 ;
6989}
0 commit comments