Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS modules break build if used with ~ #589

Closed
jantimon opened this issue Aug 4, 2017 · 15 comments · Fixed by #851
Closed

CSS modules break build if used with ~ #589

jantimon opened this issue Aug 4, 2017 · 15 comments · Fixed by #851

Comments

@jantimon
Copy link

jantimon commented Aug 4, 2017

Module not found: Error: Can't resolve '~'

What is the current behavior?

Using a tilde ~ inside a css file causes Can't resolve '~' if css-modules feature is activated:

{
  loader: 'css-loader',
  options: {
    modules: true,
  }
},

If the current behavior is a bug, please provide the steps to reproduce.

The bug can be reproduced this way: (https://github.com/jantimon/css-loader-bug)

git clone https://github.com/jantimon/css-loader-bug.git
cd css-loader-bug
npm install
npm run without-modules 
npm run with-modules

Working webpack.config.js
Broken webpack.config.js
CSS File

What is the expected behavior?

Compilation should work even if modules are set to true.

Motivation

I wrote a loader which adds ~!! to a css file and it does not know if the user is using css-modules: https://github.com/jantimon/iconfont-webpack-plugin/blob/master/lib/postcss-plugin.js#L135-L137

@alexander-akait
Copy link
Member

alexander-akait commented Aug 4, 2017

@jantimon why don't use url('file-loader!flags-of-the-world/src/DK.svg')? It is works.

@jantimon
Copy link
Author

jantimon commented Aug 4, 2017

@evilebottnawi this won't work if modules are set to false.

I am looking for a syntax which can be used with different css-loader configs for my plugin

@jantimon
Copy link
Author

jantimon commented Aug 4, 2017

For now I am working around this issue by forcing the user to tell me wether he is using cssModules or not:

https://github.com/jantimon/iconfont-webpack-plugin/blob/master/example/module/webpack.config.js#L30

@lhrinaldi
Copy link

Is this considered an inconsistency? I wish I could fix this.

@lnhrdt
Copy link

lnhrdt commented Oct 31, 2017

Is this considered an inconsistency?

Yes, it definitely is. I believe this also may be the cause of some confusion in issues like #12.

@alexander-akait
Copy link
Member

Seems bug PR welcome!

@alexander-akait
Copy link
Member

alexander-akait commented Feb 15, 2018

Just information: @import "~normalize.css/normalize.css"; also don't works, seems all syntax don't works

I had the same issue, it seems kinda counterintuitive:

:global { * { background: url('~assets/my-asset.svg'); } } // works
:local { * { background: url('~assets/my-asset.svg'); } } // does not work
:local { * { background: url('assets/my-asset.svg'); } } // no tilde here but works

That is odd behaviour.

Because of this resolving the actual URL is context-sensitive making global variables ala bootstrap-sass (e.g. $styleguilde-asset-url / $bootstrap-font-url) awkward to use.

@Oscar-ren
Copy link

I find the reason: https://github.com/webpack-contrib/css-loader/blob/master/lib/processCss.js#L163-L174
If open the css module, the mode is 'local', url will reserve the ~,such as:

:local { * { background: url('~assets/my-asset.svg'); } }
// urlToRequest
:local { * { background: url(require('~assets/my-asset.svg')); } }

else the mode is 'global', url will recognize the ~ ,https://github.com/webpack/loader-utils/blob/67499ff3d13c13ef455ec0fede90b714fc16d787/lib/urlToRequest.js#L50-L52,and remove the ~,require file as a module.such as:

:global { * { background: url('~assets/my-asset.svg'); } }
// urlToRequest
:global { * { background: url(require('assets/my-asset.svg')); } }

@dmlinn
Copy link

dmlinn commented Mar 29, 2018

Using vue-loader which allows modules to be turned on or off per <style> tag. Using or omitting a tilde in the font paths breaks my code in different places, as fonts no longer resolve depending on the module tag. Toying with possible build-process workarounds. Wish I had the background to fix it right.

@rajatpharmeasy
Copy link

This problem still exists.

alexander-akait added a commit that referenced this issue Dec 5, 2018
@alexander-akait alexander-akait mentioned this issue Dec 5, 2018
6 tasks
alexander-akait added a commit that referenced this issue Dec 5, 2018
alexander-akait pushed a commit that referenced this issue Dec 5, 2018
@jantimon
Copy link
Author

jantimon commented Dec 6, 2018

@evilebottnawi could you please summarize how this issue was solved?

@alexander-akait
Copy link
Member

alexander-akait commented Dec 6, 2018

@jantimon we use same logic for resolving urls with modules false/local/global, before we use difference logic for local and global and global was broken (you can't use ~, !! and etc stuff in url), now it is fixed

@alexander-akait
Copy link
Member

You can try master branch, feel free to feedback

@nailfar
Copy link

nailfar commented Apr 10, 2019

@evilebottnawi
This problem still exists.
Here is my code
https://github.com/nailfar/cssLoaderEx/tree/master

@alexander-akait
Copy link
Member

Please use latest version of css-loader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants