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

Handle SVGs as JSX. #38

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ Importing any stylesheet from JS (with CSS Modules enabled) will convert class n
@import '~react-select/dist/react-select';
}
```
#### 5. SVG as JSX, is it even possible?
Importing SVGs as JSX is possible, but only SVGs suffixed with `.inline` will be treated as JSX code:
```
import logo from './logo.svg';
import LogoInline from './logo.inline.svg';

<img src={logo} alt="logo" />
<LogoInline />
```
14 changes: 11 additions & 3 deletions packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ module.exports = {
// directory for faster rebuilds.
cacheDirectory: true,
},
}
]
},
],
},
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
Expand All @@ -191,7 +191,9 @@ module.exports = {
loader: require.resolve('css-loader'),
options: {
importLoaders: 2,
modules: process.env.CSS_MODULES ? process.env.CSS_MODULES == 'true' : true,
modules: process.env.CSS_MODULES
? process.env.CSS_MODULES == 'true'
: true,
camelCase: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
},
Expand Down Expand Up @@ -226,6 +228,12 @@ module.exports = {
test: /\.md$/,
loader: require.resolve('raw-loader'),
},
// This loader allows to parse SVG as JSX
{
test: /\.inline\.svg$/,
exclude: /node_modules/,
loader: 'svg-react-loader',
},
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
Expand Down
13 changes: 11 additions & 2 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,12 @@ module.exports = {
importLoaders: 2,
minimize: true,
sourceMap: shouldUseSourceMap,
modules: process.env.CSS_MODULES ? process.env.CSS_MODULES == 'true' : true,
modules: process.env.CSS_MODULES
? process.env.CSS_MODULES == 'true'
: true,
camelCase: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
localIdentName:
'[path][name]__[local]--[hash:base64:5]',
},
},
{
Expand Down Expand Up @@ -249,6 +252,12 @@ module.exports = {
test: /\.md$/,
loader: require.resolve('raw-loader'),
},
// This loader allows to parse SVG as JSX
{
test: /\.inline\.svg$/,
exclude: /node_modules/,
loader: 'svg-react-loader',
},
// "file" loader makes sure assets end up in the `build` folder.
// When you `import` an asset, you get its filename.
// This loader doesn't use a "test" so it will catch all modules
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"raw-loader": "^0.5.1",
"react-dev-utils": "^5.0.0",
"style-loader": "0.19.0",
"svg-react-loader": "0.4.5",
"sw-precache-webpack-plugin": "0.11.4",
"url-loader": "0.6.2",
"webpack": "3.8.1",
Expand Down
13 changes: 9 additions & 4 deletions packages/react-scripts/template/src/components/App/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import LogoInline from './logo.inline.svg';
import styles from './style.scss';

class App extends Component {
state = {
count: 0,
}
};

componentDidMount() {
setInterval(this.increment, 1000);
Expand All @@ -19,17 +20,21 @@ class App extends Component {
this.setState(prevState => ({
count: prevState.count + 1,
}));
}
};

render() {
return (
<div className={styles.app}>
<header className={styles.appHeader}>
<LogoInline fill="#61DAFB" className={styles.appLogo} />
<img src={logo} className={styles.appLogo} alt="logo" />
<h1 className={styles.appTitle}>Welcome to React | Count: {this.state.count}</h1>
<h1 className={styles.appTitle}>
Welcome to React | Count: {this.state.count}
</h1>
</header>
<p className={styles.appIntro}>
To get started, edit <code>src/components/App/index.js</code> and save to reload.
To get started, edit <code>src/components/App/index.js</code> and save
to reload.
</p>
</div>
);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.