打包步骤
- 初始化项目:
npm init -y
- 用来创建
package.json
文件 - 下载安装依赖包:
npm i -D webpack webpack-cli webpack-dev-server typescript ts-loader clean-webpack-plugin
1. webpack:构建工具
2. webpack-cli:webpack的命令行工具
3. webpack-dev-server:webpack的开发服务器
4. typescript:ts编译器
5. ts-loader:打包编译typescript的
6. clean-webpack-plugin:用来清除文件
文件配置
{
"name": "part3",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"start": "webpack serve --open chrome.exe",
},
"keywords": [],
"author": "",
"license": "ISC",
"deDependencies":{
"ts-loader": "^8.0.11",
"typescript": "^4.1.2",
"webpack": "^5.6.8",
"webpack-cli": "^4.2.0",
}
}
const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
module.exports = {
entry: "./src/index.ts"
output: {
path: path.resolve(__dirname,'dist'),
filename: "bundle.js",
enviroment:{
arrowFunction: false
}
},
module:{
rules: [
test: /\.ts$/,
use: [
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
{
targets:{
"chrome": "88",
"ie": "11",
},
"corejs": "3",
"useBuilIns": "usage",
}
]
},
'ts-loader',
],
exclude: /node-modules/
]
},
plugins: [
new CleanWebpackPlugin(),
new HTMLWebpackPlugin(options:{
template: "./src/index.html"
}),
],
resolve: {
extensions: ['.ts', '.js']
}
}
{
"compilerOptions":{
"module": "ES2015",
"target": "ES2015",
"strict": true
}
}