本文最后更新于:2024年4月25日 上午
配置TS环境
🥝配置
1 2 3 4 5 6 7 8 9 10 11 12
| { ... "main": "./src/index.ts" ... "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "cross-env NODE_ENV=development webpack-dev-server --config ./build/webpack.config.js" } ... "license": "MIT" }
|
🥝文件目录
1 2 3 4 5 6 7 8 9 10 11 12
| └─TS-demo ├─build ├─src │ ├─api │ ├─assets │ │ ├─font │ │ └─img │ ├─config │ ├─tools │ └─utils └─typings
|
🥝依赖安装
1
| npm install typescript tslint -g
|
🥝初始化TS配置
🥝初始化tslint
🥝安装webpack开发依赖
1
| npm install webpack webpack-cli webpack-dev-server -D
|
🥝添加webpack配置
在build
目录下添加配置文件webpack.config.js
安装TS打包加载器npm install ts-loader -D
安装插件npm install clean-webpack-plugin html-webpack-plugin -D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| const HtmlWebpackPlugin = require('html-webpack-plugin') const {CleanWebpackPlugin} = require('clean-webpack-plugin')
module.exports = { entry: "./src/index.ts", output: { filename: "main.js" }, resolve: { extensions: ['.ts', '.tsx', '.js'] }, module: { rule: [{ test: /\.tsx?$/, use: 'ts-loader', exclude: /node_modules/ }] }, devtool: process.env.NODE_ENV === 'production' ? false : 'inline-source-map', devServer: { compress: false, host: `localhost`, port: 8080 }, plugins: [ new CleanWebpackPlugin({ cleanOnceBeforeBuildPatterns: ['./dist'] }), new HtmlWebpackPlugin({ template: './src/template/index.html' }) ] }
|
🥝项目安装TS依赖