webpack-contrib / file-loader

File Loader
MIT License
1.86k stars 257 forks source link

Problem with publicPath when localhost has URL segment but production server hasn't #329

Closed vodnycheck closed 5 years ago

vodnycheck commented 5 years ago

I'm using file-loader to load images in background-image in CSS files. I faced difficulty with different URL on my local environment and server.

Locally on my IIS I have a website with the name "platform", so the URL is http://localhost/platform. The production server is working with root folder so URL on production is http://application-name.com, without "platform" segment.

Now I use a workaround where I check NODE_ENV and add or not "platform" segment:

loader: 'file-loader',
options: {
    name: '[path][name].[ext]',
    publicPath: process.env.NODE_ENV === 'development' ? '/platform/' : '/'
}

This approach is not so reliable, so could somebody help me to specify a universal publicPath?

Previously I made it another way:

loader: 'file-loader',
options: {
    name: '[path][name].[ext]',
    publicPath: (url, resourcePath, context) => {
      return `../../../${url}`;
    }
}

But now it doesn't work anymore for me because now I need to launch my code in different places of project, and there can be different levels of urls:

http://application-name.com/1/2 http://application-name.com/1/2/3

alexander-akait commented 5 years ago

It looks like you just deleted our lovely crafted issue template. It was there for good reasons. Please help us solving your issue by answering the questions asked in this template. I'm closing this. Please either update the issue with the template and reopen, or open a new issue.

Don't use relative url in your case, it can break app

alexander-akait commented 5 years ago

Also please use gitter or stackoverflow for questions