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

create-next-app doesn't work with the default postcss config #44286

Open
1 task done
hyperknot opened this issue Dec 23, 2022 · 9 comments
Open
1 task done

create-next-app doesn't work with the default postcss config #44286

hyperknot opened this issue Dec 23, 2022 · 9 comments
Labels
bug Issue was opened via the bug report template.

Comments

@hyperknot
Copy link

hyperknot commented Dec 23, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 21.6.0: Thu Sep 29 20:13:56 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.17.1
      npm: 8.15.0
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.1.0
      eslint-config-next: 13.0.7
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue

No point for this, but if really needed I can create one.

To Reproduce

  1. Create next app
  2. Insert the default PostCSS config from this page: https://nextjs.org/docs/advanced-features/customizing-postcss-config
  3. run build

I used this version:

module.exports = {
  plugins:
    process.env.NODE_ENV === 'production'
      ? [
          'postcss-flexbugs-fixes',
          [
            'postcss-preset-env',
            {
              autoprefixer: {
                flexbox: 'no-2009',
              },
              stage: 3,
              features: {
                'custom-properties': false,
              },
            },
          ],
        ]
      : [
          // No transformations in development
        ],
}

Describe the Bug

Bug 1, you cannot build with the official default config.

Cannot build, error:

Error: Cannot find module 'postcss-flexbugs-fixes'
Require stack:
- node_modules/next/dist/build/webpack/config/blocks/css/plugins.js
- node_modules/next/dist/build/webpack/config/blocks/css/index.js
- node_modules/next/dist/build/webpack/config/index.js
- node_modules/next/dist/build/webpack-config.js
- node_modules/next/dist/build/index.js
- node_modules/next/dist/cli/next-build.js
- node_modules/next/dist/lib/commands.js
- node_modules/next/dist/bin/next

I used the following:

module.exports = {
  plugins:
    process.env.NODE_ENV === 'production'
      ? [
          'postcss-flexbugs-fixes',
          [
            'postcss-preset-env',
            {
              autoprefixer: {
                flexbox: 'no-2009',
              },
              stage: 3,
              features: {
                'custom-properties': false,
              },
            },
          ],
        ]
      : [
          // No transformations in development
        ],
}

Bug 2: on that page, it mentions that Autoprefixer is used, yet there is no autoprefixer installed in yarn.lock nor is found anywhere in node_modules

Bug 3/Question 1: The official Tailwind example has this:

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

This is the exact same as in TailwindCSS's documentation. Now if I understand right, using this file disabled the default PostCSS plugins of Next.js. So we shouldn't be using this, but something which merges both configs.

From all the examples, only commerce seems to have a mixed config:
https://github.com/vercel/commerce/blob/main/site/postcss.config.js

Expected Behavior

Should build with default config. Autoprefixer should be installed by default. Recommended Tailwind config should be mixed with default Next config.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@hyperknot hyperknot added the bug Issue was opened via the bug report template. label Dec 23, 2022
@pogran
Copy link

pogran commented Dec 23, 2022

i also have problems with tailwindcss

@eldieco
Copy link

eldieco commented Dec 23, 2022

I just did extensive package updates to go from next v9 up to v12, among a bunch of other package updates and got everything working on my feature branch and merged successfully to the master branch, but when I went to deploy it, the build failed with this error message:

`Error: Cannot find module 'postcss'

with [MY_BASE_PATH]/node_modules/cssnano-preset-simple/dist/index.js at the top of the stack. This is very problematic as I need to get these changes live.

Everything works fine when running npm run dev.

CORRECTION: I'm only on next 11 because I can't update my node version at this time.

@avgupta456
Copy link

I believe it is fixed with the latest canary: https://github.com/vercel/next.js/releases/tag/v13.1.1-canary.0

@hyperknot
Copy link
Author

I've just tested it, and on next@13.1.2-canary.2, the build is still broken.

I've also tested the autoprefixer on this snippet:

.example {
    display: grid;
    transition: all .5s;
    user-select: none;
    background: linear-gradient(to bottom, white, black);
}

And except for the user-select, none of them gets transformed:

.example {
  display: grid;
  transition: all 0.5s;
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
  background: linear-gradient(180deg, #fff, #000);
}

@hyperknot
Copy link
Author

hyperknot commented Jan 7, 2023

So basically, in a default create-next-app:

@hyperknot hyperknot changed the title Cannot build with official default PostCSS config create-next-app missing autoprefixer, postcss- packages Jan 7, 2023
@hyperknot hyperknot changed the title create-next-app missing autoprefixer, postcss- packages create-next-app doesn't work with the default postcss config Jan 9, 2023
@hyperknot
Copy link
Author

hyperknot commented Jan 9, 2023

Here are my findings so far:

There is no autoprefixer installed anywhere in node_modules in a Next.js install.

Yet, it's still being applied because now autoprefixer is compiled into swc via swc_css? I'm just guessing here.

What is exactly included in swc? autoprefixer + postcss-flexbugs-fixes + postcss-preset-env are all compiled into swc? Because the moment I customise postcss.config.js I need to install those packages manually!

Also, for Tailwind, what is the correct config from the ones below?

Like in all the examples and in official docs:

plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },

or like in the commerce example:

plugins: [
    'tailwindcss/nesting',
    'tailwindcss',
    'autoprefixer',
    'postcss-flexbugs-fixes',
    [
      'postcss-preset-env',
      {
        autoprefixer: {
          flexbox: 'no-2009',
        },
        stage: 3,
        features: {
          'custom-properties': false,
        },
      },
    ],
  ]

@hyperknot
Copy link
Author

hyperknot commented Jan 9, 2023

In my testing I found that the config needs to include the postcss-flexbugs and present-env parts to be identical to the default setup. It means that every Tailwind example, including the official docs should be updated, as

{
    tailwindcss: {},
    autoprefixer: {},
  }

is doing non-default behaviour.

@ZhengRui
Copy link

ZhengRui commented Feb 13, 2023

I am having this type of error:

error - ./node_modules/next/dist/build/webpack/loaders/css-loader/src/index.js??ruleSet[1].rules[3].oneOf[9].use[1]!./node_modules/next/dist/build/webpack/loaders/postcss-loader/src/index.js??ruleSet[1].rules[3].oneOf[9].use[2]!./src/styles/globals.css
TypeError: Cannot read properties of undefined (reading '500')
Import trace for requested module:
./node_modules/next/dist/build/webpack/loaders/css-loader/src/index.js??ruleSet[1].rules[3].oneOf[9].use[1]!./node_modules/next/dist/build/webpack/loaders/postcss-loader/src/index.js??ruleSet[1].rules[3].oneOf[9].use[2]!./src/styles/globals.css
./src/styles/globals.css

very strange, i've tried to install postcss-flexbugs-fixes and postcss-preset-env and put them inside postcss.config.js as above, but doesn't fix the problem.

[Update]: this issue appears after i update tailwindcss from 2 to 3, and it seems I also need to update @tailwindcss/forms. Otherwise it causes the above issue.

@IHIutch
Copy link

IHIutch commented Feb 20, 2023

@ZhengRui This is the same issues I'm having here: #45814

timneutkens pushed a commit that referenced this issue Mar 16, 2023
### What?

This PR introduces a new `--tailwind` flag to the `create-next-app` CLI,
to make it easier to bootstrap a Next.js app with Tailwind CSS
pre-configured. This is going to be the **default**. To opt-out of
Tailwind CSS, you can use the `--no-tailwind` flag.

### Why?

Tailwind CSS is one of the most popular styling solutions right now, and
we would like to make it easier to get started.

Currently, the closest you can come to this is by running `pnpm create
next-app -e with-tailwindcss` which will clone the
https://github.com/vercel/next.js/tree/canary/examples/with-tailwindcss
example. But that example is not configured for the App Router. This PR
will let you add Tailwind CSS to both `app/`, `pages/`, and start out
with TypeScript or JavaScript via the CLI prompts.

(Some community feedback
https://twitter.com/dev_jonaskaas/status/1632367991827443713,
https://twitter.com/samselikoff/status/1634662473331617794)

### How?

We are adding 4 new templates to the CLI bundle.

> Note: The styling is not pixel-perfect compared to the current
templates (using CSS modules) to require fewer overrides, but I tried to
match it as close as possible. Here are a few screenshots:

<details>
<summary><b>Current, light</b></summary>
<img
src="https://user-images.githubusercontent.com/18369201/224733372-9dba86fe-9191-471d-ad9f-ab904c47f544.png"/>
</details>

<details>
<summary><b>Tailwind (new), light</b></summary>
<img
src="https://user-images.githubusercontent.com/18369201/224733610-038d9d0f-634d-4b69-b5c2-a5056b56760c.png"/>
</details>

<details>
<summary><b>Current, dark, responsive</b></summary>
<img
src="https://user-images.githubusercontent.com/18369201/224733790-9b4d730c-0336-4dbe-bc10-1cae1d7fd145.png"/>
</details>

<details>
<summary><b>Tailwind (new), dark, responsive</b></summary>
<img
src="https://user-images.githubusercontent.com/18369201/224734375-28384bbc-2c3a-4125-8f29-c102f3b7aa1d.png"/>
</details>

#### For reviewers

This introduces 4 new templates, with a very similar code base to the
original ones. To keep the PR focused, I decided to copy over duplicate
code, but we could potentially create a shared folder for files that are
the same across templates to somewhat reduce the CLI size. Not sure if
it's worth it, let me know. Probably fine for now, but something to
consider if we are adding more permutations in the future.

---

~Work remaining:~

- [x] app+ts
	- [x] layout
	- [x] dark mode
	- [x] media queries
	- [x] animations
- [x] app+js
- [x] pages+ts
- [x] pages+js
- [x] prompt/config
- [x] deprecate Tailwind CSS example in favor of CLI
- [x] update docs
- [x] add test
- [x] add [Prettier
plugin](https://github.com/tailwindlabs/prettier-plugin-tailwindcss)
 
Closes NEXT-772
Related #45814, #44286
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

6 participants