Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

CSS background image fails to load because of backslash #256

Closed
Legends opened this issue Feb 24, 2018 · 14 comments
Closed

CSS background image fails to load because of backslash #256

Legends opened this issue Feb 24, 2018 · 14 comments

Comments

@Legends
Copy link

Legends commented Feb 24, 2018

node 9.5
file-loader ^1.1.9

With the following approach I have a problem with background images in css files.
The last slash is a backslash!

image

{ // SO
test: /\.(png|ico|svg|jpg|gif)$/,
exclude: /node_modules/,
use: {              
	loader: 'file-loader',
	options: {
	name: function (fullPath) {
		console.log(path.relative(__dirname + '/src', fullPath)); // --> images\blue.jpg                      
		return path.relative(__dirname + '/src', fullPath);
	}
	}
}
}

Folder path src\images\

The blue.jpg is requested in my css file:

.hello {
    background: url('../images/blue.jpg');
}
@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Feb 24, 2018

Use path.relative => path.posix.relative to normalize the path, I also recommend to use options.outputPath {Function} and/or options.useRelativePath for this instead

@Legends
Copy link
Author

Legends commented Feb 24, 2018

The problem is I want to resemble the folder structure in src to dist, that's why i use the above approach.
With options.outputPath {Function} I only get the file name.

Your approach: options.outputPath {Function} and options.useRelativePath
In src I have images/icons, with your approach I get images and icons in the root of dist.

I fixed the above issue by using a regex to replace the slashes.

@alexander-akait
Copy link
Member

@Legends Can you create minimum reproducible test repo?

@Legends
Copy link
Author

Legends commented Feb 26, 2018

Here it is: repo
Just execute npm run dev, then in the output folder under wwwroot/dist you will find the generated index.html file.

@ldwonday
Copy link

ldwonday commented Mar 5, 2018

I met the same problem!
This is my config:

const relativeFileLoader = (ext = '[ext]') => ({
	loader: 'file-loader',
	options: {
		useRelativePath: true,
		name: `[name].${ext}`,
		context: resolve('src')
	}
});

this will output "..\..\".

@alexander-akait
Copy link
Member

@ldwonday why you use useRelativePath option?

@ldwonday
Copy link

ldwonday commented Mar 6, 2018

@evilebottnawi I am so sorry, maybe this porblem is other package's bug, and it was fixed!

@Nickman87
Copy link

Same issue here.

The following config:

{
	test: /\.(svg|png|jpg|gif)$/,
	use: [{
		loader: "file-loader",
		options: {
			name: "[path][name].[ext]",
			outputPath: "images",
			context: "node_modules"
		}
	}]
}

provides me with paths like: images\some\path\file.svg

Changing this to the following:

{
	test: /\.(svg|png|jpg|gif)$/,
	use: [{
		loader: "file-loader",
		options: {
			name: "[path][name].[ext]",
			outputPath: function (fullPath) {
				return `images/${fullPath}`;
			},
			context: "node_modules"
		}
	}]
}

provides me with paths like: images/some/path/file.svg as expected.

The lines that cause this difference seem to be these:

  if (options.outputPath) {
    if (typeof options.outputPath === 'function') {
      outputPath = options.outputPath(url);
    } else {
      outputPath = path.posix.join(options.outputPath, url);
    }
  }

Maybe this issue is related to this one: nodejs/node#13887 ?

@alexander-akait
Copy link
Member

@Nickman87 can you create minimum reproducible test repo to ensure bug related to above issue, also please provide windows os version

@alexander-akait
Copy link
Member

@Nickman87 friendly ping. Problem still exists? If yes can you create minimum reproducible test repo?

@Nickman87
Copy link

I have not yet found the time to make a small test repo. I will try to get it done in the coming days!

@alexander-akait
Copy link
Member

@Nickman87 friendly ping

@Nickman87
Copy link

@evilebottnawi I'm sorry, I completely forgot.
I just made a basic setup, which corresponds to my other setup and it is working as expected, no issues 😮

I'll have to cross reference more stuff to see what is going wrong to get a sample project that actually fails...

@alexander-akait
Copy link
Member

/cc @Nickman87 can we close problem?

@Legends Legends closed this as completed May 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants