当前位置:网站首页>01 邂逅typescript,环境搭建
01 邂逅typescript,环境搭建
2022-07-31 15:06:00 【是云呀!】
我们编写的typescript代码最后都会编译成javascript代码来运行,所以需要搭建对应的环境
npm全局安装typescript
npm install typescript -g
查看版本
tsc --version
编写第一个ts文件
此时直接运行这个ts文件是报错的,我们需要转为js文件,通过我们刚才安装的tsc
// 在控制台输入 tsc 加你的文件
tsc .\01_hello-typescript.ts
此时你会发现你的目录下多了一个文件,这个js文件就是转化后的
这样处理ts代码太麻烦,目前有两种方案解决
方案一:搭建webpack环境
方案二:安装ts-node ,ts-node帮我们做两件事,编译,运行在node环境
这里学习阶段先用ts-node
npm install ts-node -g
// ts-node还依赖其他两个包,我们也需要全局安装
npm install tslib @types/node -g
通过ts-node运行ts文件,
webpack搭建ts环境(可跳过)
这是我的初始目录,创建main.ts util.ts 后 npm init 之后一直回车创建package.json文件
局部安装webpack webpack-cli
npm install webpack webpack-cli -D
先在package.json中配置build命令,
在根目录创建index.html(创建本地服务用的模板)用!生成模板后就OK了
之后创建webpack.config.js文件
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
mode: "development",
entry: "./src/main.ts",
output: {
path: path.resolve(__dirname, "./dist"),
filename: "bundle.js",
},
resolve: {
extensions: [".ts", ".js", ".json", ".cjs"],
},
devServer: {},
module: {
rules: [
{
test: /\.ts$/,
loader: "ts-loader",
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./index.html",
}),
],
};
以下是各种安装,在命令行
// ts-loader依赖typescript
npm install ts-loader typescript -D
// 需要tsconfig.json文件,这里我们自动生成
tsc --init
// 到这里就可以打包了,引用打包后的文件
// 调试麻烦,我们开启本地服务
npm install webpack-dev-server -D
// 这里指定模板
npm install html-webpack-plugin -D
设置serve脚本
npm run serve
就ok了
边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggmaplot函数可视化MA图(MA-plot)、font.legend参数和font.main参数设置标题和图例字体加粗
- /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 文件的作用
- Ubantu专题4:xshell、xftp连接接虚拟机以及设置xshell复制粘贴快捷键
- DeepLab Series Learning
- 模板与泛型编程值typelist实现
- 【MySQL】Mysql范式及外键作用
- 使用 PyTorch 检测眼部疾病
- 浏览器自带的拾色器
- 自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统
- Network cable RJ45 interface pins [easy to understand]
猜你喜欢
随机推荐
工程流体力学复习
R language ggplot2 visualization: use the ggboxplot function of the ggpubr package to visualize the grouped box plot, use the ggpar function to change the graphical parameters (caption, add, modify th
Matlab矩阵基本操作(定义,运算)
NPM Taobao mirror (latest version) released a new version of npm mirror at 2021-11-21 16:53:52 [easy to understand]
微服务架构选型
How to grab configuration information for DELL SC compellent storage system
SQL、HQL、JPQL 到底有什么区别
The role of /etc/profile, /etc/bashrc, ~/.bash_profile, ~/.bashrc files
sentinel与nacos持久化
c语言hello world代码(代码编程入门)
NC | 中国农大草业学院杨高文组揭示发现多因子干扰会降低土壤微生物多样性的积极效应...
Destruction order of thread_local variables
名创优品斥资6.95亿购买创始人叶国富所持办公楼股权
ASP.NET Core 产生连续 Guid
Essential Learning for Getting Started with Unity Shader - Transparency Effect
学习笔记12--路径-速度分解法之局部路径搜索
TCP详解
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(caption、添加、修改可视化图像的题注、脚注内容)
工程水文学试卷
Message queue data storage MySQL table design