Css-loader: CSS Variables

Created on 3 Jun 2015  路  1Comment  路  Source: webpack-contrib/css-loader

It would be nice to have a simple way for defining variables.
Inheritance is a nice feature, but it doesn't cover every use case.

How about supporting the :root pseudo-class selector, so that we could have:

variables.css

.colors {
  --color-red: red;
  --color-blue: blue;
}

.fontSizes {
  --font-size-normal: 12px;
  --font-size-large: 14px;
}

component.css

:root {
  extends: colors from "shared/styles/variables.css";
}

.foo {
  color: var(--color-red);
}

transpiled to:

:root {
  --color-blue: blue;
  --color-red: red;
}

.foo {
  color: var(--color-red);
}

This works out of the box in Firefox and it can be transpiled back to CSS2 with postcss-custom-properties

Most helpful comment

I found an alternative solution by removing importLoaders from css-loader and using postcss-import instead.

With the following configuration in webpack.config.js:

module: {
  loaders: [
    { test: /\.css$/, loaders: ['style-loader', 'css-loader?module&localIdentName=[name]__[local]___[hash:base64:5]', 'postcss'] }
  ]
},

postcss: [
  require('postcss-import'),
  require('postcss-color-rebeccapurple'),
  require('postcss-custom-properties')()
  require('autoprefixer-core')
],
// ...

I can transpile something like:

colors.css

:root {
  --color-red: red;
}

component.css

@import url("../shared/styles/colors.css");

.foo {
  color: var(--color-red);
}

to:

.Component__foo___16yOh {
 color: red;
}

>All comments

I found an alternative solution by removing importLoaders from css-loader and using postcss-import instead.

With the following configuration in webpack.config.js:

module: {
  loaders: [
    { test: /\.css$/, loaders: ['style-loader', 'css-loader?module&localIdentName=[name]__[local]___[hash:base64:5]', 'postcss'] }
  ]
},

postcss: [
  require('postcss-import'),
  require('postcss-color-rebeccapurple'),
  require('postcss-custom-properties')()
  require('autoprefixer-core')
],
// ...

I can transpile something like:

colors.css

:root {
  --color-red: red;
}

component.css

@import url("../shared/styles/colors.css");

.foo {
  color: var(--color-red);
}

to:

.Component__foo___16yOh {
 color: red;
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmiller9911 picture dmiller9911  路  3Comments

cuiyue picture cuiyue  路  3Comments

Bhushankumar-pawar picture Bhushankumar-pawar  路  3Comments

Jessidhia picture Jessidhia  路  3Comments

fengyun2 picture fengyun2  路  4Comments