Skip to content

Question: how to configure load of images and fonts relatively to the CSS file? #741

Open
@romainneutron

Description

@romainneutron

Hello,

First, I'd like to give you a big 👏 for this work.
We just finished to port our manual webpack wiring in our Symfony app to webpack encore, and it works well !

However, I had to use configureLoaderRule in order to load images and fonts from a relative path:
We deploy our assets on a CDN, in a subfolder. When we're building assets, we dont know yet what will be the subfolder.

So we want that images and fonts are loaded relatively to the css file instead of an absolute path:

Encore
    // directory where compiled assets will be stored
    .setOutputPath('../public/build')
    // public path used by the web server to access the output path
    .setPublicPath('/build')

    // only needed for CDN's or sub-directory deploy
    .setManifestKeyPrefix('build/')

    // Needed because webpack encore use absolute path, and we release in a CDN folder
    .configureLoaderRule('fonts', (rules) => {
        rules.options.publicPath = function (url) {
            return `./${url}`;
        };
    })

    // Needed because webpack encore use absolute path, and we release in a CDN folder
    .configureLoaderRule('images', (rules) => {
        rules.options.publicPath = function (url) {
            return `./${url}`;
        };
    })

module.exports = Encore.getWebpackConfig();

When using configureLoaderRule a WARNING is displayed when building, and I was wondering if there were a better way to achieve this.
I've seen that there were some works years ago at #179, but maybe there is a more recent and proper way of doing this that I've missed.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions