postcss-custom-properties icon indicating copy to clipboard operation
postcss-custom-properties copied to clipboard

css variable from imported files

Open iliakan opened this issue 6 years ago • 7 comments

According to CSS vars draft, if I import a file with :root variables, it should work, right? But it doesn't with postcss-custom-properties.

vars.css:

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

main.css:

@import 'vars.css';

body {
  color: var(--some-color);
}

The variable doesn't get into main.css, is that a bug or a feature?

iliakan avatar Feb 06 '19 08:02 iliakan

P.S. I'm using postcss-custom-properties via postcss-preset-env.

iliakan avatar Feb 06 '19 08:02 iliakan

Same here, using as postcss plugin and webpack.

{ loader: 'postcss-loader', options: { ident: 'postcss', plugins: () => [ postcssCustomProperties({ preserve: true }) ] } },,

oliversierrab avatar Feb 25 '19 22:02 oliversierrab

The variable doesn't get into main.css, is that a bug or a feature?

If you want the vars.css file to get appended at the top of the main.css file and ship only one file to the browser you'll need a plugin such as postcss-import, otherwise it's just plain default @import behaviour.

Alternatively there's also the importFrom option.

pascalduez avatar Feb 25 '19 23:02 pascalduez

Hey @pascalduez thanks, I'm using sass with this, so have it like this: style-loader|css-loader|postcss-loader|sass-loader so sass-loader is handling the import. Thanks

oliversierrab avatar Feb 26 '19 01:02 oliversierrab

Oh okay. Well, at that point without a minimal reproducible code/repo, we won't be able to help much.

pascalduez avatar Feb 26 '19 06:02 pascalduez

Thanks @pascalduez I'll try to get a sample together, working on a private repo that I cannot share. Thanks

oliversierrab avatar Feb 26 '19 18:02 oliversierrab

I see the same issue. I'm even using postcss-import and it's running as the first plugin in my postcss.config.js. The only way I can get this to work is through importFrom. I'll try to put a minimal example. Just pressed for time at the moment.

ryanfitzer avatar Mar 28 '19 01:03 ryanfitzer