之前寫過類似的文章,這次看到一本新書里也介紹了這個(gè)知識(shí)點(diǎn),故嘗試之。
Refer: 《Learn React With TypeScript - A Beginner's Guide To Reactive Web Development With React 18 and TypeScript》chapter3?Creating a project with webpack
1.先建立一個(gè)空的文件夾,my-app,并用vscode打開然后到根目錄底下創(chuàng)建package.json和src目錄,并在其中添加index.html:
{
"name": "my-app",
"version": "1.0.0",
"description": "My React and TypeScript app"
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My app</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
2.安裝和配置ts:
npm install -D typescript
?根目錄新建tsconfig.json文件:
{
"compilerOptions": {
"noEmit": true,
"lib": ["dom", "dom.iterable", "esnext"],
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"jsx": "react",
"forceConsistentCasingInFileNames": true,
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
3.安裝React:
npm install react react-dom
安裝類型(react包本身不含類型):
npm install @types/react @types/react-dom
4.在src目錄地下創(chuàng)建index.tsx:
import React, { StrictMode } from "react";
import { createRoot } from "react-dom/client";
const root = createRoot(document.getElementById("root") as HTMLElement);
function App() {
return <h1>My React and TypeScript App!</h1>;
}
root.render(
<StrictMode>
<App />
</StrictMode>
);
5.安裝Babel:
npm i -D @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript @babel/plugin-transform-runtime @babel/runtime
根目錄創(chuàng)建.babelrc.json:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"regenerator": true
}
]
]
}
6.安裝webpack
npm i -D webpack webpack-cli webpack-dev-server babel-loader html-webpack-plugin
7.配置webpack
a.安裝node-ts庫(kù)允許在ts文件中配置:?
npm i -D ts-node
b.根目錄上創(chuàng)建一個(gè)文件webpack.dev.config.ts:
import path from "path";
import HtmlWebpackPlugin from "html-webpack-plugin";
import {
Configuration as WebpackConfig,
HotModuleReplacementPlugin,
} from "webpack";
import { Configuration as WebpackDevServerConfig } from "webpack-dev-server";
type Configuration = WebpackConfig & {
devServer?: WebpackDevServerConfig;
};
const config: Configuration = {
mode: "development",
output: {
publicPath: "/",
},
entry: "./src/index.tsx",
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",
],
},
},
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
plugins: [
new HtmlWebpackPlugin({
template: "src/index.html",
}),
new HotModuleReplacementPlugin(),
],
devtool: "inline-source-map",
devServer: {
static: path.join(__dirname, "dist"),
historyApiFallback: true,
port: 4000,
open: true,
hot: true,
},
};
export default config;
c.在package.json中追加啟動(dòng)腳本:
,
"scripts": {
"start": "webpack serve --config webpack.dev.config.ts"
}
8.允許app,命令行使用:
npm start
運(yùn)行結(jié)果:
文章來源:http://www.zghlxwxcb.cn/news/detail-630708.html
?源碼文章來源地址http://www.zghlxwxcb.cn/news/detail-630708.html
到了這里,關(guān)于使用webpack建立React+TS項(xiàng)目的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!