Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend PostCSS to use plugins other than autoprefixer #166

Closed
OngeUK opened this issue Jun 30, 2017 · 6 comments
Closed

Extend PostCSS to use plugins other than autoprefixer #166

OngeUK opened this issue Jun 30, 2017 · 6 comments

Comments

@OngeUK
Copy link

OngeUK commented Jun 30, 2017

Having PostCSS autoprefixer is a really useful feature, but as far as I can tell, autoprefixer is the only PostCSS plugin that can be used.

Would it be possible to extend the PostCSS functionality to enable the use of other plugins, preferably via a postcss.config.js file?

@ethanroday
Copy link
Contributor

@OngeUK, as of 1.3.0, you can use preact.config.js to customize the Webpack config of your project. Using this, you could configure the PostCSS functionality in a number of ways. You could, for example, modify the postcss entry in the default webpack.LoaderOptionsPlugin to override the list of PostCSS plugins. For example, in your preact.config.js (WARNING: UNTESTED):

export default function (config, env, helpers) {
  let loaderOptionsPlugins = helpers.getPluginsByName(config, 'LoaderOptionsPlugin');
  for (let i = 0; i < loaderOptionsPlugins.length; i++) {
    let loaderOptionsPlugin = loaderOptionsPlugins[i];
    if (loaderOptionsPlugin.options.postcss) {
      loaderOptionsPlugin.options.postcss = () => [
        //Set your list of postcss plugins here (make sure to add them to your dev dependencies)
      ]
    }
  }
}

Alternatively, instead of replacing the postcss entry, you could remove it entirely, then add a postcss.config.js in your project directory. With no specifically configured loader options, postcss-loader will automatically look for your config file.

@sylvainpolletvillard
Copy link

sylvainpolletvillard commented Jun 30, 2017

@ethanroday Thanks for the code. However, since Preact CLI already use postcss-loader and postcss-loader has built-in support for postcss.config.js file, I think it would pretty easy to add support for this config file by default with Preact CLI.

@sylvainpolletvillard
Copy link

sylvainpolletvillard commented Jun 30, 2017

Removing options.postcss from the default config let me use postcss.config.js files 👍

preact.config.js

export default function (config, env, helpers) {
	const options = helpers.getPluginsByName(config, 'LoaderOptionsPlugin')
		.find(({ plugin }) => plugin.options.options.postcss)
		.plugin.options.options;

	delete options.postcss; // will use postcss.config.js
}

@OngeUK
Copy link
Author

OngeUK commented Jul 4, 2017

Thanks for the feedback - the suggested preact.config.js approach works well.

@louisremi
Copy link

louisremi commented Oct 13, 2017

It seems this approach no longer works with preact-cli v2.0.0. :-/
The breaking commit seems to be a954fd6#diff-e8975d611d9d413eecd5c7236e2a7a85

@louisremi
Copy link

louisremi commented Oct 13, 2017

And here's the new workaround for preact-cli v2.0.0:

export default function (config, env, helpers) {
    const postcssLoader = helpers.getLoadersByName(config, 'postcss-loader');
    postcssLoader.forEach(({ loader }) => ( delete loader.options ));
}

:-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants