bholloway / resolve-url-loader

Webpack loader that resolves relative paths in url() statements based on the original source file
563 stars 71 forks source link

Only resolve relative URLs starting with '.' #215

Closed AprilArcus closed 2 years ago

AprilArcus commented 2 years ago

I want all URLs starting with ./ or ../ to be resolved using resolve-url-loader, and all other URLs to be resolved relative to node_modules or my aliases. Is there a way to restrict this loader to only rewrite paths of this kind?

bholloway commented 2 years ago

You probably need a custom join function. Check out the advanced features docs. There are a few examples that might help.

bholloway commented 2 years ago

To fall back to node_modules you might need to rewrite the uri to have with a ~ prefix.

AprilArcus commented 2 years ago

Thanks, that does work. It would be nice if the resolution logic could match sass-loader's handling of import, in which both node_modules and aliases will be tried if the file's own directory does not contain a match.

bholloway commented 2 years ago

According to this the alias should just work, but you still need the ~ to trigger webpack to resolve it. Maybe I misunderstand though. If you have a concrete example for sass-loader feel free to share so I can see how it differs.

Either way I'll mark the question as closed. Glad you got a result. 👍

AprilArcus commented 2 years ago

As you can see here, sass-loader no longer requires the ~ prefix, and in fact recommends against it.

Using ~ is deprecated and can be removed from your code (we recommend it), but we still support it for historical reasons. Why can you remove it? The loader will first try to resolve @import as a relative path. If it cannot be resolved, then the loader will try to resolve @import inside [node_modules](https://webpack.js.org/configuration/resolve/#resolvemodules).

Since sass-loader specifically recommends the use of resolve-url-loader in tandem with it, it would be great to match their behavior.