ts+webpack5开发环境搭建

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即可启动项目!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值