1.新建文件夹part3 cmd
npm init -y 生成package.json文件
2.安装依赖
cnpm i -D webpack webpack-cli typescript ts-loader
3.编写webpack.config.json文件
//引入一个包
const path = require("path");
// webpack中的所有配置信息都应该写在module.exports中
module.exports = {
// 指定入口文件
entry: "./src/index.ts",
// 指定打包文件所在目录
output: {
// 指定打包文件的目录
path: path.resolve(__dirname, "dist"),
// 打包后文件的名字
filename: "bundle.js",
},
// 指定webpack打包时要使用的模块
module: {
//指定要加载的规则
rules: [
{
// test指定的是规则生效的文件
test: /\.ts$/,
// 要使用的loader
use: "ts-loader",
//要排除的文件
exclude:/node-modules/
},
],
},
};
4.编写tsconfig.json文件
{
"compilerOptions": {
"module": "ES2015",
"target": "ES2015",
"strict": true
}
}
5.修改package.json文件
{
"name": "part3",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build":"webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-loader": "^9.3.0",
"typescript": "^4.7.3",
"webpack": "^5.73.0",
"webpack-cli": "^4.9.2"
}
}