1. 初始化,生成package.json
npm init -y
2. 安装typescript
npm install typescript ts-loader tslint --save-dev
3. 初始化tsconfig.json
./node_modules/typescript/bin/tsc --init
4. 添加webpack依赖
npm install webpack webpack-cli webpack-dev-server --save-dev
6. 安装webpack插件
npm i clean-webpack-plugin -D
npm install clean-webpack-plugin -D
7. 配置webpack配置文件
const htmlWebpackPlugin = require('html-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const path = require('path')
module.exports = {
entry: "./src/index.ts",
output: {
filename: "./main.js",
path: path.resolve(process.cwd(), 'dist'),
},
resolve: {
extensions: ['.js', '.ts', '.tsx']
},
module: {
rules: [{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}]
},
devtool: "inline-source-map",
devServer: {
static: {
directory: path.join(__dirname, 'dist'),
},
compress: false,
host: 'localhost',
port: 3000,
},
mode: 'development',
plugins: [
new CleanWebpackPlugin(),
new htmlWebpackPlugin({
template: './src/template/index.html'
})
]
}
8. 配置package.json文件,设置build、start命令
{
"name": "learntype",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"scripts": {
"start": "node_modules/webpack-dev-server/bin/webpack-dev-server.js --config ./build/webpack.config.js",
"build": "node_modules/webpack/bin/webpack.js --config ./build/webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"clean-webpack-plugin": "^4.0.0",
"html-webpack-plugin": "^5.5.0",
"ts-loader": "^9.4.1",
"tslint": "^6.1.3",
"typescript": "^4.8.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
}
}
最后使用npm start即可启动项目!