当前位置:网站首页>Typescript incremental compilation
Typescript incremental compilation
2022-07-06 23:56:00 【User 6256742】
We are using Node.js When developing the server, we usually use TypeScript To develop large projects , But use ts-node When doing full compilation, we often encounter the problem of slow compilation speed , It usually takes two minutes to modify a line of code and compile . At this time, we need to use incremental compilation to optimize the compilation speed , And other files are fully compiled at the start of the project , Which has been modified during development TS The file is compiled into the corresponding JS file .
development environment
1. modify tscon.json file
{
"compilerOptions": {
"watch": true
}
}stay ts Open in configuration watch monitor rootDir All under the path ts file ( There will be me at the end of the article tsconfig.json)
2. Programming
To write dev.js As the project development startup document , encapsulation Node.js Bring your own method to move the folder to copy, such as public Folder 、.env file , start-up tsc Incremental compilation , monitor tsc The operation results will be used after the first successful operation shell To run the compiled dist Folder
Whole demo I will put at the bottom of the article gitee link .
const fs = require('fs');
const path = require('path');
const clearDir = require('./modules/clearDir');// Delete the entire folder
const copyDir = require('./modules/copyDir');// Copy folder
const countFile = require('./modules/countFile');// Calculate the number of files in the folder
clearDir('dist'); // Empty dist
fs.mkdirSync('dist') // establish dist
copyDir('public', 'dist/public'); // Copy public
fs.writeFileSync('dist/.env.development', fs.readFileSync('.env.development').toString()) // Copy the environment variable file
// Start execution tsc
const shell = require('shelljs');
const tsCount = countFile(path.join(__dirname, '../src')) // obtain ts Real time comparison of the number of files JS
const _tsc = shell.exec('tsc', {
async: true
});
// Real time comparison TS and JS The number of
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
});
})I did it here dist Folder emptying and public Copy of folder , And the migration of environment variable files
The whole idea :
Initialize the required static folder , Start tsc Operation of , At the same time, listen for ts Number of documents and dist Medium js Whether the number of documents has been , Always explain tsc The first compilation is over . stay src After all folders are packaged, start using nodemon function dist Folder , In this way, you only need to run the corresponding js That's all right. , It greatly accelerates the hot update speed .
Many students are using it ts Developed using ts path alias , Used module-alias Plug in to solve , however dist Next package.json Additional modifications are required , Specific in node.js You can refer to this article for the use of path aliases in TypeScript Use type aliases in
pack
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'); // Empty dist
fs.mkdirSync('dist')
const _tsc = shell.exec('tsc', {
async: true
});
copyDir('public', 'dist/public'); // Copy 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')) // obtain ts Real time comparison of the number of files 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(` Packing is done ,SRC Lower total ${tsCount} File `);
shell.exit(1)
})The principle is similar to development , It's just that the folders moved are different , And in tsc After that, you can exit directly
One Koa+TypeScript The template of ,ORM Use Sequelize,TS Added incremental compilation and type aliases ,Koa It is also basically debugged , Auto import Router And configuring static files
Basically Koa+TS Download is used , The run command is in Readme.md in
git clone https://gitee.com/awebcoder/koa-ts.gittsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "ES2015",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./",
"moduleResolution": "node",
// Variables and function parameters do not use warnings
// "noUnusedLocals": true,
// "noUnusedParameters": true,
"removeComments": true, // uncomment
"strict": true,
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
},
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": true, // Implicit is not allowed any type
"watch": true
},
"include": ["src"],
"exclude": ["node_modules", "**/*.d.ts"]
}边栏推荐
- STM32通过串口进入和唤醒停止模式
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
- Scholar doctor hahaha
- Please help xampp to do sqlilab is a black
- Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work
- 《LaTex》LaTex数学公式简介「建议收藏」
- Computer reinstallation system teaching, one click fool operation, 80% of people have learned
- MVC and MVVM
- 零代码高回报,如何用40套模板,能满足工作中95%的报表需求
- DAY ONE
猜你喜欢

谁说新消费品牌大溃败?背后有人赢麻了

Yaduo Sangu IPO

设计一个抢红包系统

内网穿透zerotier 外网(手机、电脑等)访问内网设备(树莓派、NAS、电脑等)

自动化测试工具Katalon(Web)测试操作说明

Rider离线使用Nuget包的方法

【无人机】多无人协同任务分配程序平台含Matlab代码

The programmer refused the offer because of low salary, HR became angry and netizens exploded

Building lease management system based on SSM framework

【2022全网最细】接口测试一般怎么测?接口测试的流程和步骤
随机推荐
DAY ONE
Basic chart interpretation of "Oriental selection" hot out of circle data
Newsletter L Huobi ventures is in-depth contact with genesis public chain
Experiment 4: installing packages from Gui
【自动化测试框架】关于unittest你需要知道的事
Résumé des connaissances de gradle
专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
【CVPR 2022】半监督目标检测:Dense Learning based Semi-Supervised Object Detection
Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming
MySQL master-slave multi-source replication (3 master and 1 slave) setup and synchronization test
MySQL主从之多源复制(3主1从)搭建及同步测试
SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
自动化测试工具Katalon(Web)测试操作说明
How to find out if the U disk file of the computer reinstallation system is hidden
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码
Scholar doctor hahaha
After 3 years of testing bytecan software, I was ruthlessly dismissed in February, trying to wake up my brother who was paddling
app通用功能測試用例
Design a red envelope grabbing system