A project to get the Deno's Wisdom | 2020 & Up
- Install TypeScript Compiler globally with npm:
For Windows, add the path to the environment variables:
$ npm i -g typescript
C:\Users\{user}\AppData\Roaming\npm
- Version, compilation and run:
// Check version and options $ tsc // Compile a JS file $ tsc intro.ts // Run the compiled file $ node intro.js
- Initialize the project as TS project:
// create tsconfig file $ tsc --init // keep it running $ tsc intro.ts --watch // new tab to keep node also running $ nodemon intro.js
- Documentation: https://www.typescriptlang.org/docs
-
Installation
- https://deno.land/
- (Admin)
$ choco install deno
- Version:
$ deno -V
- (Admin) Upgrade to latest version:
$ deno upgrade
- Commands:
$ deno --help
- NOTE: Deno has a built-in TypeScript compiler:
$ deno run intro.ts
-
VSCode Extension:
- https://marketplace.visualstudio.com/items?itemName=denoland.vscode-deno
- Create a file .vscode/settings.json in your project folder:
{ "deno.enable": true }
-
Deno Process
-
It acts like a sandbox with some memory where you can run a program. This program is Rusty_v8 in Deno. It allows Deno to communicate with the Rust code.
-
Rust is a multi-paradigm programming language focused on performance and safety, especially safe concurrency. Rust is syntactically similar to C++, and provides memory safety without using garbage collection. https://github.com/denoland/deno/blob/master/core/core.js
-
Rust uses the Tokio library for the Thread Pool. Tokio is a runtime for writing reliable, asynchronous, and slim applications with the Rust programming language.
-
Similarities with NodeJS System:
- NodeJS Bindings ~ Rust
- Libuv ~ Tokio
-
NOTE: Rust is called every time you use the Runtime API (https://doc.deno.land/builtin/stable): Deno. & window. As difference with NodeJS, Deno has browser compatibility, so you don't need libraries like node-fetch because it's not included in global for NodeJS.
-
-
Deno Metrics
- Receive metrics from the privileged side of Deno. This is primarily used in the development of Deno. 'Ops', also called 'bindings', are the go-between between Deno JavaScript and Deno Rust.
console.table(Deno.metrics());
- Receive metrics from the privileged side of Deno. This is primarily used in the development of Deno. 'Ops', also called 'bindings', are the go-between between Deno JavaScript and Deno Rust.
-
Main game changers:
- First class TypeScript
- ES Modules
import "https://deno.land/[email protected]/examples/chat/server.ts"
- Security First
$ deno run --allow-net ./deno/welcome-example.ts
- Decentralized modules
- We don't have to use npm (Microsoft)
- We can use many places to collect the packages by just a URL with the script. e.g.: https://deno.land/x
- Standard Library -> https://deno.land/[email protected]
- Built In Tooling (testing, formatting, file watchers,...)
- Browser Compatible API (window, fetch,...)
- Single Executable (deno.exe)
- Async returns Promises (Promises in Deno translated to Futures in Rusty and vice versa)
- Opinionated Modules (https://deno.land/manual/contributing/style_guide)
-
Permissions:
-
You need to allow some permissions depending on the access level. For example:
console.log(`Hello ${Deno.env.get('USERNAME')}`);
$ deno run --allow-env ./deno/permissions.ts
$ deno run --allow-all ./deno/permissions.ts
$ deno run -A ./deno/permissions.ts
-
You can also create a script and install it globally (it takes the name of the parent path):
$ deno install --allow-env main.ts
-
Drake is a Make-like task runner (build automation tool that automatically builds executable programs and libraries from source code by reading files called Makefiles which specify how to derive the target program) for Deno inspired by Make, Rake and Jake. https://deno.land/x/[email protected]
import { desc, run, task, sh, } from 'https://deno.land/x/[email protected]/mod.ts'; desc('Minimal Drake task'); task('hello', [], async function () { console.log('Hello from Drake!'); await sh('deno run --allow-env main.ts'); }); run();
$ deno run -A Drakefile.ts hello
-
-
ES Modules: Any JavaScript or TypeScript file. In Deno, the file extension must be provided for imports (.js, .ts).
import {example} from "./intro.js"
-
To get the info of a Module:
$ deno info ./03-deno-modules/example-import.ts
local: C:\Users\Manu\Documents\Learning\Deno\deno-1.3.3\03-deno-modules\example-import.ts type: TypeScript compiled: C:\Users\Manu\AppData\Local\deno\gen\file\C\Users\Manu\Documents\Learning\Deno\deno-1.3.3\03-deno-modules\example-import.ts.js deps: 2 unique (total 371B) file:///C:/Users/Manu/Documents/Learning/Deno/deno-1.3.3/03-deno-modules/example-import.ts (137B) ├── file:///C:/Users/Manu/Documents/Learning/Deno/deno-1.3.3/03-deno-modules/example-export.ts (122B) └── https://deno.land/[email protected]/examples/welcome.ts (112B)
-
Standard Library contains all the modules (without external dependencies) maintained by the Deno team. For example,
$ deno info https://deno.land/[email protected]/examples/welcome.ts
local: C:\Users\Manu\AppData\Local\deno\deps\https\deno.land\18b31e735f535d082cbca4e4425bd22c7ebdbaf8899707498079a6a624527e3e type: TypeScript compiled: C:\Users\Manu\AppData\Local\deno\gen\https\deno.land\18b31e735f535d082cbca4e4425bd22c7ebdbaf8899707498079a6a624527e3e.js deps: 0 unique (total 112B) https://deno.land/[email protected]/examples/welcome.ts (112B)
-
Third Party Modules contains all the modules created by the Deno Community.
-
Caching is used to storage data what speeds up the processes. Deno caches remote imports in a special directory specified by the DENO_DIR environment variable (e.g. Windows: C:\Users\Manu\AppData\Local\deno). The next time you run the program, no downloads will be made. If the program hasn't changed, it won't be recompiled either.
- To force/reload the module (downloads & recompilation):
$ deno run --reload ./03-deno-modules/example-import.ts
- To change the DENO_DIR path (Windowds):
$ export DENO_DIR=./deno_dir
- More info: https://deno.land/manual/linking_to_external_code
- To force/reload the module (downloads & recompilation):
-
Pika or Skypack is a CDN that allow you to use any npm package anywhere (NPM for Deno): https://www.skypack.dev/
-
A best practice for the dependencies is to allocate all of them in a file deps.ts (similar what package.json does in NodeJS). This way, we can also manage the versioning.
-
By using a lock file (with the --lock command line flag), you can ensure that the code pulled from a URL is the same as it was during initial development.
$ deno cache --lock=./03-deno-modules/src/lock.json --lock-write ./03-deno-modules/src/deps.ts
- More info: https://deno.land/manual/linking_to_external_code/integrity_checking
-
Deno provides some built in tooling that is useful when working with JavaScript and TypeScript: https://deno.land/manual/tools
- Example Bundle: deno bundle ./03-deno-modules/example-import.ts ./03-deno-modules/example.bundle.ts
- This will output a single JavaScript file, which includes all dependencies of the specified input (~ Webpack).
- Example Bundle: deno bundle ./03-deno-modules/example-import.ts ./03-deno-modules/example.bundle.ts
Deno includes the following testing features:
- The test runner in the CLI:
$ deno test
- Run a specific test file:
$ deno test src/models/planets.test.ts
- Help command:
$ deno test --help
- Filter by the test name:
$ deno test --filter short
- Run a specific test file:
- Assertions in the Standard Library: https://deno.land/[email protected]/testing
- Built-in test fixtures with
Deno.test()
: https://doc.deno.land/builtin/stable#Deno.test