创建文件 ts-demo
npm init -y
npm i typescript -D
初始化 ts配置项
npx tsc --init
ts-demo│ README.md│└───build│ │ webpack.base.config.js│ │ webpack.config.js│ │ webpack.pro.config.js| | webpack.dev.config.js│└───public| | index.html| |└───src| │ template| │ test|└─── .eslintrc.js└─── tsconfig.json|└─── REAME.md
build目录构建 webpack 配置
npm install --save-dev webpack webpack-cli webpack-dev-server ts-loader html-webpack-plugin webpack-merge clean-webpack-plugin
入口配置webpack.config.js
const merge = require('webpack-merge');const basicConfig=require('./webpack.base.config');const devConfig=require('./webpack.dev.config');const prodConfig=require('./webpack.pro.config')const config=process.NODE_ENV==='development'?devConfig:prodConfig;module.exports=merge(basicConfig,config)
基础配置webpack.base.config.js
const path=require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');module.exports={entry:{app:'./src/index.ts'},resolve: {extensions: [ '.tsx', '.ts', '.js' ]},module: {rules: [{test: /\.tsx?$/,use: 'ts-loader',exclude: /node_modules/}]},plugins:[new HtmlWebpackPlugin({title:'webpack-ts-deno',template:'./src/template/index.html'})],output:{filename: '[name].bundle.js',path: path.resolve(__dirname, '../dist')}}
开发环境配置webpack.dev.config.js
const { CleanWebpackPlugin } =require('clean-webpack-plugin');module.exports={plugins:[new CleanWebpackPlugin(),]}
生产环境配置webpack.pro.config.js
module.exports={devtool: 'cheap-module-eval-source-map',}
修改package.json
脚本
"scripts": {"dev": "webpack-dev-server --mode=development --config ./build/webpack.config.js","build": "webpack --mode=production --config ./build/webpack.config.js"}
npm i -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser
@typescript-eslint/parser 为eslint 解析 ts
初始化 eslint 配置
npx eslint --init
module.exports = {"parser": "@typescript-eslint/parser","parserOptions": {"project":"./tsconfig.json","ecmaFeatures": {"jsx": true},"ecmaVersion": 2018,"sourceType": "module"},"extends":["plugin:@typescript-eslint/recommended"],"plugins": ["react","@typescript-eslint"],"rules": {}};
添加脚本 package.json
"scripts": {"lint":"eslint src --ext .js,.ts",}
npm i jest ts-jest
生成jest 配置文件
npx ts-jest config:init
添加脚本package.json
"scripts": {"test":"jest",}