Asp.net Core MVC 프로젝트에 TailwindCSS 적용하기
// 정의된 클래스 + 임의값 사용 예시
npm init -y
npm install -D tailwindcss@latest postcss@latest autoprefixer@latest
npx tailwind init -p
project.json
{
"name": "프로젝트명",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^10.2.6",
"postcss": "^8.3.5",
"tailwindcss": "^2.2.4"
}
}
project.json
"scripts": {
"style:build": "npx tailwind build -i ./wwwroot/css/site.css -o ./wwwroot/css/output.css"
},
프로젝트명.csproj
<Target Name="프로젝트명" BeforeTargets="Build">
<Exec Command="npm run style:build"/>
</Target>
tailwind.config.js
content: ['./Views/**/*.cshtml'],
wwwroot/css/site.css
@tailwind base;
@tailwind components;
@tailwind utilities;
이렇게 설정을 하면 프로젝트 빌드시마다 위 코드에 의해 wwwroot/css/output.css 파일이 생성된다.
빌드된 css파일을 사용해야 하므로 _Layout.cshtml 파일에 css를 링크한다.
<head>
<link rel="stylesheet" href="~/css/output.css" />
</head>
TailwindCSS 특성상 프로젝트 빌드시에만 css를 빌드하게 되면 편하게 작업할 수 없으므로
파일 수정시에도 바로바로 css파일이 빌드될 수 있게 해줘야 한다.
PowerShell을 열고 프로젝트 경로로 이동후 아래 명령어를 입력하면 된다.
물론 작업할때마다 아래 명령어를 실행시켜줘야 하는 번거로움은 있다.
이 부분도 추후에 자동으로 할 수 있는지 알아볼 계획이다.
npx tailwind build -i ./wwwroot/css/site.css -o ./wwwroot/css/output.css --watch
아쉽게도 아직 VisualStudio에서는 지원하는 플러그인이 없다...
임시방편으로 cshtml 작업을 vscode로 하면 확장을 통해서 Intellisense를 사용할 수 있다.
추후에 VisualStudio용 Intellisense를 만들수 있는지 알아볼 예정이다.