next.js: Non-standardized PostCSS config file doesn't support require statements
Bug report
Describe the bug
With the introduction of the marvelous Built-in CSS Support, a new non-standard PostCSS Config plugin syntax was introduced.
This syntax requires that plugins listed in postcss.config.js be strings rather than require statements. This breaks backwards compatibility and differs from the standard set by existing PostCSS documentation.
Additionally, this makes it tough (or impossible) to share a common PostCSS configuration with additional tools like Storybook.js.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
yarn add -D tailwindcss autoprefixer- Add the following
postcss.config.js:
module.exports = {
plugins: [
require("tailwindcss"),
require("autoprefixer")
]
};
yarn next build
Expected behavior
- The build succeeds, and either plugins imported with
require()are supported - Or the build fails, and a more descriptive error is shown in the build log.
Screenshots
The following is shown when running next build:
Error: An unknown PostCSS plugin was provided (function creator() {
--
02:32:09 PM | var transformer = initializer.apply(void 0, arguments);
02:32:09 PM | transformer.postcssPlugin = name;
02:32:09 PM | transformer.postcssVersion = new _processor.default().version;
02:32:09 PM | return transformer;
02:32:09 PM | }).
02:32:09 PM | > Build error occurred
System information
- OS: macOS
- Version of Next.js: 9.2
Additional context
This work was likely done to be able to identify, dedupe, and acknowledge Next’s default PostCSS plugins — and it’s likely the solution to this issue is nothing more than better documentation.
Here are a couple proposed solutions:
1. Support require() statements in PostCSS config
I’d rely on @Timer’s knowledge here, as there are likely other consequences (notably: not being able to inspect and dedupe plugins).
2. Display a warning/error that indicates deprecated behavior
This would be a simple check in packages/next/build/webpack/config/blocks/css/plugins.ts to see if the plugin is a function:
else if (typeof plugin === 'function') {
console.error(
`${chalk.red.bold(
'Error'
)}: A PostCSS Plugin must be provided as a ${chalk.bold(
'string'
)}. Instead, we got a function imported with require().`
)
}
I’m happy to submit PRs for either of these solutions! But looking for guidance from the core team.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 9
- Comments: 22 (13 by maintainers)
Commits related to this issue
- Fix PostCSS config New Next.js built-in CSS support doesn't work with PostCSS plugins as Array of required modules. Instead it needs it as an Object (https://github.com/zeit/next.js/issues/10117). — committed to innodoc/innodoc-webapp by tumidi 7 years ago
- Update docs for Preprocessors Add `-D` flag for dev dependencies. Prevent error with `require()` in `postcss.config.js` cfr [this issue](https://github.com/vercel/next.js/issues/10117) — committed to depsimon/tailwindcss.com by depsimon 5 years ago
the workaround, for searchers finding this issue: my
postcss.config.jspreviously looked likebut now looks like
following the format from https://github.com/postcss/postcss-loader
The big motivator behind this restriction is because self-
requireing code makes it impossible for us to cache CSS transformations.In other words, we’d have to re-transform the CSS every time the application was edited—this is slow/suboptimal.
We have not yet documented PostCSS customization, but it’s coming soon!
re: sharing config
We do support a subset of
postcss.config.js, as you posted above:@simonsmith you should be able to use the resolved file path as the plugin name:
Now explained in Next.js canaries with read more links.
it seems like you can use either format—adding purgecss to my config works just fine like so, following the config options from https://github.com/zeit/next.js/blob/canary/examples/with-tailwindcss/postcss.config.js
noting, in the case of tailwind, to ignore the base import
I love the update! Running into a snag though. I can’t seem to use purgecss in my postcss.config.js file. It was working with
next-css. No errors or warning.Here’s an example for purgecss: https://github.com/zeit/next.js/blob/canary/examples/with-tailwindcss/postcss.config.js
cssnano is not needed (applied already by next).
Thanks @Timer — that’s really illuminating! And @shrugs’s syntax suggestion worked great for me 👍
That’s already added, try upgrading to next@canary to see it.
Try next@canary, the message was updated there
The action items we want to take are:
typeof ... === 'function'that explains to notrequirethe plugin.Feel free to send PRs!