##2024/9/25 21:41:18:
vite react tailwind 构建个人主页,并上传到 cloudflare
代码:
https://github.com/qyzhizi/js-logical/tree/main/2024-09-25-vite-react-tailwind-pic-demo
效果:
https://vite-react-tailwind-pic-demo.pages.dev/home
从零开始构建
npm init -y
npm install react@18.0.0 react-dom@18.0.0 react-router-dom@6.3.0
npm install --save-dev @types/react @types/react-dom @vitejs/plugin-react autoprefixer postcss tailwindcss vite
代码目录
-> % tree -I 'node_modules' -a
.
├── .gitignore
├── index.html
├── package-lock.json
├── package.json
├── postcss.config.js
├── src
│ ├── App.jsx
│ ├── favicon.svg
│ ├── index.css
│ └── main.jsx
├── tailwind.config.js
└── vite.config.js
组件解释
function Image({ src, width }) {
return (
<img
src={src}
alt=""
style={{ width: `${width}px`, height: 'auto', objectFit: 'contain' }}
/>
);
}
这是一个 Image 函数,也是 react 组件 ,{ src, width } 是参数
使用方法:
<Image
src="https://openai-75050.gzc.vod.tencent-cloud.com/openaiassets_e07d6a54917baf0dc57fe6611f3da362_2579861727268007477.jpeg"
width={500}
/>
function Pictures() {
return (
<div className="space-y-16 text-center">
<h2 className="text-2xl">This is Pictures.</h2>
<div className="flex gap-4 flex-wrap items-start">
<Image
src="https://openai-75050.gzc.vod.tencent-cloud.com/openaiassets_e07d6a54917baf0dc57fe6611f3da362_2579861727268007477.jpeg"
width={500}
/>
<Image
src="https://openai-75050.gzc.vod.tencent-cloud.com/openaiassets_76099698d8cb082fbf1a4fd7bf37f76b_2579861727268062932.jpeg"
width={500}
/>
</div>
</div>
);
}
使用 PostCSS 和 Tailwind CSS 配置 Vite
To set up Vite with PostCSS and Tailwind CSS, follow these steps:
要使用 PostCSS 和 Tailwind CSS 设置 Vite,请按照以下步骤操作:
Install Dependencies 安装依赖项:
First, you need to install Vite, PostCSS, and Tailwind CSS. You can do this using npm or yarn:
首先,您需要安装Vite、PostCSS和Tailwind CSS。您可以使用 npm 或yarn 来完成此操作, 如果之前安装了这一步就不用安装:
npm install vite postcss tailwindcss
Or with yarn: 或者:
yarn add vite postcss tailwindcss
Initialize Tailwind CSS 初始化 Tailwind CSS:
After installing Tailwind CSS, initialize it to create a configuration file:
安装Tailwind CSS后,初始化它以创建配置文件:
npx tailwindcss init -p
This command creates two files:
该命令创建两个文件:
tailwind.config.js: where you configure Tailwind's settings.
tailwind.config.js :在其中配置 Tailwind 的设置。
postcss.config.js: where you can configure PostCSS plugins.
postcss.config.js :您可以在其中配置 PostCSS 插件。
Configure Tailwind 配置:
In your tailwind.config.js, specify the paths to all of your template files:
在tailwind.config.js中,指定所有模板文件的路径:
module.exports = {
content: ['./index.html', './src/**/*.{js,jsx,ts,tsx}'],
theme: {
extend: {},
},
plugins: [],
};
Add Tailwind Directives 添加顺风指令:
In your main CSS file (usually src/index.css), add the Tailwind directives:
在主 CSS 文件(通常是src/index.css )中,添加 Tailwind 指令:
@tailwind base;
@tailwind components;
@tailwind utilities;
Run Your Application 运行您的应用程序:
Finally, start your Vite development server:
最后,如果你源文件也完善,可以启动你的Vite开发服务器进行调试:
npm run dev
通过执行这些步骤,您将拥有一个完全配置的环境,可以使用 React 和 TypeScript 进行开发,并通过 Vite 和 PostCSS 使用 Tailwind CSS 进行样式设置。
vite 会自动识别 tailwind.config.js 与 postcss.config.js 吗?
Yes, Vite automatically recognizes and applies the configurations specified in tailwind.config.js and postcss.config.js. When you create these configuration files as part of setting up Tailwind CSS with Vite, Vite will read them during the build process.
是的,Vite 会自动识别并应用tailwind.config.js和postcss.config.js中指定的配置。当您创建这些配置文件作为使用 Vite 设置 Tailwind CSS 的一部分时,Vite 将在构建过程中读取它们。
Tailwind Configuration: The tailwind.config.js file is used to customize Tailwind's behavior, including specifying which files Tailwind should scan for class names. Vite will utilize this configuration to generate the appropriate CSS based on the classes used in your project.
Tailwind 配置: tailwind.config.js文件用于自定义 Tailwind 的行为,包括指定 Tailwind 应扫描哪些文件以查找类名。 Vite将利用此配置根据您项目中使用的类生成适当的CSS 。
Development Experience: This automatic handling enhances the development experience by allowing developers to focus on building their applications without worrying about manual configurations or additional steps to integrate these tools.
开发体验:这种自动处理允许开发人员专注于构建应用程序,而无需担心手动配置或集成这些工具的额外步骤,从而增强了开发体验 。
In summary, once you have created these configuration files in your Vite project, Vite will handle them appropriately during development and build processes.
总结一下,一旦你在你的 Vite 项目中创建了这些配置文件,Vite 将会在开发和构建过程中适当地处理它们。
Citations:
[1] https://blog.logrocket.com/setting-up-dev-environment-react-vite-tailwind/
[2] https://www.geeksforgeeks.org/how-to-setup-tailwind-css-with-vite/
[3] https://tailwindcss.com/docs/guides/vite
[4] https://tailwindcss.com/docs/content-configuration
[5] https://vitejs.dev/guide/features
[6] https://github.com/tailwindlabs/tailwindcss/issues/6553
[7] https://github.com/tailwindlabs/tailwindcss/discussions/11150
[8] https://reactrouter.com/en/main/upgrading/v5
npx tailwindcss init -p 这是什么意思?
The command npx tailwindcss init -p is used to initialize a new Tailwind CSS configuration file in your project while also creating a PostCSS configuration file. Here’s a breakdown of what it does:
命令npx tailwindcss init -p用于在项目中初始化新的 Tailwind CSS 配置文件,同时还创建 PostCSS 配置文件。下面是它的作用的详细说明:
npx: This is a package runner that comes with npm (Node Package Manager). It allows you to run commands from packages that are not globally installed on your machine.
npx :这是 npm(节点包管理器)附带的包运行器。它允许您运行计算机上未全局安装的软件包中的命令。
tailwindcss init: This part of the command generates a default configuration file for Tailwind CSS, named tailwind.config.js. This file allows you to customize various aspects of Tailwind, such as theme colors, spacing, and more. The generated file will contain minimal settings that you can expand upon as needed.
tailwindcss init :这部分命令会生成 Tailwind CSS 的默认配置文件,名为tailwind.config.js 。该文件允许您自定义 Tailwind 的各个方面,例如主题颜色、间距等。生成的文件将包含最少的设置,您可以根据需要进行扩展。
-p: The -p flag stands for "PostCSS" and indicates that you want to create a PostCSS configuration file (postcss.config.js) alongside the Tailwind configuration. This file is necessary for integrating Tailwind CSS with PostCSS, which processes your CSS files and applies Tailwind's utility classes.
-p : -p标志代表“PostCSS”,表示您想要在 Tailwind 配置旁边创建一个 PostCSS 配置文件 ( postcss.config.js )。此文件对于将 Tailwind CSS 与 PostCSS 集成是必需的,PostCSS 会处理您的 CSS 文件并应用 Tailwind 的实用程序类。
By running this command, you simplify the setup process for using Tailwind CSS in your project, ensuring that both the Tailwind and PostCSS configurations are correctly established for further development.
通过运行此命令,您可以简化在项目中使用 Tailwind CSS 的设置过程,确保正确建立 Tailwind 和 PostCSS 配置以供进一步开发 。
Citations:
[1] https://v1.tailwindcss.com/docs/configuration
[2] https://docs.plasmo.com/quickstarts/with-tailwindcss
[3] https://qiita.com/Masahiro111/items/62a4927be42a8c06bc79
[4] https://stackoverflow.com/questions/69176071/how-to-install-tailwindcss-in-react-js
[5] https://github.com/tailwindlabs/tailwindcss/discussions/11150
[6] https://zenn.dev/shuji_watanabe/articles/bcb4a3634d2ad8
[7] https://qiita.com/Masahiro111/items/251c370acb20156218de
[8] https://reactrouter.com/en/main/upgrading/v5
npm 包React": "18.0.0", 与 "@types/react": "18.0.0", 这两个包有什么区别?
To address your questions regarding the differences between react and @types/react, as well as the configuration of Vite with PostCSS and Tailwind CSS, here’s a detailed breakdown.
为了解决您有关react和@types/react之间的差异,以及 Vite 与 PostCSS 和 Tailwind CSS 的配置的问题,这里有一个详细的细分。
Differences Between react and @types/react
react和@types/react之间的区别
react: This is the core library that provides the React framework for building user interfaces. It includes the necessary functionalities, components, and hooks needed to create React applications. The version specified (e.g., ^18.0.0) indicates that you are using version 18 and any compatible minor updates.
react :这是提供用于构建用户界面的 React 框架的核心库。它包括创建 React 应用程序所需的必要功能、组件和挂钩。指定的版本(例如^18.0.0 )表示您正在使用版本 18 和任何兼容的次要更新。
@types/react: This package contains TypeScript type definitions for React. It allows TypeScript to understand the types used in React, enabling better type checking and autocompletion in TypeScript projects. The version specified (e.g., ^18.0.0) should match the version of React to ensure compatibility.
@types/react :此包包含 React 的 TypeScript 类型定义。它允许 TypeScript 理解 React 中使用的类型,从而在 TypeScript 项目中实现更好的类型检查和自动完成。指定的版本(例如^18.0.0 )应与 React 版本匹配以确保兼容性。
In summary, while react is the actual library for building applications, @types/react is used solely for TypeScript support, providing type definitions that help developers work more effectively with the React API in TypeScript environments.
总之,虽然react是用于构建应用程序的实际库,但@types/react仅用于 TypeScript 支持,提供类型定义,帮助开发人员在 TypeScript 环境中更有效地使用 React API。