As of Parcel 2, you can include images using URL
. See https://parceljs.org/recipes/react/#images
const logo = new URL('logo.svg', import.meta.url);
export function Logo() {
return <img src={logo} alt="logo" />;
}
Here's an example project layout for using js+images:
$ tree -I node_modules/ -I dist
.
├── index.html
├── package-lock.json
├── package.json
└── src
├── img
│ └── logo.png
└── js
└── index.js
package.json:
{
"devDependencies": {
"parcel": "^2.9.3"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
index.html:
<!doctype html>
<html>
<head>
<script type="module" src="src/js/index.js"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
src/js/index.js:
import { createRoot } from 'react-dom/client'
function App() {
const logo = new URL('/src/img/logo.png', import.meta.url)
return <div>
<h1>My App</h1>
<img src={logo} alt='logo'/>
</div>
}
const domNode = document.getElementById('root')
const root = createRoot(domNode)
root.render(<App/>)
import
syntax. But the path must be a string.import image from 'path/to/image.png'
– Cartogram