使用webpack打包ts代码

配置环境,进行打包

  • 下载工具
    npm i -D webpack webpack-cli typescript ts-loader
    webpack:打包工具
    webpack-cli:webpack的命令行工具
    typescript:ts核心包
    ts-loader:整合webpack和ts
  • 编写webpack配置文件
    新建 webpack.config.js:
const path = require("path")

// webpack中的所有的配置信息都应该写在module . exports中
module.exports = {
    // 入口文件
    entry: "./src/index.ts",

    output: {
        // 指定打包文件所在的目录
        path: path.resolve(__dirname, 'dist'),
        // 打包后的文件名
        filename:"boundle.js"
    },

    // 指定webpack打包是要使用的模块
    module: {
        // 指定加载规则
        rules: [
            {
                // test指定的是规则生效的文件(以.ts结尾的文件)
                test: /\.ts$/,
                // 要使用的loader
                use: 'ts-loader',
                // 编译要排除的文件,
                exclude:/node-modules/
            }
        ]
    }
}
  • 新建ts配置文件,tsconfig.json:
{
    "compilerOptions": {
        "module": "ES2015",
        "target": "ES6",
        "strict": false
    } 
}
  • package.json添加打包命令
    "build":"webpack"
{
  "name": "webpack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build":"webpack"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-loader": "^9.3.1",
    "typescript": "^4.7.4",
    "webpack": "^5.73.0",
    "webpack-cli": "^4.10.0"
  }
}
  • 打包:执行 npm run build
    执行完成之后就可以在dist下的boundle.js查看打包后的内容
    在这里插入图片描述

查看打包结果,生成index.html

打包结果是一个js文件,js文件在浏览器的显示依赖于html文件。

  • 下载插件:cnpm i -D html-webpack-plugin
  • webpack.config.js中引入插件并进行相关配置
const path = require("path")
// 引入html插件
const HTMLWebpackPlugin = require("html-webpack-plugin")
const { options } = require("nodemon/lib/config")

// webpack中的所有的配置信息都应该写在module . exports中
module.exports = {
    // 入口文件
    entry: "./src/index.ts",

    output: {
        // 指定打包文件所在的目录
        path: path.resolve(__dirname, 'dist'),
        // 打包后的文件名
        filename:"boundle.js"
    },

    // 指定webpack打包是要使用的模块
    module: {
        // 指定加载规则
        rules: [
            {
                // test指定的是规则生效的文件(以.ts结尾的文件)
                test: /\.ts$/,
                // 要使用的loader
                use: 'ts-loader',
                // 编译要排除的文件,
                exclude:/node-modules/
            }
        ]
    },

    // 配置webpack插件
    plugins: [
        // 自动生成相关文件并配置相关资源
        new HTMLWebpackPlugin({
            title:"可以自定义title"
        })
    ]
}
  • 运行 npm run build
    就会在dist下生成index.html,里面引入了我们打包好的js文件
<!doctype html><html><head><meta charset="utf-8"><title>可以自定义title</title><meta name="viewport" content="width=device-width,initial-scale=1"><script defer="defer" src="boundle.js"></script></head><body></body></html>

也可以根据我们创建好的模板生成index.html

EG:
src/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页模板</title>
</head>
<body>
    <div>模板</div>
    <div>src/index.html</div>
</body>
</html>

webpack.config.js

const path = require("path")
// 引入html插件
const HTMLWebpackPlugin = require("html-webpack-plugin")
const { options } = require("nodemon/lib/config")

// webpack中的所有的配置信息都应该写在module . exports中
module.exports = {
    // 入口文件
    entry: "./src/index.ts",

    output: {
        // 指定打包文件所在的目录
        path: path.resolve(__dirname, 'dist'),
        // 打包后的文件名
        filename:"boundle.js"
    },
    mode: 'development', // 设置mode

    // 指定webpack打包是要使用的模块
    module: {
        // 指定加载规则
        rules: [
            {
                // test指定的是规则生效的文件(以.ts结尾的文件)
                test: /\.ts$/,
                // 要使用的loader
                use: 'ts-loader',
                // 编译要排除的文件,
                exclude:/node-modules/
            }
        ]
    },

    // 配置webpack插件
    plugins: [
        // 自动生成相关文件并配置相关资源
        new HTMLWebpackPlugin({
           template:"./src/index.html"
        })
    ]
}

运行 npm run builld
dist/index.html

<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>网页模板</title><script defer="defer" src="boundle.js"></script></head><body><div>模板</div><div>src/index.html</div></body></html>

访问生成的index.html

访问dist/index.html

  • 下载模块: npm i -D webpack-dev-server
  • 在package.json中进行配置:
    "start": "webpack server"
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "webpack server"
  },
  • 终端输入:npm start
    这样就会启动webpack内置服务器。之后控制台会输出网址,点进去就可以进行查看了
    在这里插入图片描述

每次打包生成新的dist

为了确保每次打包后的文件按都是新的文件可以使用模块clean-webpack-plugin

  • 下载:npm i -D clean-webpack-plugin
  • webpack.config.js引入:
const path = require("path")
// 引入html插件
const HTMLWebpackPlugin = require("html-webpack-plugin")
const { options } = require("nodemon/lib/config")
const {CleanWebpackPlugin} = require("clean-webpack-plugin")

// webpack中的所有的配置信息都应该写在module . exports中
module.exports = {
    // 入口文件
    entry: "./src/index.ts",

    output: {
        // 指定打包文件所在的目录
        path: path.resolve(__dirname, 'dist'),
        // 打包后的文件名
        filename:"boundle.js"
    },
    mode: 'development', // 设置mode

    // 指定webpack打包是要使用的模块
    module: {
        // 指定加载规则
        rules: [
            {
                // test指定的是规则生效的文件(以.ts结尾的文件)
                test: /\.ts$/,
                // 要使用的loader
                use: 'ts-loader',
                // 编译要排除的文件,
                exclude:/node-modules/
            }
        ]
    },

    // 配置webpack插件
    plugins: [
        new CleanWebpackPlugin(),
        // 自动生成相关文件并配置相关资源
        new HTMLWebpackPlugin({
           template:"./src/index.html"
        })
    ]
}

webpack配置置引用模块

// 配置置引用模块
    resolve: {
        // 声明.ts,.js结尾的文件为模块
        extensions:['.ts','.js']
    }

eg:webpack.config.js

const path = require("path")
// 引入html插件
const HTMLWebpackPlugin = require("html-webpack-plugin")
const { options } = require("nodemon/lib/config")
const {CleanWebpackPlugin} = require("clean-webpack-plugin")

// webpack中的所有的配置信息都应该写在module . exports中
module.exports = {
    // 入口文件
    entry: "./src/index.ts",

    output: {
        // 指定打包文件所在的目录
        path: path.resolve(__dirname, 'dist'),
        // 打包后的文件名
        filename:"boundle.js"
    },
    mode: 'development', // 设置mode

    // 指定webpack打包是要使用的模块
    module: {
        // 指定加载规则
        rules: [
            {
                // test指定的是规则生效的文件(以.ts结尾的文件)
                test: /\.ts$/,
                // 要使用的loader
                use: 'ts-loader',
                // 编译要排除的文件,
                exclude:/node-modules/
            }
        ]
    },

    // 配置webpack插件
    plugins: [
        new CleanWebpackPlugin(),
        // 自动生成相关文件并配置相关资源
        new HTMLWebpackPlugin({
           template:"./src/index.html"
        })
    ],

    // 配置置引用模块
    resolve: {
        // 声明.ts,.js结尾的文件为模块
        extensions:['.ts','.js']
    }
}

为解决兼容性问题,需要将新版本的代码转换为旧版本兼容的代码,这就需要使用到babel

  • 下载:npm i -D @babel/core @babel/preset-env babel-loader core-js
    @babel/core :babel核心代码
    @babel/preset-env:babel的环境配置
    babel-loader:babel和webpack的结合
    core-js:模拟js的运行环境,用于处理js新增的一些内容,如Pomise
  • 修改webpack.config.js配置文件
const path = require("path")
// 引入html插件
const HTMLWebpackPlugin = require("html-webpack-plugin")
const { options } = require("nodemon/lib/config")
const {CleanWebpackPlugin} = require("clean-webpack-plugin")

// webpack中的所有的配置信息都应该写在module . exports中
module.exports = {
    // 入口文件
    entry: "./src/index.ts",

    output: {
        // 指定打包文件所在的目录
        path: path.resolve(__dirname, 'dist'),
        // 打包后的文件名
        filename: "boundle.js",
        // 高速webpack不是用箭头函数
        environment: {
            arrowFunction:false
        }
    },
    mode: 'development', // 设置mode

    // 指定webpack打包是要使用的模块
    module: {
        // 指定加载规则
        rules: [
            {
                // test指定的是规则生效的文件(以.ts结尾的文件)
                test: /\.ts$/,
                // 要使用的loader(use从后向前执行,先执行ts-loader)
                use: [
                    // 配置babel-loader
                    {
                        // 指定加载器
                        loader: "babel-loader",
                        // 设置babel
                        options: {
                            // 设置预定义环境
                            presets: [
                                 [
                                    // 指定环境的插件
                                    "@babel/preset-env",
                                    // 配置信息
                                    {
                                        // 要兼容的目标浏览器
                                        targets:{
                                            // "chrome": "88",
                                            "ie":"11"
                                        },
                                        "corejs": "3",
                                        // s使用corejs方式“usage”表示按需加载
                                        "useBuiltIns":"usage"
                                    }
                                ]
                            ]
                        }
                    },
                    'ts-loader'
                ],
                // 编译要排除的文件,
                exclude:/node-modules/
            }
        ]
    },

    // 配置webpack插件
    plugins: [
        new CleanWebpackPlugin(),
        // 自动生成相关文件并配置相关资源
        new HTMLWebpackPlugin({
           template:"./src/index.html"
        })
    ],

    // 配置置引用模块
    resolve: {
        // 声明.ts,.js结尾的文件为模块
        extensions:['.ts','.js']
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值