Skip to content
This repository was archived by the owner on Oct 19, 2022. It is now read-only.

Commit 54c3b8b

Browse files
author
Dominik Rowicki
authored
Merge pull request #38 from pawelnvk/svg-as-jsx
Handle SVGs as JSX.
2 parents 59fc943 + f062ff4 commit 54c3b8b

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,12 @@ Importing any stylesheet from JS (with CSS Modules enabled) will convert class n
6565
@import '~react-select/dist/react-select';
6666
}
6767
```
68+
#### 5. SVG as JSX, is it even possible?
69+
Importing SVGs as JSX is possible, but only SVGs suffixed with `.inline` will be treated as JSX code:
70+
```
71+
import logo from './logo.svg';
72+
import LogoInline from './logo.inline.svg';
73+
74+
<img src={logo} alt="logo" />
75+
<LogoInline />
76+
```

packages/react-scripts/config/webpack.config.dev.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ module.exports = {
175175
// directory for faster rebuilds.
176176
cacheDirectory: true,
177177
},
178-
}
179-
]
178+
},
179+
],
180180
},
181181
// "postcss" loader applies autoprefixer to our CSS.
182182
// "css" loader resolves paths in CSS and adds assets as dependencies.
@@ -191,7 +191,9 @@ module.exports = {
191191
loader: require.resolve('css-loader'),
192192
options: {
193193
importLoaders: 2,
194-
modules: process.env.CSS_MODULES ? process.env.CSS_MODULES == 'true' : true,
194+
modules: process.env.CSS_MODULES
195+
? process.env.CSS_MODULES == 'true'
196+
: true,
195197
camelCase: true,
196198
localIdentName: '[path][name]__[local]--[hash:base64:5]',
197199
},
@@ -226,6 +228,12 @@ module.exports = {
226228
test: /\.md$/,
227229
loader: require.resolve('raw-loader'),
228230
},
231+
// This loader allows to parse SVG as JSX
232+
{
233+
test: /\.inline\.svg$/,
234+
exclude: /node_modules/,
235+
loader: 'svg-react-loader',
236+
},
229237
// "file" loader makes sure those assets get served by WebpackDevServer.
230238
// When you `import` an asset, you get its (virtual) filename.
231239
// In production, they would get copied to the `build` folder.

packages/react-scripts/config/webpack.config.prod.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,12 @@ module.exports = {
206206
importLoaders: 2,
207207
minimize: true,
208208
sourceMap: shouldUseSourceMap,
209-
modules: process.env.CSS_MODULES ? process.env.CSS_MODULES == 'true' : true,
209+
modules: process.env.CSS_MODULES
210+
? process.env.CSS_MODULES == 'true'
211+
: true,
210212
camelCase: true,
211-
localIdentName: '[path][name]__[local]--[hash:base64:5]',
213+
localIdentName:
214+
'[path][name]__[local]--[hash:base64:5]',
212215
},
213216
},
214217
{
@@ -249,6 +252,12 @@ module.exports = {
249252
test: /\.md$/,
250253
loader: require.resolve('raw-loader'),
251254
},
255+
// This loader allows to parse SVG as JSX
256+
{
257+
test: /\.inline\.svg$/,
258+
exclude: /node_modules/,
259+
loader: 'svg-react-loader',
260+
},
252261
// "file" loader makes sure assets end up in the `build` folder.
253262
// When you `import` an asset, you get its filename.
254263
// This loader doesn't use a "test" so it will catch all modules

packages/react-scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"raw-loader": "^0.5.1",
6666
"react-dev-utils": "^5.0.0",
6767
"style-loader": "0.19.0",
68+
"svg-react-loader": "0.4.5",
6869
"sw-precache-webpack-plugin": "0.11.4",
6970
"url-loader": "0.6.2",
7071
"webpack": "3.8.1",

packages/react-scripts/template/src/components/App/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React, { Component } from 'react';
22
import logo from './logo.svg';
3+
import LogoInline from './logo.inline.svg';
34
import styles from './style.scss';
45

56
class App extends Component {
67
state = {
78
count: 0,
8-
}
9+
};
910

1011
componentDidMount() {
1112
setInterval(this.increment, 1000);
@@ -19,17 +20,21 @@ class App extends Component {
1920
this.setState(prevState => ({
2021
count: prevState.count + 1,
2122
}));
22-
}
23+
};
2324

2425
render() {
2526
return (
2627
<div className={styles.app}>
2728
<header className={styles.appHeader}>
29+
<LogoInline fill="#61DAFB" className={styles.appLogo} />
2830
<img src={logo} className={styles.appLogo} alt="logo" />
29-
<h1 className={styles.appTitle}>Welcome to React | Count: {this.state.count}</h1>
31+
<h1 className={styles.appTitle}>
32+
Welcome to React | Count: {this.state.count}
33+
</h1>
3034
</header>
3135
<p className={styles.appIntro}>
32-
To get started, edit <code>src/components/App/index.js</code> and save to reload.
36+
To get started, edit <code>src/components/App/index.js</code> and save
37+
to reload.
3338
</p>
3439
</div>
3540
);
Lines changed: 7 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)