-
-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
dynamic image not found in react component #743
Comments
Hi, I think the issue come from the
For now you have two solutions:
Encore
// if you want to keep using `file-loader`
.configureLoaderRule('images', rule => {
rule.options.esModule = false;
})
// OR if you want to use `url-loader`
.configureUrlLoader({
images: {
esModule: false
}
}); However, I think we should configure
😅 What do you think @Lyrkan? |
@Kocal It works by adding .default to the require! Thanks for the explanations, I admit it's one of the first projects I use webpack encore and I'm having a little trouble ^^' Thank you very much! |
Hm, yeah that could be an issue. But since Webpack seems to be moving everything to ES modules instead of CommonJS (for good reasons) it's probably better to encourage the same thing and keep the default There is a third "solution" to this issue : use ES imports instead of // Simple imports - needs to be at the top of your module
import '../images/foo.png' as fooPath;
// Dynamic import
// Edit: Note that it will create an async chunk by default which only
// contains the path. If that's an issue it can be fixed relatively
// easily using the `/* webpackMode: "eager" */ comment or through the
// MinChunkSizePlugin.
import('../images/foo.png').then(({ default: fooPath }) => {
// use fooPath there
});
// Dynamic import if you can use async functions
const { default: fooPath } = await import('../images/foo.png');
Maybe we could change that to something along the lines of:
(ping @weaverryan) Edit: actually using dynamic imports would also require |
I vote for updating the changelog and keeping the default |
Hello,
Here I try to import and use an image in a react component, but this one is never found :
GET http://localhost:8000/[object%20Module] 404 (Not Found)
I try to access to defaultLogo.png which is in this tree:
https://prnt.sc/s92g2r
Here is my part of the code which is supposed to call and use it:
const pathToLogo = require('../../images/user_logo/${userData.logo_name}'); <img src={pathToLogo} alt=""/>
And here's my webpack encore config :
var Encore = require('@symfony/webpack-encore'); if (!Encore.isRuntimeEnvironmentConfigured()) { Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev'); } Encore .setOutputPath('public/build/') .setPublicPath('/build') .addEntry('app', './assets/js/app.jsx') .splitEntryChunks() .enableSingleRuntimeChunk() .cleanupOutputBeforeBuild() .enableBuildNotifications() .enableSourceMaps(!Encore.isProduction()) .enableVersioning(Encore.isProduction()) .configureBabelPresetEnv((config) => { config.useBuiltIns = 'usage'; config.corejs = 3; }) .enableSassLoader() .enableReactPreset() .copyFiles({ from: './assets/images/', to: 'images/[path][name].[hash:8].[ext]', pattern: /\.(png|jpg|jpeg|svg)$/ }) ; module.exports = Encore.getWebpackConfig();
Knowing that I'm working with "dev-server", but even "dev" or "build" doesn't work...
Any ideas? Do I misunderstand the syntax? I confess I have some trouble with Webpack encore...
Thanks a lot
The text was updated successfully, but these errors were encountered: