1. 使用 npm init -y 初始化项目
2. 安装webpack相关依赖包
cnpm i webpack webpack-cli -D ;
3. webpack 最基础配置
3.1 在项目根目录下创建 webpack 的配置文件 webpack.config.js , 进行下列最基本配置;
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "./dist"),
},
};
3.2 编辑 package.json 文件,添加打包命令;
- 在
scripts 中添加 "build": "webpack" , 最终结果如下
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
}
npx webpack 或直接执行 npx 进行打包编译;
3.3 npm run build 运行打包工具
- 要根据
webpack.config.js 配置文件中入口文件的配置,创建有符合路径的入口文件。
4. 配置 typescript 打包环境
4.1 cnpm i typescript ts-loader -D
4.2 在 webpack.config.js 中配置 typescript 的编译配置
const path = require("path");
module.exports = {
entry: "./src/index.ts",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "./dist"),
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
}
]
},
}
4.3 tsconfig.json ts 的编译配置文件
- 在根目录下创建 ts 的编译配置文件
tsconfig.json , 直接创建,空文件也可以生效; - 或使用
tsc --init 生成配置文件, 此配置文件含有一些默认配置,需要按需修改;
运行 npm run build 即可进行ts编译;
5. babel 低版本浏览器兼容
5.1 cnpm i @babel/core @babel/preset-env babel-loader core-js -D ;
5.2 修改配置文件 webpack.config.js ;
module.exports = {
output: {
environment: {
arrowFunction: false,
},
},
module: {
rules: [
{
test: /\.ts$/,
use: [
{
loader: 'babel-loader',
options:{
presets: [
[
"@babel/preset-env",
{
targets: {
"chrome": "79",
"ie": "11",
},
"corejs": "3",
"useBuiltIns": "usage",
}
]
],
},
},
'ts-loader',
],
exclude: /node_modules/,
}
]
},
}
6. 编译时自动删除已有的历史 dist 文件
6.1 cnpm i clean-webpack-plugin -D : 安装第三方插件;
6.2 修改配置文件
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
module.exports = {
plugins: [
new CleanWebpackPlugin(),
],
}
7. 指定html模板
cnpm i html-webpack-plugin -D : 安装依赖;- 修改配置文件
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./index.html",
}),
],
}
8. 热编译 & mode编译模式
动态监听ts文件改变,自动编译;
cnpm i webpack-dev-server -D - 配置调试命令
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"dev": "webpack serve --open --mode=development",
"prod": "webpack serve --open --mode=production"
}
console.log(process.env.NODE_ENV);
最终状态
安装所有依赖
cnpm i -D webpack webpack-cli typescript ts-loader @babel/core @babel/preset-env babel-loader core-js clean-webpack-plugin html-webpack-plugin webpack-dev-server
webpack.config.js 配置文件最终状态
const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index.ts",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "./dist"),
environment: {
arrowFunction: false,
},
},
module: {
rules: [
{
test: /\.ts$/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
[
"@babel/preset-env",
{
targets: {
"chrome": "79",
"ie": "11",
},
"corejs": "3",
"useBuiltIns": "usage",
}
]
],
},
},
'ts-loader',
],
exclude: /node_modules/,
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./index.html",
}),
],
}
package.json 文件最终状态
{
"name": "typescript",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode=production",
"dev": "webpack serve --open --mode=development",
"prod": "webpack serve --open --mode=production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.19.6",
"@babel/preset-env": "^7.19.4",
"babel-loader": "^8.2.5",
"clean-webpack-plugin": "^4.0.0",
"core-js": "^3.25.5",
"html-webpack-plugin": "^5.5.0",
"ts-loader": "^9.4.1",
"typescript": "^4.8.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
}
}
tsconfig.json 文件最终状态
- 此文件使用
tsc --init 自动生成,不予展示;
目录结构
至此:.ts 文件的 webpack 打包功能就实现了
|