css-loader icon indicating copy to clipboard operation
css-loader copied to clipboard

Option to turn off url() inlining

Open catdad opened this issue 11 years ago • 21 comments

Would it be possible to turn off url() inlining/importing? I have looked throughout and cannot find a config option to disable this.

In a project, I am only using url() for fonts (supporting legacy browsers) and do not want all 4 fonts inlined into the CSS file. I can't seem to find an option to allow that code to get through the loader untouched.

catdad avatar Apr 14 '15 18:04 catdad

and do not want all 4 fonts inlined into the CSS file.

With the file-loader they won't be inlined. Have you tried this?

sokra avatar Apr 15 '15 15:04 sokra

After some more research, I was able to work around this issue in a way other than the pull request I submitted. It's far less elegant though. I still feel that an "always inline everything" approach is not suitable for CSS and we could use some more control.

catdad avatar Apr 15 '15 17:04 catdad

What do you mean by inlining? Data-uris?

jhnns avatar Apr 28 '15 19:04 jhnns

Yes, data-uris.

Basically, I am modularizing some legacy code. I can't afford to change much about the output structure (folder breakdown, types and locations of files, etc.), but I would still appreciate the benefits of writing modular code.

With CSS specifically, it has been difficult to get Webpack to just leave my code alone and let it just be CSS code. The most frustrating parts have been legacy fonts and device ppi-based alternative images, as the CSS loader will always attempt to embed them as data-uris. Using the file-loader for those files requires me to put the files in the modules folder instead, and then always ends up placing them in the wrong location as output.

It seems like there should just be an option such as "this is my final CSS code, don't mess with it".

catdad avatar Apr 29 '15 01:04 catdad

"this is my final CSS code, don't mess with it"

Well, that defeats the purpose of the css-loader. Then you could also use the raw-loader and pass it to style-loader.

But I'm not sure... usually the file-loader and the url-loader are interchangeable. It doesn't require re-ordering or anything. And it should definitely not end up in the wrong location.... maybe there is something wrong with your configuration?

jhnns avatar Apr 29 '15 07:04 jhnns

I naively switched to the raw-loader, and am only now noticing that my CSS sourcemaps are broken. From what I can tell, the raw-loader doesn't support sourcemaps. So the css-loader still has the purpose of providing sourcemaps, even if it didn't parse anything else.

I'm almost positive there is something wrong with my configuration file, since there is very little documentation on advanced scenarios, and I have mostly been working with trial and error. It places extra files (those that are not the direct CSS output file) in the root output directory, even though I have my Webpack output files going into separate folders (one for js, another for css using the ExtraxtTextPlugin).

However, more on topic, the concern here is whether we can treat a CSS file as CSS and still not edit the CSS.

This could also be seen as a request for the raw-loader to support sourcemaps, but seeing as how it is supported by the same people, and a grant total of 5 lines of code, I think addressing this in a CSS module is more appropriate.

catdad avatar May 13 '15 21:05 catdad

The raw-loader can't support source maps because that would require knowledge about the language (and the raw-loader shouldn't know about CSS).

What about using the file-loader instead of the url-loader?

jhnns avatar May 20 '15 08:05 jhnns

Yes, I mentioned that the raw-loader doesn't support sourcemaps already... or really anything that would be expected. My point there was that the css-loader provides more benefits than just embedding external resources (like sourcemaps, minification, etc.).

As I mentioned earlier, using the file-loader resulted in the static files being copied to the wrong location, so the CSS could no longer access them.

Anyway, respectfully, this whole process was taking way too long for me -- I have a product to ship -- so I went ahead and implemented a new loader that is able to provide the benefits of a dedicated CSS loader without changing the code. Thanks for your help and insights.

catdad avatar May 21 '15 01:05 catdad

css-loader?-url

sokra avatar May 21 '15 06:05 sokra

I would also love to be able to turn it off. I'm using custom urls in my css (parsed later by a custom library), but makes css-loader fails:

Module build failed: ModuleNotFoundError: Module not found: Error: Cannot resolve \'file\' or \'directory\' ./ui://buttons/red.png

micky2be avatar Nov 11 '15 01:11 micky2be

@micky2be This was implemented. Are you using the css loader as css-loader?-url? The -url part is what turns it off.

catdad avatar Nov 11 '15 03:11 catdad

I tried but didn't do anything, getting same errors (using latest webpack and css-loader).

micky2be avatar Nov 11 '15 03:11 micky2be

I tested it when the fix was released, but I never ended up switching any of my builds to use it. I have been using my own lib, https://github.com/catdad/css-raw-loader, to import CSS without modifying it. Not sure if it would meet all of your requirements.

I do not see the -url handling in the source now, but I will check back on it when I have some time.

catdad avatar Nov 11 '15 04:11 catdad

Well, I guess I'm gonna use your loader then ;)

micky2be avatar Nov 11 '15 04:11 micky2be

Btw, to any newcomers, it appears this was fixed in [email protected]. I was on [email protected] and neither css-loader?-url nor css-loader?url=false disabled url loading, but npm update cleared that up.

chbrown avatar Jan 12 '16 20:01 chbrown

@chbrown Thanks for the info :+1: I was stuck at css?-url.

wizawu avatar Feb 17 '16 07:02 wizawu

I can't get url disabling working with sourceMaps with css?sourceMap&url=false, though each option works individually. Is there a reason this can't work?

alexmeq avatar Nov 02 '16 19:11 alexmeq

+1, having the same issue where when I turn on sourceMaps url disabling no longer works.

neenhouse avatar Jan 10 '17 19:01 neenhouse

Somehow, seeming related to this.. I also want css-loader to ignore my urls.

This seems to work, also with sourcemaps.

However with sourcemaps the files are not actually loading in the browser. It does display the path to right (unmodified & existing) url, but will not show. Only when I rewrite the URL to an absolute path to the same asset, it will load.

navFooh avatar Jan 23 '17 10:01 navFooh

css-loader 0.28.5

{
          loader: 'css-loader',
          options: {
            url: false
          }
        }

works!

cpatti97100 avatar Sep 04 '17 10:09 cpatti97100