@azerion/bunosaurus-plugin-svgr 0.0.0-stage → 4.0.0

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/README.md CHANGED
@@ -1,3 +1,7 @@
1
- # Temporary Holding Version
1
+ # `@docusaurus/plugin-svgr`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [SVGR](https://react-svgr.com/) plugin for Docusaurus.
4
+
5
+ ## Usage
6
+
7
+ See [plugin-svgr documentation](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-svgr).
package/lib/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { LoadContext, Plugin } from '@docusaurus/types';
8
+ import type { PluginOptions, Options } from './options';
9
+ export default function pluginSVGR(_context: LoadContext, options: PluginOptions): Plugin;
10
+ export { validateOptions } from './options';
11
+ export type { PluginOptions, Options };
package/lib/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.validateOptions = void 0;
10
+ exports.default = pluginSVGR;
11
+ const svgrLoader_1 = require("./svgrLoader");
12
+ function pluginSVGR(_context, options) {
13
+ return {
14
+ name: 'docusaurus-plugin-svgr',
15
+ configureWebpack: (config, isServer) => {
16
+ (0, svgrLoader_1.enhanceConfig)(config, { isServer, svgrConfig: options.svgrConfig });
17
+ },
18
+ };
19
+ }
20
+ var options_1 = require("./options");
21
+ Object.defineProperty(exports, "validateOptions", { enumerable: true, get: function () { return options_1.validateOptions; } });
@@ -0,0 +1,11 @@
1
+ import type { OptionValidationContext } from '@docusaurus/types';
2
+ export type SVGRConfig = any;
3
+ export type SVGOConfig = NonNullable<SVGRConfig['svgoConfig']>;
4
+ export type PluginOptions = {
5
+ svgrConfig: SVGRConfig;
6
+ };
7
+ export type Options = {
8
+ svgrConfig?: Partial<SVGRConfig>;
9
+ };
10
+ export declare const DEFAULT_OPTIONS: Partial<PluginOptions>;
11
+ export declare function validateOptions({ validate, options, }: OptionValidationContext<Options | undefined, PluginOptions>): PluginOptions;
package/lib/options.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_OPTIONS = void 0;
4
+ exports.validateOptions = validateOptions;
5
+ /**
6
+ * Copyright (c) Facebook, Inc. and its affiliates.
7
+ *
8
+ * This source code is licensed under the MIT license found in the
9
+ * LICENSE file in the root directory of this source tree.
10
+ */
11
+ const utils_validation_1 = require("@docusaurus/utils-validation");
12
+ exports.DEFAULT_OPTIONS = {
13
+ svgrConfig: {},
14
+ };
15
+ const pluginOptionsSchema = utils_validation_1.Joi.object({
16
+ svgrConfig: utils_validation_1.Joi.object()
17
+ .pattern(utils_validation_1.Joi.string(), utils_validation_1.Joi.any())
18
+ .optional()
19
+ .default(exports.DEFAULT_OPTIONS.svgrConfig),
20
+ }).default(exports.DEFAULT_OPTIONS);
21
+ function validateOptions({ validate, options, }) {
22
+ return validate(pluginOptionsSchema, options);
23
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { SVGRConfig } from './options';
8
+ import type { Configuration } from 'webpack';
9
+ type Params = {
10
+ isServer: boolean;
11
+ svgrConfig: SVGRConfig;
12
+ };
13
+ export declare function enhanceConfig(config: Configuration, params: Params): void;
14
+ export {};
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.enhanceConfig = enhanceConfig;
10
+ const utils_1 = require("@docusaurus/utils");
11
+ // TODO Docusaurus v4: change these defaults?
12
+ // see https://github.com/facebook/docusaurus/issues/8297
13
+ // see https://github.com/facebook/docusaurus/pull/10205
14
+ // see https://github.com/facebook/docusaurus/pull/10211
15
+ const DefaultSVGOConfig = {
16
+ plugins: [
17
+ {
18
+ name: 'preset-default',
19
+ params: {
20
+ overrides: {
21
+ removeTitle: false,
22
+ removeViewBox: false,
23
+ },
24
+ },
25
+ },
26
+ ],
27
+ };
28
+ const DefaultSVGRConfig = {
29
+ prettier: false,
30
+ svgo: true,
31
+ svgoConfig: DefaultSVGOConfig,
32
+ titleProp: true,
33
+ };
34
+ function createSVGRRule(params) {
35
+ const options = {
36
+ ...DefaultSVGRConfig,
37
+ ...params.svgrConfig,
38
+ };
39
+ return {
40
+ loader: require.resolve('@svgr/webpack'),
41
+ options,
42
+ };
43
+ }
44
+ function enhanceConfig(config, params) {
45
+ const utils = (0, utils_1.getFileLoaderUtils)(params.isServer);
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 = {
55
+ test: /\.svg$/i,
56
+ with: { type: { not: 'text' } },
57
+ oneOf: [
58
+ {
59
+ use: [createSVGRRule(params)],
60
+ // We don't want to use SVGR loader for non-React source code
61
+ // ie we don't want to use SVGR for CSS files...
62
+ issuer: {
63
+ and: [/\.(?:tsx?|jsx?|mdx?)$/i],
64
+ },
65
+ },
66
+ existingSvgRule,
67
+ ],
68
+ };
69
+ // This is annoying, but we have to "wrap" the existing SVG rule
70
+ // Adding another extra SVG rule (first or last) will not "override"
71
+ // the default: both rules will be applied (from last to bottom) leading to
72
+ // conflicting behavior.
73
+ const index = rules.indexOf(existingSvgRule);
74
+ rules[index] = newSvgRule;
75
+ }
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "@azerion/bunosaurus-plugin-svgr",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "4.0.0",
4
+ "description": "SVGR plugin for Docusaurus.",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc --build",
9
+ "watch": "tsc --build --watch"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/azerion/bunosaurus.git",
17
+ "directory": "packages/docusaurus-plugin-svgr"
18
+ },
19
+ "license": "MIT",
20
+ "dependencies": {
21
+ "@docusaurus/core": "npm:@azerion/bunosaurus-core@4.0.0",
22
+ "@docusaurus/types": "npm:@azerion/bunosaurus-types@4.0.0",
23
+ "@docusaurus/utils": "npm:@azerion/bunosaurus-utils@4.0.0",
24
+ "@docusaurus/utils-validation": "npm:@azerion/bunosaurus-utils-validation@4.0.0",
25
+ "@svgr/core": "^8.1.0",
26
+ "@svgr/webpack": "^8.1.0",
27
+ "tslib": "^2.8.1"
28
+ },
29
+ "devDependencies": {
30
+ "@docusaurus/module-type-aliases": "npm:@azerion/bunosaurus-module-type-aliases@4.0.0",
31
+ "webpack": "^5.111.0"
32
+ },
33
+ "peerDependencies": {
34
+ "react": "^19.3.0",
35
+ "react-dom": "^19.3.0"
36
+ },
37
+ "engines": {
38
+ "bun": ">=1.4.1"
39
+ }
40
+ }
package/src/index.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {enhanceConfig} from './svgrLoader';
9
+ import type {LoadContext, Plugin} from '@docusaurus/types';
10
+ import type {PluginOptions, Options} from './options';
11
+
12
+ export default function pluginSVGR(
13
+ _context: LoadContext,
14
+ options: PluginOptions,
15
+ ): Plugin {
16
+ return {
17
+ name: 'docusaurus-plugin-svgr',
18
+ configureWebpack: (config, isServer) => {
19
+ enhanceConfig(config, {isServer, svgrConfig: options.svgrConfig});
20
+ },
21
+ };
22
+ }
23
+
24
+ export {validateOptions} from './options';
25
+
26
+ export type {PluginOptions, Options};
package/src/options.ts ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import {Joi} from '@docusaurus/utils-validation';
8
+ import type {OptionValidationContext} from '@docusaurus/types';
9
+
10
+ // TODO unfortunately there's a SVGR TS error when skipLibCheck=false
11
+ // related to prettier, see https://github.com/gregberge/svgr/issues/904
12
+ // import type {Config as SVGRConfig} from '@svgr/core';
13
+ // export type {SVGRConfig};
14
+ export type SVGRConfig = any;
15
+ export type SVGOConfig = NonNullable<SVGRConfig['svgoConfig']>;
16
+
17
+ export type PluginOptions = {
18
+ svgrConfig: SVGRConfig;
19
+ };
20
+
21
+ export type Options = {
22
+ svgrConfig?: Partial<SVGRConfig>;
23
+ };
24
+
25
+ export const DEFAULT_OPTIONS: Partial<PluginOptions> = {
26
+ svgrConfig: {},
27
+ };
28
+
29
+ const pluginOptionsSchema = Joi.object<PluginOptions>({
30
+ svgrConfig: Joi.object()
31
+ .pattern(Joi.string(), Joi.any())
32
+ .optional()
33
+ .default(DEFAULT_OPTIONS.svgrConfig),
34
+ }).default(DEFAULT_OPTIONS);
35
+
36
+ export function validateOptions({
37
+ validate,
38
+ options,
39
+ }: OptionValidationContext<Options | undefined, PluginOptions>): PluginOptions {
40
+ return validate(pluginOptionsSchema, options);
41
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {getFileLoaderUtils} from '@docusaurus/utils';
9
+
10
+ import type {SVGRConfig, SVGOConfig} from './options';
11
+ import type {Configuration, RuleSetRule} from 'webpack';
12
+
13
+ // TODO Docusaurus v4: change these defaults?
14
+ // see https://github.com/facebook/docusaurus/issues/8297
15
+ // see https://github.com/facebook/docusaurus/pull/10205
16
+ // see https://github.com/facebook/docusaurus/pull/10211
17
+ const DefaultSVGOConfig: SVGOConfig = {
18
+ plugins: [
19
+ {
20
+ name: 'preset-default',
21
+ params: {
22
+ overrides: {
23
+ removeTitle: false,
24
+ removeViewBox: false,
25
+ },
26
+ },
27
+ },
28
+ ],
29
+ };
30
+
31
+ const DefaultSVGRConfig: SVGRConfig = {
32
+ prettier: false,
33
+ svgo: true,
34
+ svgoConfig: DefaultSVGOConfig,
35
+ titleProp: true,
36
+ };
37
+
38
+ type Params = {isServer: boolean; svgrConfig: SVGRConfig};
39
+
40
+ function createSVGRRule(params: Params): RuleSetRule {
41
+ const options: SVGRConfig = {
42
+ ...DefaultSVGRConfig,
43
+ ...params.svgrConfig,
44
+ };
45
+ return {
46
+ loader: require.resolve('@svgr/webpack'),
47
+ options,
48
+ };
49
+ }
50
+
51
+ export function enhanceConfig(config: Configuration, params: Params): void {
52
+ const utils = getFileLoaderUtils(params.isServer);
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 = {
69
+ test: /\.svg$/i,
70
+ with: {type: {not: 'text'}},
71
+ oneOf: [
72
+ {
73
+ use: [createSVGRRule(params)],
74
+ // We don't want to use SVGR loader for non-React source code
75
+ // ie we don't want to use SVGR for CSS files...
76
+ issuer: {
77
+ and: [/\.(?:tsx?|jsx?|mdx?)$/i],
78
+ },
79
+ },
80
+ existingSvgRule,
81
+ ],
82
+ };
83
+
84
+ // This is annoying, but we have to "wrap" the existing SVG rule
85
+ // Adding another extra SVG rule (first or last) will not "override"
86
+ // the default: both rules will be applied (from last to bottom) leading to
87
+ // conflicting behavior.
88
+ const index = rules.indexOf(existingSvgRule);
89
+ rules[index] = newSvgRule;
90
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ /// <reference types="@docusaurus/module-type-aliases" />