1、使用命令:npm init -y 创建 package.json ,即初始化一个工程
2、使用命令:tsc --init 初始化一个 tsconfig.json
3、使用命令:npm install parcel@next -D 安装 Parcel
4、在项目中创建一个src目录,并新建 index.html and index.ts
index.html 中引入 index.ts
![db6300d5f4d309637a5cceb64d00e871.png](https://img-blog.csdnimg.cn/img_convert/db6300d5f4d309637a5cceb64d00e871.png)
index.ts
![f3c2a65a4b04991b4a8722ae60473274.png](https://img-blog.csdnimg.cn/img_convert/f3c2a65a4b04991b4a8722ae60473274.png)
通常浏览器是不能直接运行ts代码的,直接访问index.html是会报错的。这时候,我们使用parcel对ts代码进行打包编译,才能够是浏览器访问不报错。
5、在 tsconfig.json 中配置根路径rootDir和打包输出路径outDir
![a7999d22fd41c38f9a9a33c65a5bbee5.png](https://img-blog.csdnimg.cn/img_convert/a7999d22fd41c38f9a9a33c65a5bbee5.png)
6、在 package.json 中配置打包运行命令 parcel ./src/index.html
![85e740fc224127dedf2ca420490e4aaf.png](https://img-blog.csdnimg.cn/img_convert/85e740fc224127dedf2ca420490e4aaf.png)
7、运行命令:npm run test 进行编译
![3e75ac60557a77e47bfd47880d6bef15.png](https://img-blog.csdnimg.cn/img_convert/3e75ac60557a77e47bfd47880d6bef15.png)
这时候parcel会启动一个服务器server,我们直接访问http://localhost:1234/ 打开控制台就能看到ts代码打印的字符
parcel github 文档:https://github.com/parcel-bundler/parcel