• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用webpack建立React+TS项目

武飞扬头像
守护砂之国泰裤辣
帮助1

之前写过类似的文章,这次看到一本新书里也介绍了这个知识点,故尝试之。

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.先建立一个空的文件夹,my-app,并用vscode打开然后到根目录底下创建package.json和src目录,并在其中添加index.html:

  1.  
    {
  2.  
    "name": "my-app",
  3.  
    "version": "1.0.0",
  4.  
    "description": "My React and TypeScript app"
  5.  
    }
  1.  
    <!DOCTYPE html>
  2.  
    <html>
  3.  
    <head>
  4.  
    <meta charset="utf-8" />
  5.  
    <title>My app</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    <div id="root"></div>
  9.  
    </body>
  10.  
    </html>

学新通

2.安装和配置ts:

npm install -D typescript

 根目录新建tsconfig.json文件:

  1.  
    {
  2.  
    "compilerOptions": {
  3.  
    "noEmit": true,
  4.  
    "lib": ["dom", "dom.iterable", "esnext"],
  5.  
    "moduleResolution": "node",
  6.  
    "allowSyntheticDefaultImports": true,
  7.  
    "esModuleInterop": true,
  8.  
    "jsx": "react",
  9.  
    "forceConsistentCasingInFileNames": true,
  10.  
    "strict": true
  11.  
    },
  12.  
    "include": ["src"],
  13.  
    "exclude": ["node_modules", "dist"]
  14.  
    }

3.安装React:

npm install react react-dom

安装类型(react包本身不含类型):

 npm install @types/react @types/react-dom

4.在src目录地下创建index.tsx:

  1.  
    import React, { StrictMode } from "react";
  2.  
    import { createRoot } from "react-dom/client";
  3.  
     
  4.  
    const root = createRoot(document.getElementById("root") as HTMLElement);
  5.  
     
  6.  
    function App() {
  7.  
    return <h1>My React and TypeScript App!</h1>;
  8.  
    }
  9.  
     
  10.  
    root.render(
  11.  
    <StrictMode>
  12.  
    <App />
  13.  
    </StrictMode>
  14.  
    );

5.安装Babel:

npm i -D @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript @babel/plugin-transform-runtime @babel/runtime

根目录创建.babelrc.json:

  1.  
    {
  2.  
    "presets": [
  3.  
    "@babel/preset-env",
  4.  
    "@babel/preset-react",
  5.  
    "@babel/preset-typescript"
  6.  
    ],
  7.  
    "plugins": [
  8.  
    [
  9.  
    "@babel/plugin-transform-runtime",
  10.  
    {
  11.  
    "regenerator": true
  12.  
    }
  13.  
    ]
  14.  
    ]
  15.  
    }
学新通

6.安装webpack

 npm i -D webpack webpack-cli webpack-dev-server babel-loader html-webpack-plugin

7.配置webpack

a.安装node-ts库允许在ts文件中配置: 

npm i -D ts-node

b.根目录上创建一个文件webpack.dev.config.ts:

  1.  
    import path from "path";
  2.  
    import HtmlWebpackPlugin from "html-webpack-plugin";
  3.  
    import {
  4.  
    Configuration as WebpackConfig,
  5.  
    HotModuleReplacementPlugin,
  6.  
    } from "webpack";
  7.  
    import { Configuration as WebpackDevServerConfig } from "webpack-dev-server";
  8.  
     
  9.  
    type Configuration = WebpackConfig & {
  10.  
    devServer?: WebpackDevServerConfig;
  11.  
    };
  12.  
     
  13.  
    const config: Configuration = {
  14.  
    mode: "development",
  15.  
    output: {
  16.  
    publicPath: "/",
  17.  
    },
  18.  
    entry: "./src/index.tsx",
  19.  
    module: {
  20.  
    rules: [
  21.  
    {
  22.  
    test: /\.(ts|js)x?$/i,
  23.  
    exclude: /node_modules/,
  24.  
    use: {
  25.  
    loader: "babel-loader",
  26.  
    options: {
  27.  
    presets: [
  28.  
    "@babel/preset-env",
  29.  
    "@babel/preset-react",
  30.  
    "@babel/preset-typescript",
  31.  
    ],
  32.  
    },
  33.  
    },
  34.  
    },
  35.  
    ],
  36.  
    },
  37.  
    resolve: {
  38.  
    extensions: [".tsx", ".ts", ".js"],
  39.  
    },
  40.  
    plugins: [
  41.  
    new HtmlWebpackPlugin({
  42.  
    template: "src/index.html",
  43.  
    }),
  44.  
    new HotModuleReplacementPlugin(),
  45.  
    ],
  46.  
    devtool: "inline-source-map",
  47.  
    devServer: {
  48.  
    static: path.join(__dirname, "dist"),
  49.  
    historyApiFallback: true,
  50.  
    port: 4000,
  51.  
    open: true,
  52.  
    hot: true,
  53.  
    },
  54.  
    };
  55.  
     
  56.  
    export default config;
学新通

c.在package.json中追加启动脚本:

  1.  
    ,
  2.  
    "scripts": {
  3.  
    "start": "webpack serve --config webpack.dev.config.ts"
  4.  
    }

8.允许app,命令行使用:

npm start

运行结果:

学新通

 源码

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfhebg
系列文章
更多 icon
同类精品
更多 icon
继续加载