npx create-next-app@latest my-blog
用最新的Next.js创建应用,选择typescript、eslint、tailwind css、app router、src/app目录结构。
// .npmrc
registry="https://registry.yarnpkg.com/"
使用Pnpm安装依赖,节省磁盘空间,配置npm源,使用yarn的源,解决npm源被墙的问题。
// .VSCode settings.json
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
编辑器设置保存自动使用Prettier格式化代码,使用ESLint检查代码。
// prettier.config.js
module.exports = {
plugins: ['prettier-plugin-tailwindcss'],
semi: false,
singleQuote: true,
'prettier.printWidth': 120,
}
// .eslintrc.json
{
"extends": ["next/core-web-vitals", "prettier"],
}
Prettier 配置,使用Tailwind CSS插件,可以自动调整样式顺序,使用单引号,120字符换行,不需要分号。
ESLint 配置,使用Next.js核心规则,使用Prettier规则。
pnpm i
pnpm dev
完成以上配置,即可开始开发,访问 http://localhost:3000 即可看到效果。