summaryrefslogtreecommitdiff
path: root/doc/ESModules.md
blob: 4c3f0f47d88d1f84261f01f8e11a5d0111fd3e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# ECMAScript Modules

> _This documentation is inspired by [Node.js' documentation](https://github.com/nodejs/node/blob/HEAD/doc/api/esm.md)
> on ECMAScript modules._

ECMAScript Modules or "ES modules" are the [official ECMAScript
standard][] for importing, exporting, and reusing JavaScript code.

ES modules can export `function`, `class`, `const`, `let`, and `var`
statements using the `export` keyword.

```js
// animalSounds.js
export function bark(num) {
  log('bark');
}

export const ANIMALS = ['dog', 'cat'];
```

Other ES modules can then import those declarations using `import`
statements like the one below.

```js
// main.js
import { ANIMALS, bark } from './animalSounds.js';

// Logs 'bark'
bark();

// Logs 'dog, cat'
log(ANIMALS);
```

## Loading ES Modules

### Command Line

From the command line ES modules can be loaded with the `-m` flag:

```sh
gjs -m module.js
```

### JavaScript

ES modules cannot be loaded from strings at this time.

Besides the import expression syntax described above, Dynamic [`import()` statements][] can be used to load modules from any GJS script or module.

```js
import('./animalSounds.js').then((module) => {
    // module.default is the default export
    // named exports are accessible as properties
    // module.bark
}).catch(logError)
```

Because `import()` is asynchronous, you will need a mainloop running.

### C API

Using the C API in `gjs.h`, ES modules can be loaded from a file or
resource using `gjs_load_module_file()`. <!-- TODO -->

### Shebang

`example.js`

```js
#!/usr/bin/env -S gjs -m

import GLib from 'gi://GLib';
log(GLib);
```

```sh
chmod +x example.js
./example.js
```


## `import` Specifiers

### Terminology

The _specifier_ of an `import` statement is the string after the `from`
keyword, e.g. `'path'` in `import { sep } from 'path'`.
Specifiers are also used in `export from` statements, and as the
argument to an `import()` expression.

There are three types of specifiers:

* _Relative specifiers_ like `'./window.js'`.
  They refer to a path relative to the location of the importing file.
  _The file extension is always necessary for these._

* _Bare specifiers_ like `'some-package'`.
  In GJS bare specifiers typically refer to built-in modules like `gi`.

* _Absolute specifiers_ like `'file:///usr/share/gjs-app/file.js'`.
  They refer directly and explicitly to a full path or library.

Bare specifier resolutions import built-in modules.
All other specifier resolutions are always only resolved with the
standard relative URL resolution semantics.

### Mandatory file extensions

A file extension must be provided when using the `import` keyword to
resolve relative or absolute specifiers.
Directory files (e.g. `'./extensions/__init__.js'`) must also be fully
specified.

The recommended replacement for directory files (`__init__.js`) is:

```js
'./extensions.js'
'./extensions/a.js'
'./extensions/b.js'
```

Because file extensions are required, folders and `.js` files with the
same "name" should not conflict as they did with `imports`.

### URLs

ES modules are resolved and cached as URLs.
This means that files containing special characters such as `#` and `?`
need to be escaped.

`file:`, `resource:`, and `gi:` URL schemes are supported.
A specifier like `'https://example.com/app.js'` is not supported in GJS.

#### `file:` URLs

Modules are loaded multiple times if the `import` specifier used to
resolve them has a different query or fragment.

```js
import './foo.js?query=1'; // loads ./foo.js with query of "?query=1"
import './foo.js?query=2'; // loads ./foo.js with query of "?query=2"
```

The root directory may be referenced via `file:///`.

#### `gi:` Imports

`gi:` URLs are supported as an alternative means to load GI (GObject
Introspected) modules.

`gi:` URLs support declaring libraries' versions.
An error will be thrown when resolving imports if multiple versions of a
library are present and a version has not been specified.
The version is cached, so it only needs to be specified once.

```js
import Gtk from 'gi://Gtk?version=4.0';
import Gdk from 'gi://Gdk?version=4.0';
import GLib from 'gi://GLib';
// GLib, GObject, and Gio are required by GJS so no version is necessary.
```

It is recommended to create a "version block" at your application's
entry point.

```js
import 'gi://Gtk?version=3.0'
import 'gi://Gdk?version=3.0'
import 'gi://Hdy?version=1.0'
```

After these declarations, you can import the libraries without version
parameters.

```js
import Gtk from 'gi://Gtk';
import Gdk from 'gi://Gdk';
import Hdy from 'gi://Hdy';
```

## Built-in modules

Built-in modules provide a default export with all their exported functions and properties. Most modules provide named exports too. `cairo` does not provide named exports of its API.

Modifying the values of the default export _does not_ change the values of named exports.

```js
import system from 'system';
system.exit(1);
```

```js
import { ngettext as _ } from 'gettext';
_('Hello!');
```

## `import.meta`

* {Object}

The `import.meta` meta property is an `Object` that contains the
following properties:

### `import.meta.url`

* {string} The absolute `file:` or `resource:` URL of the module.

This is identical to Node.js and browser environments.
It will always provide the URI of the current module.

This enables useful patterns such as relative file loading:

```js
import Gio from 'gi://Gio';
const file = Gio.File.new_for_uri(import.meta.url);
const data = file.get_parent().resolve_relative_path('data.json');
const [, contents] = data.load_contents(null);
```

or if you want the path for the current file or directory

```js
import GLib from 'gi://GLib';
const [filename] = GLib.filename_from_uri(import.meta.url);
const dirname = GLib.path_get_dirname(filename);
```

## Interoperability with legacy `imports` modules

Because `imports` is a global object, it is still available in ES
modules.
It is not recommended to purposely mix import styles unless absolutely
necessary.

### `import` statements

An `import` statement can only reference an ES module.
`import` statements are permitted only in ES modules, but dynamic
[`import()`][] expressions is supported in legacy `imports` modules
for loading ES modules.

When importing legacy `imports` modules, all `var` declarations are
provided as properties on the default export.

### Differences between ES modules and legacy `imports` modules

#### No `imports` and `var` exports

You must use the [`export`][] syntax instead.

#### No meta path properties

These `imports` properties are not available in ES modules:

 * `__modulePath__`
 * `__moduleName__`
 * `__parentModule__`

`__modulePath__`, `__moduleName__` and `__parentModule__` use cases can
be replaced with [`import.meta.url`][].

[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
[`import()`]: #esm_import_expressions
[`import()` statements]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports
[`import.meta.url`]: #esm_import_meta_url
[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
[`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
[special scheme]: https://url.spec.whatwg.org/#special-scheme
[official ECMAScript standard]: https://tc39.github.io/ecma262/#sec-modules