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

Add support for configuring PostCSS for Angular projects #9650

Closed
ambeur opened this issue Apr 1, 2022 · 8 comments
Closed

Add support for configuring PostCSS for Angular projects #9650

ambeur opened this issue Apr 1, 2022 · 8 comments
Labels
outdated scope: angular Issues related to Angular support in Nx type: feature

Comments

@ambeur
Copy link

ambeur commented Apr 1, 2022

I created new nx/angular project with tailwind and storybook. All works fine, but now I need to add postcss nesting plugin, like postcss-nested or tailwindcss/nesting.
I created postcss.config.js in my app and lib directory with following config

module.exports = {
  plugins: [
    'postcss-import',
    'tailwindcss/nesting',
    'tailwindcss',
    'autoprefixer',
  ]
}

Current Behavior

I got "Nested CSS was detected, but CSS nesting has not been configured correctly.
Please enable a CSS nesting plugin before Tailwind in your configuration." message in build log. Seems like nx doesn't respect postcss.config.js.

Expected Behavior

Should be possibe to add postcss-plugins.

Environment

Node : 16.13.0
   OS   : win32 x64
   yarn : 1.22.17

   nx : 13.8.3
   @nrwl/angular : 13.8.3
   @nrwl/cli : 13.8.3
   @nrwl/cypress : 13.8.3
   @nrwl/detox : undefined
         @angular/cli: 13.2.5
         @angular/compiler-cli: 13.2.4
         @angular/language-service: 13.2.4
         @storybook/angular: 6.4.19
@CasualBot
Copy link

CasualBot commented Apr 1, 2022

Have you tried the following?
https://nx.dev/guides/using-tailwind-css-in-react#pointing-postcss-to-tailwind-config

const { join } = require('path');

module.exports = {
  plugins: {
    tailwindcss: {
      config: join(__dirname, 'tailwind.config.js'), 
    },
    autoprefixer: {},
  },
};

@ambeur
Copy link
Author

ambeur commented Apr 1, 2022

Yes, i tried, but it looks like postcss.config.js just ignored by builder.

@leosvelperez
Copy link
Member

Thanks for reporting this!

This limitation comes from ng-packagr, which is the package used to build Angular libraries, and it doesn't support PostCSS. It's not exactly a bug but rather a feature that's currently not supported. Note that the documentation shared in #9650 (comment) above is for React projects, not for Angular projects.

I'm changing the label and title of the issue to track the request correctly.

@leosvelperez leosvelperez added type: feature scope: angular Issues related to Angular support in Nx and removed type: bug labels Apr 1, 2022
@leosvelperez leosvelperez changed the title Can't add postcss nesting with tailwindcss to new nx/angular project. Can't configure PostCSS for Angular libraries Apr 1, 2022
@ambeur
Copy link
Author

ambeur commented Apr 1, 2022

@leosvelperez hi! my problem occurs with applications in serve and build mode too. as far as I know the application builder does not use ng-packagr.

@leosvelperez
Copy link
Member

@zvnt I'm sorry, somehow I missed you were referring to apps as well 🤦🏻

Still, there's no built-in/automatic support for PostCSS configuration in Angular apps either (you can see here angular/angular-cli#22849 (comment)). Again, this is a limitation on the underlying Angular tech used by the Nx executors to build projects. You can still configure PostCSS by using a custom webpack configuration.

@leosvelperez leosvelperez changed the title Can't configure PostCSS for Angular libraries Can't configure PostCSS for Angular projects Apr 1, 2022
@leosvelperez leosvelperez changed the title Can't configure PostCSS for Angular projects Add support for configuring PostCSS for Angular projects Apr 1, 2022
@ambeur
Copy link
Author

ambeur commented Apr 1, 2022

I tried custom webpack config with @nrwl/angular:webpack-browser builder and storybook builder. It works!

const {join} = require('path');

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: [
          {
            loader: "postcss-loader",
            options: {
              implementation: require("postcss"),
              postcssOptions: {
                plugins: [
                  'postcss-import',
                  'tailwindcss/nesting',
                  [
                    'tailwindcss',
                    {
                      config: join(__dirname, 'tailwind.config.js'),
                    },
                  ],
                  "autoprefixer",
                ],
              },
            },
          },
        ],
      },
    ],
  },
};

Are there any pitfalls with overwriting the rule for css-files?

@leosvelperez
Copy link
Member

@zvnt you need to be careful not to overwrite the default Angular CLI webpack configuration for styles. The above will replace the rule configuration for the .css extension. Instead, you could use the function syntax that receives the base webpack configuration and update it with the changes you need:

module.exports = config => {
	// add your logic here updating the config
};

That way, you can obtain the specific rule(s) you want to update from the base configuration and make the needed changes without losing any functionality. You can take a look at some docs/examples updating the webpack config in a similar way to get an idea:

https://indepth.dev/posts/1294/angular-css-modules
angular/angular-cli#8427 (comment)

@ambeur ambeur closed this as completed Apr 3, 2022
@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: angular Issues related to Angular support in Nx type: feature
Projects
None yet
Development

No branches or pull requests

3 participants