postcss-simple-vars icon indicating copy to clipboard operation
postcss-simple-vars copied to clipboard

Variables hot reloading

Open franciscolourenco opened this issue 7 years ago • 6 comments

The variables seem to be updated, but only after restarting webpack dev server. postcss.config.js seems to be added as a loader dependency by default, which means that editing this file causes webpack to recompile. However the variable files imported in postcss.config.js don't. Is there a way to access the loader in postcss.config.js in order to call addDependency with the variable files? Otherwise how is the reloading supposed to work?

franciscolourenco avatar Mar 19 '18 14:03 franciscolourenco

Hi. You need to open issue in postcss-loader.

ai avatar Mar 19 '18 15:03 ai

@ai postcss-simple-vars docs mention variable hot reloading. Is it currently not supported?

franciscolourenco avatar Mar 19 '18 15:03 franciscolourenco

They are supported by postcss-loader. It reads a config. So I have no idea how to fix it in this plugin 🤷‍♂️

ai avatar Mar 19 '18 15:03 ai

One way to do it would be to receive file(s) path(s) instead of an object. That way you could you could do loader.addDependency('filepath'); require('filepath'); automatically and it would work.

franciscolourenco avatar Mar 20 '18 14:03 franciscolourenco

Yeap. I will think about it on next weekend.

ai avatar Mar 20 '18 17:03 ai

For anyone still having this issue, it's solvable by using the plugins option of postcss-loader, which gives you access to the loader context.

Example loader definition

{
  loader: 'postcss-loader',
  options: {
    plugins: function(loader) {
      const colors = require.resolve('./colors.js');
      loader.addDependency(colors);
      delete require.cache[colors];

      return [
        simpleVars({
          variables: function() {
            return require(colors);
          },
        }),
      ];
    },
  },
}

philipbordallo avatar Dec 06 '18 04:12 philipbordallo