环境
- vscode
- mac
- react 18
- webpack
- typescript
- npm
一、开发环境搭建
vscode搭建react-typescript-webpack
1. 创建项目文件夹及webpack配置文件、html入口文件
mkdir react-ts-webpack-demo
cd react-ts-webpack-demo
touch webpack.config.js index.html
2. 初始化npm和创建入口文件index.tsx
npm init -y
- 生成最小粒度的package.json配置文件,之后使用npm安装的依赖包都会放在
package.json 文件中。 - 创建src文件夹将入口文件index.tsx放进去,此时项目结构如下所示:
3. 安装webpack\react
npm i webpack webpack-cli webpack-dev-server -D
npm i react react-dom -S
此时的packages.json文件自动配置安装的五个依赖包
4. 配置webpack.config.js
(1)安装html-webpack-plugin、clean-webpack-plugin、awesome-typescript-loader、typescript
npm i html-webpack-plugin --dev
npm i clean-webpack-plugin -S
npm i typescript ts-loader -D
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
module.exports = {
mode: "development",
entry: "./src/index.tsx",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "index.html"),
}),
new CleanWebpackPlugin({}),
],
};
5. 创建和配置tsconfig.json文件
添加 tsconfig.json 文件,告诉 webpack 去哪儿寻找 typescript 文件以及编译选项配置,配置内容如下所示:
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node"
}
}
6. 安装@react文件
使用@types/前缀表示我们额外要获取 React 和 React-DOM 的声明文件
npm i @types/react @types/react-dom -D
7. 到package.json修改启动项目的方式
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "webpack-dev-server",
"build": "webpack"
},
现在可以使用npm run serve 启动项目,npm run build 打包项目,打开之后是空白页面,因为还没有加入DOM元素对页面进行渲染。 目录结构如下所示: packages.json文件如下所示:
{
"name": "react-ts-webpack-demo",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "webpack-dev-server",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"clean-webpack-plugin": "^4.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "^5.0.1"
},
"devDependencies": {
"@types/react": "^18.0.14",
"@types/react-dom": "^18.0.5",
"html-webpack-plugin": "^5.5.0",
"ts-loader": "^9.3.1",
"typescript": "^4.7.4",
"webpack": "^5.73.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.9.2"
}
}
ps:以上步骤参照文章:https://zhuanlan.zhihu.com/p/59023070,但是做的过程中发现页面渲染部分有问题,所以在文章的基础上重新做了改进。
8. 配置index.html 、入口文件index.tsx
index.html
在body标签里面创建一个id为root的div,方便入口文件获取
<!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">
// 将项目标题改为从htmlwebpackplugin中动态获取
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<div id="root"></div>
</body>
</html>
使用createReact获取根元素root后将DOM渲染到该根元素下面:
import * as React from "react";
import { createRoot } from "react-dom/client";
const root = createRoot(document.getElementById("root"));
root.render(<h1>Hello world!</h1>);
react 18 用createRoot取代React17 的render函数去渲染DOM元素,开发的时候遇到了DOM没有渲染出来的问题,可以参考附录的开发过程记录文章区解决。
// 持续更新中。。。
附录
开发过程错误记录
- react 18 createRoot没有渲染出DOM元素
- 运行时出现Uncaught ReferenceError: process is not defined错误提示
|