Skip to content

Commit 2abd7f1

Browse files
committed
Replace vite-plugin-svgr with a custom Vite plugin for loading SVGs as React components
- [Fragment] was created by a no-name developer. can we replace it with someting else? ├── apps │ └── editor │ ├── package.json │ ├── scripts │ │ └── build.mjs │ └── vite.config.ts └── packages └── ui └── src └── components └── editor └── common └── Icon └── Icon.tsx
1 parent a13ebd9 commit 2abd7f1

3 files changed

Lines changed: 118 additions & 212 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,7 @@
13711371
"sass-embedded": "1.89.2",
13721372
"ts-node": "10.9.2",
13731373
"typed-css-modules": "0.9.1",
1374-
"vite": "8.3.6",
1375-
"vite-plugin-svgr": "5.2.0"
1374+
"vite": "8.3.6"
13761375
},
13771376
"dependencies": {
13781377
"@joplin/turndown": "4.0.81",

‎apps/editor/vite.config.ts‎

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as crypto from 'crypto'
22
import * as fs from 'fs'
33
import * as path from 'path'
44
import { defineConfig, type Plugin, type UserConfig } from 'vite'
5-
import svgr from 'vite-plugin-svgr'
65

76
const node_targets: Record<string, string> = {
87
extension: 'src/extension.ts',
@@ -54,6 +53,56 @@ const copy_readme = (): Plugin => {
5453
}
5554
}
5655

56+
const svg_as_component = (): Plugin => ({
57+
name: 'svg-as-component',
58+
enforce: 'pre',
59+
load(id) {
60+
const file = id.split('?')[0]
61+
if (!file.endsWith('.svg')) return null
62+
63+
const source = fs
64+
.readFileSync(file, 'utf8')
65+
.replace(/<\?xml[\s\S]*?\?>/g, '')
66+
.replace(/<!DOCTYPE[\s\S]*?>/gi, '')
67+
.replace(/<!--[\s\S]*?-->/g, '')
68+
.trim()
69+
70+
const match = source.match(/^<svg([\s\S]*?)>([\s\S]*)<\/svg>$/i)
71+
if (!match) {
72+
this.error(`Unsupported SVG structure: ${file}`)
73+
}
74+
75+
const attributes: Record<string, string> = {}
76+
const attribute_regex = /([^\s=]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g
77+
let attribute_match: RegExpExecArray | null
78+
while ((attribute_match = attribute_regex.exec(match![1])) !== null) {
79+
const name = attribute_match[1]
80+
if (name == 'style') continue // inline style strings are not valid React props
81+
const react_name =
82+
name == 'class'
83+
? 'className'
84+
: name.replace(/[-:](\w)/g, (_, char: string) => char.toUpperCase())
85+
attributes[react_name] = attribute_match[2] ?? attribute_match[3]
86+
}
87+
88+
return {
89+
code: [
90+
`import { createElement } from 'react'`,
91+
`const attributes = ${JSON.stringify(attributes)}`,
92+
`const html = ${JSON.stringify(match![2])}`,
93+
`const SvgComponent = (props) =>`,
94+
` createElement('svg', {`,
95+
` ...attributes,`,
96+
` ...props,`,
97+
` dangerouslySetInnerHTML: { __html: html }`,
98+
` })`,
99+
`export default SvgComponent`
100+
].join('\n'),
101+
map: null
102+
}
103+
}
104+
})
105+
57106
const generate_scoped_name = (local_name: string, filename: string) => {
58107
const file = filename.split('?')[0]
59108
const relative_file = path.relative(root_dir, file).replace(/\\/g, '/')
@@ -111,12 +160,7 @@ const create_view_config = (
111160
is_production: boolean
112161
): UserConfig => ({
113162
publicDir: false,
114-
plugins: [
115-
svgr({
116-
include: '**/*.svg',
117-
svgrOptions: { exportType: 'default' }
118-
})
119-
],
163+
plugins: [svg_as_component()],
120164
define: {
121165
'process.env.NODE_ENV': JSON.stringify(
122166
is_production ? 'production' : 'development'

0 commit comments

Comments
 (0)