Open maitakedayo opened 7 months ago
-- ここで初学者は倒れて立ち上がれない気がするので動かして学ぶシリーズにしてみた
const express = require("express"); const app = express(); const port = 3000;
app.use(express.static("public"));
app.get("/", (req, res, next) => { res.end("Top Page"); });
app.listen(port, () => { console.log(Server is running on http://localhost:${port}); });
Server is running on http://localhost:${port}
```html:public/index.html <html> <head> <title>Top Page</title> </head> <body> <h1>Top Page</h1> </body> </html>
@tailwind base; @tailwind components; @tailwind utilities;
const fs = require('fs'); const util = require('util'); const exec = util.promisify(require('child_process').exec);
async function build() { try { const { stdout, stderr } = await exec('npx tailwindcss build tailwind.css -o public/styles.css'); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (err) { console.error(err); } } build();
- htmlヘッダにpublic/styles.cssのリンク追加 ```html:public/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Top Page</title> <link href="/styles.css" rel="stylesheet"> </head> <body> <h1 class="font-sans text-2xl text-blue-800 border-b-2 border-yellow-200 mb-6">Top Page</h1> </body> </html>
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ './public/**/*.html', ], theme: { extend: {}, }, plugins: [], }
import Head from 'next/head';
export default function Home() { const title = "Next.js page"; const message = "React Next.js sample page.";
return (
{title}
{message}.
); }
- "npm run build" "npm run dev"で実行 動作確認 "http://localhost:3000" ## 第3章-3 設定ファイルを書く 【作成環境A】を使って説明 -------------------- - - Windows 11 - - Node.js - - Express - - Tailwind CSS --------------------- - //---bad theme:extend:無しは以下3色で他の色が使えなくなるので注意 ```js:tailwind.config.js /** @type {import('tailwindcss').Config} */ module.exports = { content: [ './public/**/*.html', ], theme: { colors: {//---bad theme:extend:無しは以下3色しか使えなくなる primary: '#0000ff', secondary: '#eeeeee', warning: '#ff0000', }, }, plugins: [], }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Top Page</title> <link href="/styles.css" rel="stylesheet"> </head> <body> <!-- カスタムカラーパレットから色を使った例 --> <div class="bg-primary text-secondary p-4"> <p>This is a primary colored div with secondary text.</p> </div> <!-- 他のTailwind CSSのクラスを使った例 --> <button class="bg-blue-300 text-white px-4 py-2 rounded">Warning Button</button> </body> </html>
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ './public/**/*.html', ], theme: { extend: { colors: {//---good theme:extend:で以下3色以外も使える primary: '#0000ff', secondary: '#eeeeee', warning: '#ff0000', }, }, }, plugins: [], }
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ './public/**/*.html', ], theme: { extend: { colors: {//---good 自作Tailwind CSSのクラスmypurpleを使った例 transparent: "transparent", current: 'currentColor', mypurple: '#3f3cbb', }, }, }, plugins: [], }
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Top Page</title> <link href="/styles.css" rel="stylesheet" /> </head> <body> <div class="text-secondary bg-blue-300 p-4"> <p>This is a primary colored div with secondary text.</p> </div> <!-- 自作Tailwind CSSのクラスmypurpleを使った例 --> <button class="rounded bg-mypurple px-4 py-2 text-white"> Warning Button </button> </body> </html>
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ './public/**/*.html', ], theme: { extend: { colors: {//---good theme:extend:で以下3色以外も使える transparent: "transparent", current: 'currentColor', mypurple: { //bg-mypurple-300 --> 2段ネストまでOK 100: '#cffafa', 200: '#a5f3fc', 300: '#67e8f9', 400: '#22d3ee', 500: '#06b6d4', 600: '#0891b2', 700: '#0e7490', 800: '#155e75', 900: '#164e63', DEFAULT: '#06b6d4', }, }, }, }, plugins: [], }
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Top Page</title> <link href="/styles.css" rel="stylesheet" /> </head> <body> <div class="text-secondary bg-blue-300 p-4"> <p>This is a primary colored div with secondary text.</p> </div> <!-- 自作Tailwind CSSのクラスmypurpleを使った例 --> <button class="bg-mypurple-300 rounded px-4 py-2 text-white"> Warning Button </button> </body> </html>
【Tailwind CSS実践入門 --> 動かして学ぶシリーズにしてみた】
第3章 Tailwind CSSをインストールする
-- ここで初学者は倒れて立ち上がれない気がするので動かして学ぶシリーズにしてみた
【作成環境A】
Tailwind CSS
ミッション1))Expressを立ち上げる
app.use(express.static("public"));
app.get("/", (req, res, next) => { res.end("Top Page"); });
app.listen(port, () => { console.log(
Server is running on http://localhost:${port}
); });ミッション2))ExpressでTailwind CSSの環境をつくる
async function build() { try { const { stdout, stderr } = await exec('npx tailwindcss build tailwind.css -o public/styles.css'); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (err) { console.error(err); } } build();
【作成環境B】
TypeScript
export default function Home() { const title = "Next.js page"; const message = "React Next.js sample page.";
return (
React
{title}
{message}.
); }