6.2 Hello world program v2

TS and JS code

We'll continue to use Typescript for our examples. The program is the same, with a some additions:
  • There are two imports with one import has another inside it
  • There is a synchronous op
  • There is an asynchronous op
  • Also, there are some console logs which are also synchronous ops
The file name is helloV2.ts.
The location of the source file is:
/Users/mayankc/Work/source/denoExamples/helloV2.ts
Here is the code that we'll use to understand some new concepts:
import { nanoid } from "npm:nanoid";
import { getMachineId } from "https://deno.land/x/machine_id/mod.ts";
const id = nanoid();
const machineId = await getMachineId();
const homeDir = Deno.env.get("HOME");
function printNumber(input: number) {
console.log(input);
}
function printString(input: string) {
console.log(input);
}
printNumber(1);
printString("One");
console.log("Nanoid=", id, ", MachineId=", machineId, ", homeDir=", homeDir);
This remains a straightforward program consisting of a single TypeScript file. At the program's outset, two imports are employed to access ES modules: 'nanoid' and 'machine_id'. The 'nanoid' module is sourced from the NPM registry, while the 'machine_id' module is acquired from Deno's registry.
The program can be partitioned into three distinct segments:
  1. 1.
    Import Section: The initial portion encompasses the two imports, serving the purpose of illustrating the internal mechanics of the import process.
  2. 2.
    Synchronous and Asynchronous Operations: This phase involves invocations of both synchronous and asynchronous operations. The operations involving 'nanoid' and 'env.get' are categorized as synchronous, whereas the 'getMachineId' operation is asynchronous in nature.
  3. 3.
    TypeScript Code: The subsequent part entails the inclusion of code extracted from 'helloLog.ts', showcasing the conversion process from TypeScript to JavaScript. This step is taken to offer a demonstration of how TypeScript code translates into its JavaScript equivalent.
Following the conversion to JavaScript, the program's structure transforms as illustrated below:
import { nanoid } from "npm:nanoid";
import { getMachineId } from "https://deno.land/x/machine_id/mod.ts";
const id = nanoid();
const machineId = await getMachineId();
const homeDir = Deno.env.get("HOME");
function printNumber(input) {
console.log(input);
}
function printString(input) {
console.log(input);
}
printNumber(1);
printString("One");
console.log("Nanoid=", id, ", MachineId=", machineId, ", homeDir=", homeDir);
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZpbGU6Ly8vVXNlcnMvbWF5YW5r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ZO0FBQ1osUUFBUSxHQUFHLENBQUMsV0FBVyxJQUFJLGdCQUFnQixXQUFXLGNBQWMifQ==
This section discusses the similarities and slight differences in the current version of Deno compared to the previous one. The primary changes revolve around imports and operations (ops). It's worth noting that some ops, like console.log, were present in the previous version as well.
--
Let's take a moment to review. The primary worker in Deno operates through two main phases: module loading and module evaluation. The process of loading a module can vary due to the presence of imports.
Now, let's delve into a new topic – creating the module graph for a program that incorporates imports. This involves visually representing how different modules connect and interact within a program.
To build a module graph, Deno analyzes the code to identify all the imported modules and their dependencies. It then arranges these modules in a structured manner, illustrating their connections.