bun-plugin-svgr 0.1.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 +68 -0
- package/index.ts +24 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# `bun-plugin-svgr`
|
|
2
|
+
|
|
3
|
+
Import SVG files as React/Preact components in Bun. No-config plugin for when you want SVGs to Just Work™.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add the plugin as a dev dependency:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bun add -d bun-plugin-svgr
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
### Development
|
|
16
|
+
|
|
17
|
+
Add to `bunfig.toml`:
|
|
18
|
+
|
|
19
|
+
```toml
|
|
20
|
+
[serve.static]
|
|
21
|
+
plugins = ["bun-plugin-svgr"]
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Production
|
|
25
|
+
|
|
26
|
+
For production builds, specify `svgr()` among the `plugins` of the `Bun.build()` options:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { svgr } from "bun-plugin-svgr";
|
|
30
|
+
|
|
31
|
+
await Bun.build({
|
|
32
|
+
entrypoints: ["./src/index.tsx"],
|
|
33
|
+
outdir: "./dist",
|
|
34
|
+
plugins: [svgr()],
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## TypeScript Support
|
|
39
|
+
|
|
40
|
+
Add `svg.d.ts` to any directory of your project.
|
|
41
|
+
|
|
42
|
+
If the project is running React, add to the file:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
declare module "*.svg" {
|
|
46
|
+
const Component: (props: SVGProps<SVGSVGElement>) => Element;
|
|
47
|
+
export default Component;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
If Preact, add this instead:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
declare module "*.svg" {
|
|
55
|
+
const Component: (props: SVGAttributes<SVGSVGElement>) => Element;
|
|
56
|
+
export default Component;
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Usage
|
|
61
|
+
|
|
62
|
+
Simply import the desired SVG and use it as a component:
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import Icon from "icon.svg";
|
|
66
|
+
|
|
67
|
+
const Comp = () => <Icon className="w-6 h-6" />;
|
|
68
|
+
```
|
package/index.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Config, transform } from "@svgr/core";
|
|
2
|
+
import type { BunPlugin } from "bun";
|
|
3
|
+
|
|
4
|
+
function svgr(config: Config = {}): BunPlugin {
|
|
5
|
+
return {
|
|
6
|
+
name: "bun-plugin-svgr",
|
|
7
|
+
setup(build) {
|
|
8
|
+
build.onLoad({ filter: /\.svg$/ }, async ({ path }) => {
|
|
9
|
+
const svg = await Bun.file(path).text();
|
|
10
|
+
const contents = await transform(svg, {
|
|
11
|
+
plugins: ["@svgr/plugin-jsx"],
|
|
12
|
+
// * default to modern approach
|
|
13
|
+
// * `automatic` does not global React imports, which works in React 17+ anyway
|
|
14
|
+
jsxRuntime: "automatic",
|
|
15
|
+
...config,
|
|
16
|
+
});
|
|
17
|
+
return { contents, loader: "jsx" };
|
|
18
|
+
});
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { svgr };
|
|
24
|
+
export default svgr() satisfies BunPlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bun-plugin-svgr",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Import SVG files as React/Preact components in Bun",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.ts",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/Hyperseeker/bun-plugin-svgr.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"bun",
|
|
14
|
+
"bun-plugin",
|
|
15
|
+
"svgr",
|
|
16
|
+
"svg",
|
|
17
|
+
"react",
|
|
18
|
+
"preact",
|
|
19
|
+
"jsx"
|
|
20
|
+
],
|
|
21
|
+
"files": [
|
|
22
|
+
"index.ts"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@svgr/core": "^8.0.0",
|
|
26
|
+
"@svgr/plugin-jsx": "^8.0.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/bun": "^1.3.4"
|
|
30
|
+
}
|
|
31
|
+
}
|