tsc ./src/es6/a.ts -t es3
tsc ./src/es6/a.ts -t es5
tsc ./src/es6/a.ts -t es6
npm i ts-node -g
将nodejs ts 编译成js
ts-node ./src/node/c.node.ts
npm i -g express-generator express应用生成器
express ts-express 生成应用文件
npm install == npm i
npm i -D typescript 安装typescript
tsc --init 生成配置文件
将express 里js 文件改成ts文件
将www 改成server.ts
安装声明文件
npm i @types/node @types/express -D
安装声明文件
npm i -D @types/http-errors @types/cookie-parser @types/morgan @types/debug
修改 :"outDir": "./dist", /* Redirect output structure to the directory. */
添加一个构建脚本
"scripts": {
"start": "node ./bin/www",
"build-ts": "tsc"
},
运行脚本
npm run build-ts
资源文件处理
创建一个:copyStatic.ts
npm i shelljs @types/shelljs -D
新增拷贝脚本
"scripts": {
"start": "node ./bin/www",
"build-ts": "tsc",
"copy-static": "ts-node copyStatic.ts"
},
拷贝代码
import * as shelljs from "shelljs";
shelljs.cp("-R",'public','dist');
shelljs.cp("-R",' views','dist')
安装ts-node工具
npm i ts-node -D
运行拷贝脚本
npm run copy-static
合并命令
"scripts": {
"start": "node ./bin/www",
"build-ts": "tsc",
"copy-static": "ts-node copyStatic.ts",
"build": "npm run build-ts && npm run copy-static"
},
npm run build
tsconfig.json
新增排除文件
"exclude":[
"copyStatic.ts"
],
修改package.json "start": "node ./bin/www",
为 "start": "node ./dist/bin/server.js",
启动服务 npm start
增加命令: "watch": "nodemon ./dist/bin/server.js",
安装命令插件 npm i nodemon -D
运行watch命令
npm run watch
重新构建查看 页面变动
npm run build