React創(chuàng)建配置項(xiàng)目的實(shí)現(xiàn)
tips:所有配置文件均配置在項(xiàng)目的根目錄下
1.初始化一個(gè)前端項(xiàng)目
新建一個(gè)項(xiàng)目文件夾:npm init -y

創(chuàng)建文件目錄如下
- src/ -index.ts//項(xiàng)目入口文件 - package.json
2.添加TypeScript(可選/默認(rèn)JS)
安裝TS,要先在全局安裝有yarn包管理工具,--dev命令將ts安裝為開(kāi)發(fā)環(huán)境的依賴,不需要在生產(chǎn)環(huán)境中運(yùn)行。

進(jìn)行TS配置,在項(xiàng)目根目錄通過(guò)tsc --init命令創(chuàng)建tsconfig.json文件

如果報(bào)錯(cuò):zsh: command not found: tsc。通常是因?yàn)?tsc 命令未被正確識(shí)別??赡苁且?yàn)?TypeScript 沒(méi)有全局安裝,或者你沒(méi)有正確配置路徑來(lái)運(yùn)行 tsc。這里我們不需要全局安裝,所以我們直接配置路徑。
確認(rèn)路徑設(shè)置(針對(duì) zsh 用戶)
( 可以通過(guò) vi ~/.zshrc打開(kāi).zshrc文件,鍵盤(pán)敲I鍵進(jìn)入編輯模式)
在 ~/.zshrc 文件中,添加以下行(如果未添加):
export PATH="$PATH:./node_modules/.bin"
輸入:wq退出并保存文件,然后應(yīng)用更改
source ~/.zshrc
通過(guò)tsc --version查看版本,如果出現(xiàn)版本說(shuō)明路徑設(shè)置成功
配置tsconfig.json文件
{
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"lib": ["esnext", "dom"],
"baseUrl": ".",
"jsx": "react-jsx",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"strict": true,
"paths": {
"@/*": ["./src/*"]
},
"noEmit": true
},
"include": [
"src/**/*",
"typings/**/*",
"config/**/*",
".eslintrc.js",
".stylelintrc.js",
".prettierrc.js"
],
"exclude": ["node_modules", "build", "dist"]
}
3.添加ESLint
yarn add eslint eslint-plugin-react eslint-plugin-react-hooks @typescript-eslint/parser @typescript-eslint/eslint-plugin --dev
配置.eslintrc.json文件
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "react-hooks"],
"extends": [
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"react/prop-types": "off",
"@typescript-eslint/explicit-module-boundary-types": "off"
}
}
配置.eslintignore文件
# 忽略目錄 .DS_Store node_modules dist build public
4.添加Prettier
yarn add prettier --dev
配置.prettierrc文件(不允許注釋)
{
"printWidth": 100,
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always",
"requirePragma": false,
"proseWrap": "preserve"
}
或者配置 .prettierrc.js文件(允許注釋)
module.exports={
"printWidth": 100, // 換行字符串閾值
"semi": true, // 句末加分號(hào)
"singleQuote": true, // 用單引號(hào)
"tabWidth": 2,
"trailingComma": "all", // 最后一個(gè)對(duì)象元素加逗號(hào)
"bracketSpacing": true, // 對(duì)象,數(shù)組加空格
"jsxBracketSameLine": false, // jsx > 是否另起一行
"arrowParens": "always", // (x) => {} 是否要有小括號(hào)
"requirePragma": false, // 是否要注釋來(lái)決定是否格式化代碼
"proseWrap": "preserve" // 是否要換行
}
配置.prettierignore文件
*.svg package.json .DS_Store .eslintignore *.png *.toml .editorconfig .gitignore .prettierignore LICENSE .eslintcache *.lock yarn-error.log /build /public
5.添加 EditorConfig 代碼風(fēng)格統(tǒng)一工具
在VSCode 中安裝 EditorConfig 插件
配置.editorconfig文件
# http://editorconfig.org root = true [*] #縮進(jìn)風(fēng)格:空格 indent_style = space #縮進(jìn)大小2 indent_size = 2 #換行符lf end_of_line = lf #字符集utf-8 charset = utf-8 #是否刪除行尾的空格 trim_trailing_whitespace = true #是否在文件的最后插入一個(gè)空行 insert_final_newline = true [*.md] trim_trailing_whitespace = false [Makefile] indent_style = tab
6.stylelint樣式規(guī)范校驗(yàn)工具
VScode中安裝stylelint插件
安裝依賴
yarn add stylelint stylelint-config-standard --dev
配置.stylelintrc.js文件
module.exports = {
extends: "stylelint-config-standard",
rules: {
// your rules
},
// 忽略其他文件,只校驗(yàn)樣式相關(guān)的文件
ignoreFiles: [
"node_modules/**/*",
"public/**/*",
"dist/**/*",
"**/*.js",
"**/*.jsx",
"**/*.tsx",
"**/*.ts",
],
};
7.添加git hook(git提交前的強(qiáng)制校驗(yàn),不滿足條件不讓提交)
- husky 是一個(gè) gitHook 工具,可以配置 git 的一些鉤子,本文主要用來(lái)配置 commit 鉤子
- lint-staged 是一個(gè)在 git 暫存文件上運(yùn)行 lint 校驗(yàn)的工具,配合 husky 配置 commit 鉤子,用于 git commit 前的強(qiáng)制校驗(yàn)
安裝依賴(下面兩條命令二選一)
yarn add husky lint-staged --dev npm install husky lint-staged --save-dev
8.基本項(xiàng)目結(jié)構(gòu)

index.html內(nèi)容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>react-app</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
9.安裝react
yarn add react react-dom yarn add @types/react @types/react-dom --dev
10.添加react根組件
在src下添加index.tsx文件,內(nèi)容如下
import { createRoot } from 'react-dom/client';
import React from 'react';
const App = () => {
return (<h1>你好!</h1>);
};
// 這里用非空斷言說(shuō)明root這個(gè)根結(jié)點(diǎn)是一定存在的
const root = createRoot(document.getElementById("root")!);
root.render(<App />);11.添加Babel
需要使用 Babel 將 React 和 TypeScript 代碼轉(zhuǎn)換為 JavaScript。接下來(lái)我們安裝一些 Babel 工具
yarn add @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript @babel/plugin-transform-runtime @babel/runtime --dev
配置 .babelrc文件
{
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"regenerator": true
}
]
]
}
12.添加webpack
yarn add webpack webpack-cli @types/webpack --dev yarn add webpack-dev-server @types/webpack-dev-server --dev yarn add babel-loader --dev yarn add html-webpack-plugin --dev
開(kāi)發(fā)環(huán)境文件config/webpack.dev.js
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const config = {
mode: "development",
entry: {
main: path.resolve(__dirname, "../src/index.tsx"),
},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "../build"),
},
module: {
rules: [
{
test: /\.(ts|js)x?$/i,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript",
],
plugins: [
[
"@babel/plugin-transform-runtime",
{
regenerator: true,
},
],
],
},
},
},
],
},
resolve: {
alias: {
"@": path.resolve(__dirname, "../src"),
},
extensions: [".tsx", ".ts", ".jsx", ".js"],
},
plugins: [
new HtmlWebpackPlugin({
template: "public/index.html",
}),
new webpack.HotModuleReplacementPlugin(),
],
devtool: "inline-source-map",
devServer: {
static:{
directory: '../build',
},
historyApiFallback: true,
port: 4000,
hot: true,
},
};
module.exports = config;到此這篇關(guān)于React創(chuàng)建配置項(xiàng)目的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)React創(chuàng)建配置項(xiàng)目?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react中的watch監(jiān)視屬性-useEffect介紹
這篇文章主要介紹了react中的watch監(jiān)視屬性-useEffect使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
React中useEffect與生命周期鉤子函數(shù)的對(duì)應(yīng)關(guān)系說(shuō)明
這篇文章主要介紹了React中useEffect與生命周期鉤子函數(shù)的對(duì)應(yīng)關(guān)系說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
React實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果(樓梯效果)
這篇文章主要為大家詳細(xì)介紹了React實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果,樓梯效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
react 下拉框內(nèi)容回顯的實(shí)現(xiàn)思路
這篇文章主要介紹了react 下拉框內(nèi)容回顯,實(shí)現(xiàn)思路是通過(guò)將下拉框選項(xiàng)的value和label一起存儲(chǔ)到state中, 初始化表單數(shù)據(jù)時(shí)將faqType對(duì)應(yīng)的label查找出來(lái)并設(shè)置到Form.Item中,最后修改useEffect,需要的朋友可以參考下2024-05-05
useState?解決文本框無(wú)法輸入的問(wèn)題詳解
這篇文章主要為大家介紹了useState?解決文本框無(wú)法輸入的問(wèn)題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

