Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Tailwind for a new Angular project?

I want to create a new Angular project using Tailwind CSS. My current CLI version is 10.1.1. Things I have done so far:

  • Create a new app using ng new my-app
  • Use Angular routing => yes
  • Use SCSS as the stylesheet
  • In the root directory of the project run npm i tailwindcss postcss-import postcss-loader postcss-scss @angular-builders/custom-webpack -D
  • In the src folder there is a styles.scss file, modify it to

.

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
  • In the root directory of the project run npx tailwind init
  • In the root directory of the project create a new file webpack.config.js with the following content

.

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loader: "postcss-loader",
        options: {
          ident: "postcss",
          syntax: "postcss-scss",
          plugins: () => [
            require("postcss-import"),
            require("tailwindcss"),
            require("autoprefixer"),
          ],
        },
      },
    ],
  },
};
  • In the root directory there is a Angular.json file
    • Search for the key projects => my-app => architect => build
      • Change the builder to "builder": "@angular-builders/custom-webpack:browser",
      • Add to the options

.

"customWebpackConfig": {
    "path": "./webpack.config.js"
},
  • Search for the key projects => my-app => architect => serve
    • Change the builder to "builder": "@angular-builders/custom-webpack:dev-server",
    • Add to the options

.

"customWebpackConfig": {
    "path": "./webpack.config.js"
},
  • Run the app with ng serve from the app's root directory

I'm getting this error

ERROR in ./src/styles.scss (./node_modules/css-loader/dist/cjs.js??ref--13-1!./node_modules/@angular-devkit/build-angular/node_modules/postcss-loader/src??embedded!./node_modules/resolve-url-loader??ref--13-3!./node_modules/sass-loader/dist/cjs.js??ref--13-4!./node_modules/postcss-loader/dist/cjs.js??postcss!./src/styles.scss) Module build failed (from ./node_modules/postcss-loader/dist/cjs.js): ValidationError: Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.

  • options has an unknown property 'plugins'. These properties are valid: object { postcssOptions?, execute?, sourceMap? } at validate (/.../my-app/node_modules/schema-utils/dist/validate.js:98:11) at Object.loader (/.../my-app/node_modules/postcss-loader/dist/index.js:43:28)

ERROR in Module build failed (from ./node_modules/postcss-loader/dist/cjs.js): ValidationError: Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.

  • same text as above

How can I setup Tailwind correctly?

like image 286
Question3r Avatar asked Sep 12 '20 09:09

Question3r


People also ask

Can I use Tailwind with angular?

Tailwind generally works well with popular frameworks like Bootstrap, Angular Material, and others. But there are some issues you might run into: Class Name Overlap: Most UI frameworks, such as Bootstrap, have their own sets of utility classes.

Can Tailwind be used with SCSS?

A guide to using Tailwind with common CSS preprocessors like Sass, Less, and Stylus. Since Tailwind is a PostCSS plugin, there's nothing stopping you from using it with Sass, Less, Stylus, or other preprocessors, just like you can with other PostCSS plugins like Autoprefixer.

How do I set up tailwind CSS in angular?

Setting up Tailwind CSS in an Angular project. Start by creating a new Angular project if you don’t have one set up already. The most common approach is to use Angular CLI. Install tailwindcss via npm, and then run the init command to generate a tailwind.config.js file. npm install -D tailwindcss postcss autoprefixer npx tailwindcss init

How to use tailwind directives & functions in angular?

First step is to run the following schematic in your Angular project: ng add @ngneat/tailwind When asked if you would you like to use Tailwind directives & functions in component styles? select Yes

How do I start a new angular project?

Generate a new Angular application Let's start by creating a new Angular project using the ng new command: When the CLI asks you " which stylesheet format would you like to use? " choose CSS because: With Tailwind, you don't need a CSS preprocessor like Sass. You'll rarely need to write custom CSS anyway.

What is tailwind CSS?

It comes with a set of utility classes (CSS classes). This will allow you to create and combine the classes to give your UI the aspect that you want. TailwindCSS allows you to customize their styles in a very easy way to create your own design systems. You will spend more time in your business logic rather than your CSS


3 Answers

I have found the answer after banging my head everywhere today, change your webpack.config.js to,

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loader: "postcss-loader",
        options: {
          postcssOptions: {
            ident: "postcss",
            syntax: "postcss-scss",
            plugins: [
              require("postcss-import"),
              require("tailwindcss"),
              require("autoprefixer"),
            ],
          },
        },
      },
    ],
  },
};

There is small change, plugins now take array instead of function. Thanks in advance πŸ˜‰.


If anyone is still running into issue, checkout this blog I've written on Angular 10 + Tailwind CSS πŸ‘‡

https://fullyunderstood.com/get-started-with-angular-tailwind-css/

like image 53
Indrajeet Avatar answered Oct 12 '22 22:10

Indrajeet


I managed to get Angular 10 + Angular Material to work with Tailwind CSS using configuration highlighted in this diff - Tailwind CSS support in Angular 10, with Angular Material.

Some key points besides what has already been highlighted in the question/answers:

  • Need to explicitly install postcss (8.x). Somehow default pulls (7.x) causing Error: true is not a PostCSS plugin.
  • Config in webpack.config.js depends on postcss-loader version 4.x.
  • If you are using Angular Material, you will get Error: Failed to find '~@angular/material/theming'. To resolve this, I limit the scss files that will be processed by postcss by separating the scss file (i.e. test: /tailwind\.scss$/ in webpack.config.js).

So far, looks like it is working. 🀞

like image 35
kctang Avatar answered Oct 12 '22 22:10

kctang


Imports are 'tailwindcss', not 'tailwind':

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
like image 3
txk Avatar answered Oct 12 '22 23:10

txk