当前位置:网站首页>TypeScript增量编译
TypeScript增量编译
2022-07-06 16:30:00 【用户6256742】
我们在使用Node.js开发服务端时通常会使用TypeScript来开发大型项目,但是使用ts-node进行全量编译时经常遇到编译速度慢的问题,通常是修改一行代码编译两分钟。这时我们需要使用增量编译来优化编译速度,及其他的文件在项目启动时进行全量编译,开发时修改了哪个TS文件就编译成对应的JS文件。
开发环境
1.修改tscon.json文件
{
"compilerOptions": {
"watch": true
}
}在ts配置中开启watch监听rootDir路径下的全部ts文件(文章末尾会有我的tsconfig.json)
2.编写程序
编写dev.js作为项目开发启动文件,封装Node.js自带方法来移动文件夹来复制如public文件夹、.env文件,启动tsc增量编译,监听tsc运行结果首次运行成功后开始使用shell来运行编译好的dist文件夹
整个demo我会在文章底部放gitee链接。
const fs = require('fs');
const path = require('path');
const clearDir = require('./modules/clearDir');//删除整个文件夹
const copyDir = require('./modules/copyDir');//复制文件夹
const countFile = require('./modules/countFile');//计算文件夹内文件的个数
clearDir('dist'); //清空dist
fs.mkdirSync('dist') //创建dist
copyDir('public', 'dist/public'); //复制public
fs.writeFileSync('dist/.env.development', fs.readFileSync('.env.development').toString()) //复制环境变量文件
// 开始执行tsc
const shell = require('shelljs');
const tsCount = countFile(path.join(__dirname, '../src')) //获取ts文件个数实时对比JS
const _tsc = shell.exec('tsc', {
async: true
});
// 实时对比TS和JS的个数
new Promise((resolve, reject) => {
_tsc.stdout.on('data', function (data) {
let src = path.join(__dirname, '../dist/src');
if (fs.existsSync(src)) {
let timer = setInterval(() => {
let jsCount = countFile('./dist/src')
if (jsCount == tsCount) {
clearInterval(timer)
resolve()
}
}, 50);
}
});
}).then(() => {
shell.exec('cross-env ENV=development nodemon --watch ./dist ./dist/src/index.js', {
async: true
});
})我在这里面进行了dist文件夹的清空和public文件夹的复制,以及环境变量文件的迁移
整体思路:
初始化所需的静态文件夹,开始tsc的运行,同时监听源代码中的ts文件数量和dist中的js文件数量是否一直,一直则说明tsc首次编译结束。在src文件夹全部打包后开始使用nodemon运行dist文件夹,这样就只需要运行对应的js就可以了,大大加快了热更新速度。
很多同学在使用ts开发时使用到了ts路径别名,使用了module-alias插件来解决,但是dist下package.json 需要额外修改,具体在node.js中使用路径别名可以参考这篇文章TypeScript中使用类型别名
打包
const fs = require('fs');
const shell = require('shelljs');
const path = require('path');
const clearDir = require('./modules/clearDir')
const copyDir = require('./modules/copyDir');
const countFile = require('./modules/countFile');
clearDir('dist'); //清空dist
fs.mkdirSync('dist')
const _tsc = shell.exec('tsc', {
async: true
});
copyDir('public', 'dist/public'); //复制public
fs.writeFileSync('dist/package.json', fs.readFileSync('package.json').toString())
fs.writeFileSync('dist/yarn.lock', fs.readFileSync('yarn.lock').toString())
fs.writeFileSync('dist/.env.production', fs.readFileSync('.env.production').toString())
const tsCount = countFile(path.join(__dirname, '../src')) //获取ts文件个数实时对比JS
new Promise((resolve, reject) => {
_tsc.stdout.on('data', function (data) {
let src = path.join(__dirname, '../dist/src');
if (fs.existsSync(src)) {
let timer = setInterval(() => {
let jsCount = countFile('./dist/src');
if (jsCount == tsCount) {
clearInterval(timer)
resolve()
}
}, 50);
}
});
}).then(() => {
console.log(`打包完成,SRC下共 ${tsCount} 个文件`);
shell.exit(1)
})原理和开发类似,只不过移动的文件夹不同,并且在tsc结束后可以直接退出
一个Koa+TypeScript的模板,ORM使用Sequelize,TS添加了增量编译以及类型别名,Koa也基本调试好,自动引入Router以及配置了静态文件
基本上可以Koa+TS下载即用,运行命令在Readme.md中
git clone https://gitee.com/awebcoder/koa-ts.gittsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "ES2015",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./",
"moduleResolution": "node",
//变量和函数参数未使用警告
// "noUnusedLocals": true,
// "noUnusedParameters": true,
"removeComments": true, //取消注释
"strict": true,
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
},
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": true, // 不允许隐式的 any 类型
"watch": true
},
"include": ["src"],
"exclude": ["node_modules", "**/*.d.ts"]
}边栏推荐
- 基于SSM框架实现的房屋租赁管理系统
- okcc呼叫中心的订单管理时怎么样的
- Today's sleep quality record 78 points
- 量子时代计算机怎么保证数据安全?美国公布四项备选加密算法
- Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
- PostgreSQL使用Pgpool-II实现读写分离+负载均衡
- 在Docker中分分钟拥有Oracle EMCC 13.5环境
- Rider离线使用Nuget包的方法
- Summary of three methods for MySQL to view table structure
- Why is bat still addicted to 996 when the four-day working system is being tried out in Britain?
猜你喜欢

STM32通过串口进入和唤醒停止模式

The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
![Tourism Management System Based on jsp+servlet+mysql framework [source code + database + report]](/img/41/94488f4c7627a1dfcf80f170101347.png)
Tourism Management System Based on jsp+servlet+mysql framework [source code + database + report]

DevOps可以帮助减少技术债务的十种方式

资产安全问题或制约加密行业发展 风控+合规成为平台破局关键

Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming

AI金榜题名时,MLPerf榜单的份量究竟有多重?

app通用功能测试用例

leetcode:236. 二叉树的最近公共祖先

leetcode:236. The nearest common ancestor of binary tree
随机推荐
[unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code
【系统分析师之路】第七章 复盘系统设计(面向服务开发方法)
AI金榜题名时,MLPerf榜单的份量究竟有多重?
【精品】pinia 基于插件pinia-plugin-persist的 持久化
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码
【无人机】多无人协同任务分配程序平台含Matlab代码
士大夫哈哈哈
The method of reinstalling win10 system is as simple as that
Computer reinstallation system teaching, one click fool operation, 80% of people have learned
英国都在试行4天工作制了,为什么BAT还对996上瘾?
MATLIB reads data from excel table and draws function image
Competition between public and private chains in data privacy and throughput
AVL树到底是什么?
How rider uses nuget package offline
在docker中快速使用各个版本的PostgreSQL数据库
【212】php发送post请求有哪三种方法
[system analyst's road] Chapter 7 double disk system design (service-oriented development method)
Experiment 4: installing packages from Gui
app通用功能测试用例