У меня есть приложение, использующее create-react-app
.Я хотел бы, чтобы структура папок была такой:
src/
components/
CustomAppBar.js
images/
logo.svg
App.js
index.tsx
images.d.ts
В настоящее время я хотел бы использовать logo.svg
в папке изображений в CustomAppBar.js
.В настоящее время этот файл выглядит следующим образом:
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import logo from '*.svg';
const styles = {
flex: {
flex: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
root: {
flexGrow: 1,
},
};
function ButtonAppBar(props) {
const { classes } = props;
return (
<div className={classes.root}>
<AppBar position="static" title={<img styles={{height: '50px'}} src={logo} />}>
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="title" color="inherit" className={classes.flex}>
Title
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</div>
);
}
ButtonAppBar.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(ButtonAppBar);
Как и ожидалось, он завершается с:
Module not found: Can't resolve '*.svg' in 'some\path\src\components
Содержимое images.d.ts
является стандартным:
declare module '*.svg'
declare module '*.png'
declare module '*.jpg'
В другом месте я нашел несколько предложений, в которых упоминается изменение конфигурации Webpack.Кажется, create-react-app
спрятал вещи конфигурации Webpack.Какая лучшая практика, когда дело доходит до этого?