昨天,deno1.0正式Release了,弃JS转TS两年后,当然是要尝个鲜的。我想在这个时候或是更早测试版去使用的,一定都是 TypeScript 粉了,进入正题
deno 发音 [ daɪnə ] 与 dinosaur:恐龙 前面发音相同
安装
在 windows 环境,打开 powershell ,输入以下内容那可安装 :
iwr https://deno.land/x/install/install.ps1 -useb | iex
我这边大概十分钟左右,显示出,即安装成功,安装在当前登录用户的 .deno
目录下,只有一个 deno.exe
文件, 这个文件包括了deno运行所需要的一切。
Deno was installed successfully to C:\Users\xxxxx\.deno\bin\deno.exe
Run 'deno --help' to get started
其它平台或Windows的其它安装方式参考 : https://deno.land/#installation
获取帮助
PS C:\Windows\system32> deno --help
deno 1.0.0
A secure JavaScript and TypeScript runtime
Docs: https://deno.land/manual
Modules: https://deno.land/std/ https://deno.land/x/
Bugs: https://github.com/denoland/deno/issues
To start the REPL:
deno
To execute a script:
deno run https://deno.land/std/examples/welcome.ts
To evaluate code in the shell:
deno eval "console.log(30933 + 404)"
USAGE:
deno [OPTIONS] [SUBCOMMAND]
OPTIONS:
-h, --help
Prints help information
-L, --log-level <log-level>
Set log level [possible values: debug, info]
-q, --quiet
Suppress diagnostic output
By default, subcommands print human-readable diagnostic messages to stderr.
If the flag is set, restrict these messages to errors.
-V, --version
Prints version information
SUBCOMMANDS:
bundle Bundle module and dependencies into single files
cache Cache the dependencies
doc Show documentation for a module
eval Eval script
fmt Format source files
help Prints this message or the help of the given subcommand(s)
info Show info about cache or info related to source file
install Install script as an executable
repl Read Eval Print Loop
run Run a program given a filename or url to the module
test Run tests
types Print runtime TypeScript declarations
upgrade Upgrade deno executable to given version
ENVIRONMENT VARIABLES:
DENO_DIR Set deno's base directory (defaults to $HOME/.deno)
DENO_INSTALL_ROOT Set deno install's output directory
(defaults to $HOME/.deno/bin)
NO_COLOR Set to disable color
HTTP_PROXY Proxy address for HTTP requests
(module downloads, fetch)
HTTPS_PROXY Same but for HTTPS
翻译一下:
SUBCOMMANDS:
bundle 打包 : (相当于 webpack?)
cache 缓存依赖库
doc 查看一个模块的文档
eval 执行脚本
fmt 代码的格式美化
help 查看本帮助
info 显示本地的依赖缓存
install 安装脚本为可执行文件
repl 进入 REPL 环境
run 运行脚本
test 运行测试
types 打印 TypeScript 运行时
upgrade 升级 Deno 到指定版本
运行
- 运行官网示例
deno 可以(通过下载)直接运行互联网上的代码,比如Github ,或者私人服务器,也可以是例如 pika.dev or jspm.io.的包平台
C:\Users\xxxx>deno run https://deno.land/std/examples/welcome.ts
Download https://deno.land/std/examples/welcome.ts
Warning Implicitly using master branch https://deno.land/std/examples/welcome.ts
Compile https://deno.land/std/examples/welcome.ts
Welcome to Deno 🦕
- 运行项目脚本
现在,建立 index.ts 文件 console.log('Hello Deno')
运行
PS D:\deno> deno run index.ts
Compile file:///D:/deno/index.ts
Hello Deno
也可以修改代码,引入一个库,获得更强大的能力
import { printf } from "https://deno.land/std/fmt/printf.ts"
printf("%*.*f", 9, 8, 456.0)
其它探索
- 如你所愿,你可以直接写 typescript , 不需要
ts-node
或ts-loader
、babel
等工具了。 - deno 和 nodejs 一样,默认提供了一套核心 api,功能基本可以覆盖到nodejs的各方面。 https://doc.deno.land/https/github.com/denoland/deno/releases/latest/download/lib.deno.d.ts
- deno 没有nodejs的模块概念,不会有 package.json 和 node_moudles ,所有引入只能通过 url 进行! 支持 file:// https:// 或相对路径
- deno 导出模块,不再使用 CommonJS 语法,统一使用 ESModule 语法规范,deno 内部所有异步方法都是返回 Promise,不再有 callback 之类的玩意
- 相对于 nodejs deno 还内置了一个 windows 全局变量,有 fetch,setTimeout 等方法,可以仿生浏览器环境
- 多年前布道过一端时间的 golang 非常喜欢 golang 的 fmt ,虽然被很多强迫症吐槽,现在 deno 也可以用了~
小结
由于时间问题,就没有去写浏览器应用了,很多前端包虽然没有发布在 deno.land 上,但理论上 github 甚至 npmjs.com 的链接都是可以直接用的,deno 同时支持 ts 与 js 所以基本不用担心资源问题。
但是 bundle 与 webpack 比起来,还是会有些不足,所以现在中大型项目还是得先观望观望。
后面有深入的话,继续更新,回见!