postcss-css-variables icon indicating copy to clipboard operation
postcss-css-variables copied to clipboard

Define global variables as css file

Open artem-popov opened this issue 7 years ago • 10 comments

It's just a suggestion for implement.

Now global variables defined as 'variables' in options. And it is an js object.

For me it would be better to import variables as file instead, for example:

options: { variablesPath: 'src/variables.css' }

P.S. Why I need it:

It's because I use css-modules and have no way to set variables declarations somewhere top.

It is possible to import variables from every file which needs them, but I'm using variables as a method to decouple components from global files :) As a context for component...

The solution I use for now is implicitly importing of variables file in sass-loader.

artem-popov avatar Sep 26 '17 20:09 artem-popov

Yes! This is what I've been trying to do - just reference a "global_variables.css" file that has my variables, custom-media, mixins, etc. and have webpack/postcss deliver them to all my app pages/components without me having to explicitly @import that file into every other CSS file in my app.

nathanchase avatar Aug 03 '18 13:08 nathanchase

This would be great. We have a project structure with separate CSS files for each component, and currently we have to duplicate the shared CSS custom props in every CSS file to enable IE11. The bloat really adds up.

cheshrkat avatar Sep 11 '18 23:09 cheshrkat

This is still an issue, and given I have css modules, this prevents me from using this plugin, the custom-properties plugin has a importFrom option that works exactly like I need but doesn't do the local variables. Can't win.

mbulfair avatar Dec 07 '18 20:12 mbulfair

@mbulfair Pull request welcome

MadLittleMods avatar Dec 07 '18 20:12 MadLittleMods

Need this functionality for people using css-modules

rajatbarman avatar Jan 24 '19 11:01 rajatbarman

@MadLittleMods I really need this and would like to start working on a PR. Could you give me some hints on how you would approach this problem?

damianfrizzi avatar Mar 09 '19 15:03 damianfrizzi

@damianfrizzi Some rough overview,

  • Move the variable collection into a re-usable function, https://github.com/MadLittleMods/postcss-css-variables/blob/fe5c821b8feedbe80d8f4399a2b80ca166bb3515/index.js#L152-L199
  • Setup a PostCSS processor to run against options.variablesCssFile
  • Add the variables collected to the map, https://github.com/MadLittleMods/postcss-css-variables/blob/fe5c821b8feedbe80d8f4399a2b80ca166bb3515/index.js#L92-L93

MadLittleMods avatar May 09 '19 10:05 MadLittleMods

any update about this implement?

DevinCarl avatar Dec 04 '20 04:12 DevinCarl

Is there any other solution to solve this?

nilsgabriel avatar Oct 29 '21 10:10 nilsgabriel

My solution has been to just point to a css file and not use this plugin anymore. Unless you have IE as a requirement, custom properties are fully supported now. https://caniuse.com/css-variables

// variables.css
:root {
   --blackColor: hsl(0, 0%, 0%);
}

nathanchase avatar Oct 29 '21 13:10 nathanchase