question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non-standardized PostCSS config file doesn't support require statements

See original GitHub issue

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:

  1. yarn add -D tailwindcss autoprefixer
  2. Add the following postcss.config.js:
module.exports = {
  plugins: [
    require("tailwindcss"),
    require("autoprefixer")
  ]
};
  1. 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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:22 (13 by maintainers)

github_iconTop GitHub Comments

49reactions
shrugscommented, Jan 15, 2020

the workaround, for searchers finding this issue: my postcss.config.js previously looked like

module.exports = {
  plugins: [
    require('postcss-import'),
    require('tailwindcss'),
    require('postcss-preset-env')({ stage: 2 }),
  ],
};

but now looks like

module.exports = {
  plugins: {
    'postcss-import': {},
    tailwindcss: {},
    'postcss-preset-env': { stage: 2 },
  },
};

following the format from https://github.com/postcss/postcss-loader

10reactions
Timercommented, Jan 15, 2020

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:

module.exports = {
  plugins: {
    'postcss-import': {},
    tailwindcss: {},
    'postcss-preset-env': { stage: 2 },
  },
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

No PostCSS config found - webpack - Stack Overflow
Made a new file in the root directory named postcss.config.js and added. module.exports = {};. Found this on the following post:.
Read more >
Changelog | Stylelint
Fixed: autofix will respect scoped disable comments by turning off autofix for the scoped rules for the entire source; this is a continuation...
Read more >
gatsby-plugin-mdx
This config option is used for compatibility with a set of plugins many people use with remark that require the Gatsby environment to...
Read more >
Advanced Features: Customizing PostCSS Config - Next.js
No configuration is needed to support CSS Modules. To enable CSS Modules for a file, rename the file to have the extension .module.css...
Read more >
Configuring Vite
Note Vite supports using ES modules syntax in the config file even if the ... Note that Vite doesn't load .env files by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found