当前位置: 首页 > news >正文

TypeScript React(下)

目录

TypeScript & React

TS开发环境的搭建

tsconfig.json

webpack.config.js

babel.config.js

.eslintrc.js


TypeScript & React

TS开发环境的搭建

  • 软件版本:TypeScript:3.9.5;React:16.13.1 Node:8.17.0
  • 环境搭建:正确搭建一个通用的TypeScript开发环境

差异:

  • tsconfig->babel->eslint

tsconfig.json

{"compilerOptions": {"outDir": "./","target": "esnext","allowJs": true,"noEmit": true,"skipLibCheck": true,"esModuleInterop": true,"allowSyntheticDefaultImports": true,"strict": true,"forceConsistentCasingInFileNames": true,"module": "esnext","moduleResolution": "node","resolveJsonModule": true,"experimentalDecorators": true,"noUnusedParameters": true,"noUnusedLocals": true,"jsx": "react","sourceMap": true,"pretty": true,"baseUrl": ".","paths": {"@/*": ["./src/*"]},},  "typeRoots": ["node_modules/@types","src/types"],"include": ["src/**/*"],"exclude": ["node_modules","dist","**/*.spec.ts"],
}

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const devPort = parseInt(process.env.port, 10) || 8065;
module.exports = (env) => {const publicPath = '';const buildPath = './build';const config = {mode: env.prod ? 'production' : 'development',entry: {app: path.join(__dirname, './src/index.tsx'),},devServer: {historyApiFallback: true,contentBase: path.join(__dirname, buildPath),port: devPort,hot: true,disableHostCheck: true,},output: {path: path.join(__dirname, buildPath),pathinfo: true,filename: env.prod || env.test ? '[name].[hash].js' : '[name].js',publicPath: `${publicPath}/`,},resolve: {alias: {'@': path.resolve(__dirname, './src'),},extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],},module: {rules: [{test: /(\.ts|\.tsx)$/,exclude: /(node_modules)/,use: [{// 新增支持ts编译loader: 'babel-loader',},],},{test: /\.css$/,exclude: /node_modules/,use: [{loader: 'style-loader',},{loader: 'css-loader',options: {importLoaders: 1,},},{loader: 'postcss-loader',},],},],},plugins: [new webpack.DefinePlugin({'process.env.ENV': env.dev,}),new webpack.LoaderOptionsPlugin({ options: {} }),new HtmlWebpackPlugin({title: 'React & TypeScript',template: path.join(__dirname, './src/view/index.html'),inject: true,hash: false,filename: 'index.html',publicPath: '',}),],devtool: 'source-map',watch: true,};return config;
};

babel.config.js

module.exports = (api) => {api.cache(true);const plugins = [require('@babel/plugin-transform-runtime'),[require('@babel/plugin-proposal-decorators'), { legacy: true }],[require('@babel/plugin-proposal-class-properties'), { loose: true }],require('@babel/plugin-proposal-export-default-from'),// require("react-hot-loader/babel"),];const presets = [[require('@babel/preset-env'),{modules: false,useBuiltIns: 'usage',corejs: { version: 3, proposals: true },},],[require('@babel/preset-react')],// 插件将ts、tsx转译为js[require('@babel/preset-typescript')],];return {plugins,presets,};
};

.eslintrc.js

const path = require('path');
module.exports = {root: true,// 新增ts的编译parser: '@typescript-eslint/parser',extends: ['airbnb-typescript','airbnb/hooks',// 新增tslint推荐'plugin:@typescript-eslint/recommended','plugin:@typescript-eslint/recommended-requiring-type-checking',],// 新增@typescript-eslint配置项plugins: ['@typescript-eslint', 'jsx-a11y', 'import', 'react-hooks'],parserOptions: {ecmaVersion: 2019,sourceType: 'module',// 关联ts配置文境project: './tsconfig.json',},settings: {'import/resolver': {webpack: {config: {resolve: {alias: {'@': path.resolve(__dirname, './src'),},extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],},},},},},env: {browser: true,},globals: {window: true,ENV: true,},rules: {indent: ['error', 2],quotes: ['error', 'single'],semi: ['error', 'always'],'no-console': 1,'arrow-parens': 0,'no-shadow': 'off','react/jsx-filename-extension': 'off','react/jsx-props-no-spreading': 0,// 自定义规则'@typescript-eslint/consistent-type-definitions': ['error', 'interface'],'@typescript-eslint/explicit-function-return-type': 'off','@typescript-eslint/no-explicit-any': 0,'import/extensions': ['error','ignorePackages',{ts: 'never',tsx: 'never',js: 'never',jsx: 'never',json: 'never',},],'import/no-unresolved': [2, { commonjs: true, amd: true }],},
};

http://www.lryc.cn/news/193057.html

相关文章:

  • 『Linux小程序』进度条
  • 【手写数字识别】GPU训练版本
  • c#-特殊的集合
  • Android 使用 eChart 设置标线
  • 红队专题-Cobalt strike 4.x - Beacon重构
  • 一文掌握 Go 文件的写入操作
  • 小程序入门及案例展示
  • linux 安装python django pip 遇到的问题
  • 【问题解决】【爬虫】抓包工具charles与pycharm发送https请求冲突问题
  • Hadoop3教程(二):HDFS的定义及概述
  • 【物联网+JAVA 】智慧工地源码
  • 001数据安全传输-多端协议传输平台:Openssl安装和配置 - EVP代码测试
  • 关于小编入坑第512天
  • VS2015编译Qt工程发生MSB4018错误完整解决过程
  • 如何使用JMeter测试导入接口/导出接口
  • [入门一]C# webApi创建、与发布、部署、api调用
  • 关于Vue+webpack使用unocss编写CSS,打包后CSS没加前缀
  • 软件工程与计算总结(十一)人机交互设计
  • Jmeter组件执行顺序与作用域
  • 第一天商城项目
  • C++笔记之通用多态函数包装器std::function
  • Linux命令(92)之passwd
  • 光电柴微电网日前调度报告
  • Godot 单元测试
  • 2.9 深入GPU硬件架构及运行机制
  • 【苍穹外卖 | 项目日记】第四天
  • 零代码编程:用ChatGPT批量采集bookroo网页上的英文书目列表
  • 7.定时器
  • 计算机网络 | 网络层
  • 21GA-ELM,遗传算法优化ELM预测,并和优化前后以及真实数值进行对比,确定结果,基于MATLAB平台,程序已经调通,可以直接运行,需要直接拍下。