2020-09-21 08:01:25 -04:00
|
|
|
# Import and export modules
|
2020-08-06 11:35:08 -04:00
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
## Concepts
|
|
|
|
|
|
|
|
- [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)
|
|
|
|
allows you to include and use modules held elsewhere, on your local file
|
|
|
|
system or remotely.
|
2020-09-27 13:49:41 -04:00
|
|
|
- Imports are URLs or file system paths.
|
2020-09-12 08:03:18 -04:00
|
|
|
- [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)
|
|
|
|
allows you to specify which parts of your module are accessible to users who
|
2020-09-27 13:49:41 -04:00
|
|
|
import your module.
|
2020-09-12 08:03:18 -04:00
|
|
|
|
|
|
|
## Overview
|
|
|
|
|
2020-08-06 11:35:08 -04:00
|
|
|
Deno by default standardizes the way modules are imported in both JavaScript and
|
2020-09-05 16:34:20 -04:00
|
|
|
TypeScript using the ECMAScript 6 `import/export` standard.
|
|
|
|
|
|
|
|
It adopts browser-like module resolution, meaning that file names must be
|
|
|
|
specified in full. You may not omit the file extension and there is no special
|
|
|
|
handling of `index.js`.
|
2020-08-06 11:35:08 -04:00
|
|
|
|
|
|
|
```js
|
2020-09-27 06:22:32 -04:00
|
|
|
import { add, multiply } from "./arithmetic.ts";
|
2020-08-06 11:35:08 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
Dependencies are also imported directly, there is no package management
|
|
|
|
overhead. Local modules are imported in exactly the same way as remote modules.
|
|
|
|
As the examples show below, the same functionality can be produced in the same
|
|
|
|
way with local or remote modules.
|
|
|
|
|
|
|
|
## Local Import
|
|
|
|
|
|
|
|
In this example the `add` and `multiply` functions are imported from a local
|
|
|
|
`arithmetic.ts` module.
|
|
|
|
|
|
|
|
**Command:** `deno run local.ts`
|
|
|
|
|
|
|
|
```ts
|
2020-09-12 08:03:18 -04:00
|
|
|
/**
|
|
|
|
* local.ts
|
|
|
|
*/
|
2020-08-06 11:35:08 -04:00
|
|
|
import { add, multiply } from "./arithmetic.ts";
|
|
|
|
|
|
|
|
function totalCost(outbound: number, inbound: number, tax: number): number {
|
|
|
|
return multiply(add(outbound, inbound), tax);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(totalCost(19, 31, 1.2));
|
|
|
|
console.log(totalCost(45, 27, 1.15));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output
|
|
|
|
*
|
|
|
|
* 60
|
|
|
|
* 82.8
|
|
|
|
*/
|
|
|
|
```
|
|
|
|
|
|
|
|
## Remote Import
|
|
|
|
|
|
|
|
In the local import example above an `add` and `multiply` method are imported
|
|
|
|
from a locally stored arithmetic module. The same functionality can be created
|
|
|
|
by importing `add` and `multiply` methods from a remote module too.
|
|
|
|
|
|
|
|
In this case the Ramda module is referenced, including the version number. Also
|
2020-12-10 14:45:45 -05:00
|
|
|
note a JavaScript module is imported directly into a TypeScript module, Deno has
|
2020-08-06 11:35:08 -04:00
|
|
|
no problem handling this.
|
|
|
|
|
|
|
|
**Command:** `deno run ./remote.ts`
|
|
|
|
|
|
|
|
```ts
|
2020-09-12 08:03:18 -04:00
|
|
|
/**
|
|
|
|
* remote.ts
|
|
|
|
*/
|
2020-08-06 11:35:08 -04:00
|
|
|
import {
|
|
|
|
add,
|
|
|
|
multiply,
|
|
|
|
} from "https://x.nest.land/ramda@0.27.0/source/index.js";
|
|
|
|
|
|
|
|
function totalCost(outbound: number, inbound: number, tax: number): number {
|
|
|
|
return multiply(add(outbound, inbound), tax);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(totalCost(19, 31, 1.2));
|
|
|
|
console.log(totalCost(45, 27, 1.15));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output
|
|
|
|
*
|
|
|
|
* 60
|
|
|
|
* 82.8
|
|
|
|
*/
|
|
|
|
```
|
2020-09-12 08:03:18 -04:00
|
|
|
|
|
|
|
## Export
|
|
|
|
|
|
|
|
In the local import example above the `add` and `multiply` functions are
|
|
|
|
imported from a locally stored arithmetic module. To make this possible the
|
|
|
|
functions stored in the arithmetic module must be exported.
|
|
|
|
|
|
|
|
To do this just add the keyword `export` to the beginning of the function
|
|
|
|
signature as is shown below.
|
|
|
|
|
|
|
|
```ts
|
|
|
|
/**
|
|
|
|
* arithmetic.ts
|
|
|
|
*/
|
|
|
|
export function add(a: number, b: number): number {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function multiply(a: number, b: number): number {
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
All functions, classes, constants and variables which need to be accessible
|
2021-03-19 16:26:49 -04:00
|
|
|
inside external modules must be exported. Either by prepending them with the
|
2020-09-12 08:03:18 -04:00
|
|
|
`export` keyword or including them in an export statement at the bottom of the
|
|
|
|
file.
|