Skip to content

Preserve subdirectories for assets in webpack output #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
flow38 opened this issue Dec 6, 2017 · 5 comments
Closed

Preserve subdirectories for assets in webpack output #223

flow38 opened this issue Dec 6, 2017 · 5 comments
Labels

Comments

@flow38
Copy link

flow38 commented Dec 6, 2017

Hello,

Currently encore do not seem to handle subdirectories path for generated assets:

My project structure is:
/assets
/assets/images/
/assets/images/logo

My template use this kind of code
img src="{{ asset('build/images/image1.jpg') }}"
img src="{{ asset('build/images/logos/image2.jpg') }}"

image2.jpg loading will fail cause manifest will look like
"build/images/image1.jpg": "/build/images/image1.6f30c87b.jpg",
"build/images/image2.jpg": "/build/images/image2.6f30c87b.jpg",

I could use in my template 'build/images/image2.jpg' path bu it's totally conter intuitive and error prone.

I did not test on other kind of assets, but subdirectories path should alway take into account by encore.

I found similar issue here but don't know how to apply it to encore and as explain, solution should be apply to all kind of assets.

Thanks for you help

Florent

@Lyrkan
Copy link
Collaborator

Lyrkan commented Dec 6, 2017

Hi @flow38,

Indeed, subdirectories are not kept by the default naming strategy. We discussed about it in #103 to solve another issue but decided not to keep them since it could generate some ugly paths sometimes.

Usually that's not an issue because if you require your images from a file that's processed by Webpack, it'll directly give you the right path (e.g require('images/logo/image1.jpg') from a JS file)... but that's not the case for your Twig files.

The good news is: you can change that quite easily using Encore.configureFilenames(), for instance:

Encore.configureFilenames({
    images: 'images/[path][name].[hash:8].[ext]',
});

@Lyrkan Lyrkan added the question label Dec 6, 2017
@Lyrkan
Copy link
Collaborator

Lyrkan commented Dec 17, 2017

Closing this issue, feel free to reopen it if you need more explanations @flow38.

@Lyrkan Lyrkan closed this as completed Dec 17, 2017
@anstapol
Copy link

anstapol commented Jul 9, 2020

Hi @Lyrkan, love your answer, is there a way to manipulate that path?

Because it's generating the whole path for the image and putting it in the images folder: images/resources/assets/modules/{moduleName}/images/[name].[ext]

It would be great if somehow I could achieve the next path:
images/{pathFromModule}/[name].[ext]

@Lyrkan
Copy link
Collaborator

Lyrkan commented Jul 9, 2020

@antonstsk You'd have to set the context option of the file-loader.

There is no helper method in Encore to configure that loader but you can use configureUrlLoader() to get the same result.

Note that this will be applied to all image files, so if you import some from somewhere else it could lead to strange/long paths too.

@anstapol
Copy link

anstapol commented Jul 9, 2020

@Lyrkan understand that. The thing is that I have solid structure where the images are always in same places.
Will try the configureUrlLoader().

Thank you for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants