5.4 Module Specifier
The initial task for executing a program involves creating a module specifier designated for the main module. In Deno, the main module serves as the initial piece of code, file, or program that is provided as input. This core module is also referred to as the root module or the main module.
Overview
The concept of a "Module Specifier" permeates throughout Deno's codebase. Essentially, the role of a Module Specifier in Deno is to transform an input path into a URL format. This format can take on various forms, such as "file," "http," or "https" URLs. This URL format is crucial because it helps Deno accurately locate and access the required modules, whether they are local files or resources from the web.
The code to resolve the main module is as follows:
The main interface is called "resolve_url_or_path," and it's also used by various other subcommands. This interface combines two distinct functionalities: resolving URLs and resolving file paths. These functionalities are designed to take the input, which could be either a URL or a file path, and convert it into a format that adheres to a Uniform Resource Identifier (URI) scheme. This applies to different types of files, whether they are stored locally or accessed remotely. As a result of using this interface, you obtain an object known as the "ModuleSpecifier," which encapsulates the resolved information about the input.
Functionality
As we discussed previously, the primary API called resolve_url_or_path carries out two main functions:
resolve_url
resolve_path
Just as the names imply, the initial function is used for resolving a URL, whereas the latter one is employed to resolve a local file path. This means that the first function helps in figuring out the details of a web address, while the second function assists in determining the specifics of a file's location on your device.
resolve_url_or_path
Here is the source of the function resolve_url_or_path:
The code is quite straightforward.
If input already has a URI scheme
resolve as URL
Otherwise,
resolve as a path (for local files)
Let's take an example to understand this better.
The source code for the resolve_url
function is also straightforward. It's not overly complex and can be easily understood.
Since the input is already in the form of a URL, we proceed to parse the URL mainly to validate its correctness. Now, let's look into the source of the resolve_path function, which is quite straightforward:
In resolving the path, it takes three steps:
Append current directory path to input path
Example:
/Users/mayankc/Work/source/denoExamples/
+helloLog.ts
The path is normalized
Example:
/Users/mayankc/Work/source/denoExamples/
+../../ABC.ts
gets normalized to/Users/mayankc/Work/ABC.ts
Build a file URI scheme from the path
Example:
/Users/mayankc/Work/source/denoExamples/helloLog.ts
gets converted tofile:///Users/mayankc/Work/source/denoExamples/helloLog.ts
resolve_import
Up until now, we have examined how Deno handles the determination of the main module. This module refers to the primary file specified when using the 'deno run' command. But what about imports? It's important to note that imports also undergo a comparable resolution process. While we're discussing this subject, let's look into the concept of import resolution. It's worth mentioning that we will explore imports more extensively in Chapter 6 of this book. This will provide us with a deeper understanding of how Deno manages and resolves imported modules.
Last updated