@rsbuild/plugin-svgr 2.0.0 → 2.0.2

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.
@@ -1,10 +1,12 @@
1
+ import { Buffer } from 'node:buffer';
1
2
  import type { Rspack } from '@rsbuild/core';
2
3
  type FilenameTemplate = string | ((resourcePath: string, resourceQuery?: string) => string);
3
4
  export type SvgAssetLoaderOptions = {
4
5
  limit: number;
5
6
  name: FilenameTemplate;
7
+ publicPath?: string | ((url: string, resourcePath: string, context: string) => string);
6
8
  };
7
- type RawLoaderDefinition<OptionsType = {}> = ((this: Rspack.LoaderContext<OptionsType>, content: Buffer) => string | Buffer | void | Promise<string | Buffer | void>) & {
9
+ type RawLoaderDefinition<OptionsType = {}> = ((this: Rspack.LoaderContext<OptionsType>, content: Buffer | string) => string | Buffer | void | Promise<string | Buffer | void>) & {
8
10
  raw: true;
9
11
  };
10
12
  declare const assetLoader: RawLoaderDefinition<SvgAssetLoaderOptions>;
@@ -1,24 +1,27 @@
1
1
  import node_path from "node:path";
2
+ import { Buffer } from "node:buffer";
2
3
  import { interpolateName } from "loader-utils";
3
4
  const SVG_MIME_TYPE = 'image/svg+xml';
4
5
  const HASH_TEMPLATE_REGEX = /\[(?:[^:\]]+:)?(?:hash|contenthash)(?::[^\]]+)?]/i;
5
6
  const normalizePath = (value)=>value.replace(/\\/g, '/');
6
7
  const assetLoader = function(content) {
7
- const { limit, name } = this.getOptions();
8
- if (content.length <= limit) {
9
- const dataUrl = `data:${SVG_MIME_TYPE};base64,${content.toString('base64')}`;
8
+ const { limit, name, publicPath } = this.getOptions();
9
+ const buffer = Buffer.isBuffer(content) ? content : Buffer.from(content, 'utf8');
10
+ if (buffer.length <= limit) {
11
+ const dataUrl = `data:${SVG_MIME_TYPE};base64,${buffer.toString('base64')}`;
10
12
  return `export default ${JSON.stringify(dataUrl)}`;
11
13
  }
12
14
  const filename = interpolateName(this, name, {
13
15
  context: this.rootContext,
14
- content
16
+ content: buffer
15
17
  });
16
18
  const assetInfo = {
17
19
  sourceFilename: normalizePath(node_path.relative(this.rootContext, this.resourcePath))
18
20
  };
19
21
  if ('string' == typeof name && HASH_TEMPLATE_REGEX.test(name)) assetInfo.immutable = true;
20
- this.emitFile(filename, content, void 0, assetInfo);
21
- return `export default __webpack_public_path__ + ${JSON.stringify(filename)};`;
22
+ this.emitFile(filename, buffer, void 0, assetInfo);
23
+ const publicPathCode = 'function' == typeof publicPath ? JSON.stringify(publicPath(filename, this.resourcePath, this.rootContext)) : 'string' == typeof publicPath ? JSON.stringify(`${publicPath.endsWith('/') ? publicPath : `${publicPath}/`}${filename}`) : `__webpack_public_path__ + ${JSON.stringify(filename)}`;
24
+ return `export default ${publicPathCode};`;
22
25
  };
23
26
  assetLoader.raw = true;
24
27
  const src_assetLoader = assetLoader;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-svgr",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "SVGR plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,12 +29,12 @@
29
29
  },
30
30
  "devDependencies": {
31
31
  "@rsbuild/core-v1": "npm:@rsbuild/core@^1.7.5",
32
- "@rslib/core": "0.21.2",
32
+ "@rslib/core": "0.21.3",
33
33
  "@types/node": "^24.12.2",
34
34
  "svgo": "^3.3.3",
35
35
  "typescript": "^6.0.3",
36
- "@scripts/test-helper": "1.0.0",
37
- "@rsbuild/core": "2.0.0"
36
+ "@rsbuild/core": "2.0.3",
37
+ "@scripts/test-helper": "1.0.0"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@rsbuild/core": "^2.0.0-0"