MadLittleMods / postcss-css-variables

PostCSS plugin to transform CSS Custom Properties(CSS variables) syntax into a static representation
https://madlittlemods.github.io/postcss-css-variables/playground/
Other
536 stars 62 forks source link

Update variables without server restart #34

Closed eduarddotgg closed 8 years ago

eduarddotgg commented 8 years ago

I'm using postcss express middleware and i would like to use variables from external file. Something like this:

var postcss = require('postcss');
var cssvariables = require('postcss-css-variables');
var cssVariables   = require(path.join(__dirname, src, 'cssvariables.js'));
postcss([
    cssvariables({
        variables: cssVariables
        }
    })
])
.process(css, opts);

it works just fine but one tiny problem if i modify something in cssvariables.js i will need to restart server to see changes.

There was same issue with postcss-simple-vars and work around was this:

var cssVariables   = path.join(__dirname, src, '_cssVariables.js');

variables: function(){
    delete require.cache[require.resolve(cssVariables)];
    return require(cssVariables);
}

So it would be great to see changes without server restart.

MadLittleMods commented 8 years ago

This isn't really an issue with postcss-css-variables, just how the require cache isn't updated while an app is running.

You can register a new middleware before your postcss-middleware that will run delete require.cache[require.resolve(cssVariables)];.

app.use(function(req, res, next) {
  delete require.cache[require.resolve(cssVariables)];
  next();
});