Skip to content

Commit b0d9309

Browse files
authored
docs: Add custom icons to support Rsbuild documentation (#7743)
1 parent f64d071 commit b0d9309

File tree

2 files changed

+165
-1
lines changed

2 files changed

+165
-1
lines changed

components/icon/index.en-US.md

+80
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ See [iconfont.cn documents](http://iconfont.cn/help/detail?spm=a313x.7781069.199
122122

123123
### Custom SVG Icon
124124

125+
#### vue cli 3
126+
125127
You can import SVG icon as an vue component by using `vue cli 3` and [`vue-svg-loader`](https://www.npmjs.com/package/vue-svg-loader). `vue-svg-loader`'s `options` [reference](https://github.com/visualfanatic/vue-svg-loader).
126128

127129
```js
@@ -149,6 +151,84 @@ export default defineComponent({
149151
});
150152
```
151153

154+
#### Rsbuild
155+
156+
Rsbuild is a new generation of build tool, official website https://rsbuild.dev/
157+
Create your own `vue-svg-loader.js` file, which allows you to customize and beautify SVG, and then configure it in `rsbuild.config.ts`
158+
159+
```js
160+
// vue-svg-loader.js
161+
/* eslint-disable */
162+
const { optimize } = require('svgo');
163+
const { version } = require('vue');
164+
const semverMajor = require('semver/functions/major');
165+
166+
module.exports = async function (svg) {
167+
const callback = this.async();
168+
169+
try {
170+
({ data: svg } = await optimize(svg, {
171+
path: this.resourcePath,
172+
js2svg: {
173+
indent: 2,
174+
pretty: true,
175+
},
176+
plugins: [
177+
'convertStyleToAttrs',
178+
'removeDoctype',
179+
'removeXMLProcInst',
180+
'removeComments',
181+
'removeMetadata',
182+
'removeTitle',
183+
'removeDesc',
184+
'removeStyleElement',
185+
'removeXMLNS',
186+
'removeXMLProcInst',
187+
],
188+
}));
189+
} catch (error) {
190+
callback(error);
191+
return;
192+
}
193+
194+
if (semverMajor(version) === 2) {
195+
svg = svg.replace('<svg', '<svg v-on="$listeners"');
196+
}
197+
198+
callback(null, `<template>${svg}</template>`);
199+
};
200+
```
201+
202+
```js
203+
// rsbuild.config.ts
204+
/* eslint-disable */
205+
import { defineConfig } from '@rsbuild/core';
206+
import { pluginVue } from '@rsbuild/plugin-vue';
207+
208+
export default defineConfig({
209+
tools: {
210+
bundlerChain(chain, { CHAIN_ID }) {
211+
chain.module.rule(CHAIN_ID.RULE.SVG).exclude.add(/\.svg$/);
212+
},
213+
rspack: {
214+
module: {
215+
rules: [
216+
{
217+
test: /\.svg$/,
218+
use: ['vue-loader', 'vue-svg-loader'],
219+
},
220+
],
221+
},
222+
resolveLoader: {
223+
alias: {
224+
'vue-svg-loader': require('path').join(__dirname, './vue-svg-loader.js'),
225+
},
226+
},
227+
},
228+
},
229+
});
230+
```
231+
152232
The following properties are available for the component:
153233

154234
| Property | Description | Type | Default |

components/icon/index.zh-CN.md

+85-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ export default defineComponent({
119119

120120
### 自定义 SVG 图标
121121

122-
如果使用 `vue cli 3`,可以通过配置 [vue-svg-loader](https://www.npmjs.com/package/vue-svg-loader) 来将 `svg` 图标作为 `Vue` 组件导入。更多`vue-svg-loader` 的使用方式请参阅 [文档](https://github.com/visualfanatic/vue-svg-loader)
122+
#### vue cli 3
123+
124+
可以通过配置 [vue-svg-loader](https://www.npmjs.com/package/vue-svg-loader) 来将 `svg` 图标作为 `Vue` 组件导入。更多`vue-svg-loader` 的使用方式请参阅 [文档](https://github.com/visualfanatic/vue-svg-loader)
123125

124126
```js
125127
// vue.config.js
@@ -146,6 +148,88 @@ export default defineComponent({
146148
});
147149
```
148150

151+
#### Rsbuild
152+
153+
Rsbuild 是新一代构建工具,官网 https://rsbuild.dev/
154+
155+
自己实现一个 `vue-svg-loader.js` 文件,好处是可以自定义美化 svg,然后在 `rsbuild.config.ts` 中配置:
156+
157+
```js
158+
// vue-svg-loader.js
159+
/* eslint-disable */
160+
const { optimize } = require('svgo');
161+
const { version } = require('vue');
162+
const semverMajor = require('semver/functions/major');
163+
164+
module.exports = async function (svg) {
165+
const callback = this.async();
166+
167+
try {
168+
({ data: svg } = await optimize(svg, {
169+
path: this.resourcePath,
170+
js2svg: {
171+
indent: 2,
172+
pretty: true,
173+
},
174+
plugins: [
175+
'convertStyleToAttrs',
176+
'removeDoctype',
177+
'removeXMLProcInst',
178+
'removeComments',
179+
'removeMetadata',
180+
'removeTitle',
181+
'removeDesc',
182+
'removeStyleElement',
183+
'removeXMLNS',
184+
'removeXMLProcInst',
185+
],
186+
}));
187+
} catch (error) {
188+
callback(error);
189+
return;
190+
}
191+
192+
if (semverMajor(version) === 2) {
193+
svg = svg.replace('<svg', '<svg v-on="$listeners"');
194+
}
195+
196+
callback(null, `<template>${svg}</template>`);
197+
};
198+
```
199+
200+
```js
201+
// rsbuild.config.ts
202+
/* eslint-disable */
203+
import { defineConfig } from '@rsbuild/core';
204+
import { pluginVue } from '@rsbuild/plugin-vue';
205+
206+
export default defineConfig({
207+
tools: {
208+
bundlerChain(chain, { CHAIN_ID }) {
209+
chain.module
210+
// 先给svg排除默认的规则,方便下面自定义loader
211+
.rule(CHAIN_ID.RULE.SVG)
212+
.exclude.add(/\.svg$/);
213+
},
214+
rspack: {
215+
module: {
216+
rules: [
217+
{
218+
test: /\.svg$/,
219+
use: ['vue-loader', 'vue-svg-loader'],
220+
},
221+
],
222+
},
223+
resolveLoader: {
224+
alias: {
225+
'vue-svg-loader': require('path').join(__dirname, './vue-svg-loader.js'),
226+
},
227+
},
228+
},
229+
},
230+
});
231+
```
232+
149233
`Icon` 中的 `component` 组件的接受的属性如下:
150234

151235
| 字段 | 说明 | 类型 | 只读值 |

0 commit comments

Comments
 (0)