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


    一、创建一个next项目

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

    运行命令:

    npx create-next-app@latest

    执行结果如下: 

     启动项目:

    pnpm dev

    执行结果: 

    启动成功! 

    二、安装Material UI依赖

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

    运行命令:

    pnpm add @mui/material @emotion/styled @emotion/react @emotion/cache @mui/icons-material

     三、使用prettier美化项目代码

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

    运行命令:

    pnpm add prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react-hooks -D

    prettier文件配置.prettierrc.json

    1. {
    2. "semi": true,
    3. "endOfLine": "auto",
    4. "singleQuote": true,
    5. "trailingComma": "none",
    6. "bracketSpacing": true,
    7. "jsxBracketSameLine": false,
    8. "vueIndentScriptAndStyle": false,
    9. "jsxBracketSameLine:": true,
    10. "htmlWhitespaceSensitivity": "ignore",
    11. "wrapAttributes": true,
    12. "overrides": [
    13. {
    14. "files": "*.html",
    15. "options": {
    16. "parser": "html"
    17. }
    18. }
    19. ]
    20. }

    安装eslint相关依赖:

    运行命令:

    pnpm  add @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-import-resolver-typescript -D

    eslint文件.eslintrc.json配置:

    1. {
    2. "root": true,
    3. "extends": ["eslint:recommended", "next","prettier"],
    4. "env": {
    5. "es6": true,
    6. "node": true,
    7. "browser": true
    8. },
    9. "parserOptions": {
    10. "ecmaVersion": 12,
    11. "parser": "@typescript-eslint/parser"
    12. },
    13. "plugins": ["prettier", "react-hooks","@typescript-eslint"],
    14. "parser": "@typescript-eslint/parser",
    15. "globals": {
    16. "chrome": true,
    17. "React":true
    18. },
    19. "settings": {
    20. "import/resolver": {
    21. "node": {
    22. "extensions": [".js", ".jsx", ".ts", ".tsx"]
    23. }
    24. }
    25. },
    26. "rules": {
    27. "prettier/prettier":"error"
    28. }
    29. }

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

    为了使用Prettier自动排序tailwind CSS类,我们需要安装Prettier -plugin-tailwindcss。
    运行如下命令安装插件:

    pnpm add -D prettier-plugin-tailwindcss

    prettier文件配置.prettierrc.json:

    1. {
    2. "semi": true,
    3. "endOfLine": "auto",
    4. "singleQuote": true,
    5. "trailingComma": "none",
    6. "bracketSpacing": true,
    7. "jsxBracketSameLine": false,
    8. "vueIndentScriptAndStyle": false,
    9. "jsxBracketSameLine:": true,
    10. "htmlWhitespaceSensitivity": "ignore",
    11. "wrapAttributes": true,
    12. "plugins": ["prettier-plugin-tailwindcss"],
    13. "overrides": [
    14. {
    15. "files": "*.html",
    16. "options": {
    17. "parser": "html"
    18. }
    19. }
    20. ]
    21. }

    五、安装sass

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

    pnpm add sass -D

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

  • 相关阅读:
    力扣labuladong一刷day8共2题
    【光电工程实训】光通信与光纤通信 电子元器件认知 万用表测量参数 元件特性 激光传声实验 自由空间光通讯 光纤光通信 全反射
    纸张大小和铅笔的规格简述
    windows + logrotate 按天切割 D:\Tomcat8.5\logs\tomcat8-stdout.log的日志
    Vue3 从入门到放弃 (第四篇.Props使用)
    【Python 实战基础】Pandas如何给股票数据新增年份和月份
    数据库系统工程师------流水线
    AI算法工程师的寒冬?我们完全可以反向思维。。。
    【加密算法】RSA和AES在项目中的使用
    SQL改写系列九:外连接转内连接的常见场景与错误
  • 原文地址:https://blog.csdn.net/sinat_36728518/article/details/133794534