nextjs构建服务端渲染,同时使用Material UI进行项目配置

一、创建一个next项目

使用create-next-app来启动一个新的Next.js应用,它会自动为你设置好一切

运行命令:

javascript 复制代码
npx create-next-app@latest

执行结果如下:

启动项目:

javascript 复制代码
pnpm dev

执行结果:

启动成功!

二、安装Material UI依赖

根据Material UI官网介绍,截至2021年底,样式组件与服务器渲染的材质UI项目不兼容。这是因为babel-plugin风格的组件不能与@mui包中的styled()实用程序一起工作。有关详细信息,请参阅此GitHub问题。我们强烈建议在SSR项目中使用Emotion。

运行命令:

javascript 复制代码
pnpm add @mui/material @emotion/styled @emotion/react @emotion/cache @mui/icons-material

三、使用prettier美化项目代码

安装prettier相关依赖,及其文件配置

运行命令:

javascript 复制代码
pnpm add prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react-hooks -D

prettier文件配置.prettierrc.json

javascript 复制代码
{
  "semi": true,
  "endOfLine": "auto",
  "singleQuote": true,
  "trailingComma": "none",
  "bracketSpacing": true,
  "jsxBracketSameLine": false,
  "vueIndentScriptAndStyle": false,
  "jsxBracketSameLine:": true,
  "htmlWhitespaceSensitivity": "ignore",
  "wrapAttributes": true,
  "overrides": [
    {
      "files": "*.html",
      "options": {
        "parser": "html"
      }
    }
  ]
}

安装eslint相关依赖:

运行命令:

javascript 复制代码
pnpm  add @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-import-resolver-typescript -D

eslint文件.eslintrc.json配置:

javascript 复制代码
{
  "root": true,
  "extends": ["eslint:recommended", "next","prettier"],
  "env": {
    "es6": true,
    "node": true,
    "browser": true
  },
  "parserOptions": {
    "ecmaVersion": 12,
    "parser": "@typescript-eslint/parser"
  },
  "plugins": ["prettier", "react-hooks","@typescript-eslint"],
  "parser": "@typescript-eslint/parser",
  "globals": {
    "chrome": true,
    "React":true
  },
  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  },
  "rules": {
    "prettier/prettier":"error"
  }
}

四、使用Prettier自动排序tailwind CSS类

为了使用Prettier自动排序tailwind CSS类,我们需要安装Prettier -plugin-tailwindcss。

运行如下命令安装插件:

javascript 复制代码
pnpm add -D prettier-plugin-tailwindcss

prettier文件配置.prettierrc.json:

javascript 复制代码
{
  "semi": true,
  "endOfLine": "auto",
  "singleQuote": true,
  "trailingComma": "none",
  "bracketSpacing": true,
  "jsxBracketSameLine": false,
  "vueIndentScriptAndStyle": false,
  "jsxBracketSameLine:": true,
  "htmlWhitespaceSensitivity": "ignore",
  "wrapAttributes": true,
  "plugins": ["prettier-plugin-tailwindcss"],
  "overrides": [
    {
      "files": "*.html",
      "options": {
        "parser": "html"
      }
    }
  ]
}

五、安装sass

虽然项目当中已经安装了tailwind css进行样式处理,但是有时候不可避免的需要写点样式.因此,安装sass进行,样式的编写:

javascript 复制代码
pnpm add sass -D

至此,一个使用Material UI组件,使用eslint、Prettier进行规范、美化代码的next项目就搭建完毕了。开始你的炫酷旅程吧!

相关推荐
gentle coder1 天前
【langchain】agent部署的基础入门代码(持续更新中~)
python·langchain·react
RichardLau_Cx2 天前
【保姆级实操】MediaPipe SDK/API 前端项目接入指南(Web版,可直接复制代码)
前端·vue·react·webassembly·mediapipe·手部追踪·前端计算机视觉
小小工匠2 天前
大模型开发 - 零手写 AI Agent:深入理解 ReAct 模式与 Java 实现
人工智能·react
雪域迷影3 天前
MacOS中运行Next.js项目注册新用户时MongoDB报错MongoServerError
mongodb·macos·react·next.js
Highcharts.js3 天前
使用Highcharts与React集成 官网文档使用说明
前端·react.js·前端框架·react·highcharts·官方文档
im_AMBER5 天前
Web文件下载 : 从PDF预览Bug到Hook架构演进
前端·架构·react·浏览器·blob
shadow fish6 天前
React学习笔记(二)
前端·javascript·react
Marshmallowc11 天前
React 刷新页面 Token 消失?深度解析 Redux + LocalStorage 数据持久化方案与 Hook 避坑指南
javascript·react·数据持久化·redux·前端工程化
还是码字踏实14 天前
智能体平台Dify的 模型接口与 Agent 架构深度解析
react·抽象工厂模式·function call·模型插件系统
guangzan16 天前
Agent Skill: react-best-practices
ai·react·agents·skills