Skip to content

Commit

Permalink
Updates for 1.40 (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored Jan 25, 2024
1 parent aebb503 commit ef0aae8
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 30 deletions.
2 changes: 1 addition & 1 deletion runtime/manual/advanced/typescript/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ this:
"compilerOptions": {
"allowJs": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"experimentalDecorators": false,
"inlineSourceMap": true,
"isolatedModules": true,
"jsx": "react",
Expand Down
4 changes: 1 addition & 3 deletions runtime/manual/node/compatibility.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,7 @@ which modules you need by
</div>
</summary>
<p>
Missing <code>disconnect</code>, <code>message</code>,{" "}
<code>multipleResolves</code>, <code>rejectionHandled</code> and{" "}
<code>worker</code> events.
Missing <code>multipleResolves</code>, <code>worker</code> events.
</p>
<p>
<a href="https://nodejs.org/api/process.html">Node.js docs</a>
Expand Down
112 changes: 106 additions & 6 deletions runtime/manual/runtime/import_meta_api.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,115 @@
# `import.meta` API

Deno supports a number of methods on the
Deno supports a number of properties and methods on the
[`import.meta`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta)
API:

- `import.meta.url`: returns the URL of the current module.
- `import.meta.main`: returns whether the current module is the entry point to
your program.
- `import.meta.resolve`: resolve specifiers relative to the current module.
## `import.meta.url`

## `import.meta.resolve` Example
Returns the URL of the current module.

```ts
// main.ts
console.log(import.meta.url);
```

```sh
$ deno run main.ts
file:///dev/main.ts

$ deno run https:/example.com/main.ts
https://example.com/main.ts
```

## `import.meta.main`

Returns whether the current module is the entry point to your program.

```ts
// main.ts
import "./other.ts";

console.log(`Is ${import.meta.url} the main module?`, import.meta.main);

// other.ts
console.log(`Is ${import.meta.url} the main module?`, import.meta.main);
```

```sh
$ deno run main.ts
Is file:///dev/other.ts the main module? false
Is file:///dev/main.ts the main module? true
```

## `import.meta.filename`

_This property is only available for local modules (module that have
`file:///...` specifier) and returns `undefined` for remote modules._

Returns the fully resolved path to the current module. The value contains OS
specific path separators.

```ts
// main.ts
console.log(import.meta.filename);
```

On Unix:

```sh
$ deno run main.ts
/dev/main.ts

$ deno run https://example.com/main.ts
undefined
```

On Windows:

```sh
$ deno run main.ts
C:\dev\main.ts

$ deno run https://example.com/main.ts
undefined
```

## `import.meta.dirname`

_This property is only available for local modules (module that have
`file:///...` specifier) and returns `undefined` for remote modules._

Returns the fully resolved path to the directory containing the current module.
The value contains OS specific path separators.

```ts
// main.ts
console.log(import.meta.dirname);
```

On Unix:

```sh
$ deno run main.ts
/dev/

$ deno run https://example.com/main.ts
undefined
```

On Windows:

```sh
$ deno run main.ts
C:\dev\

$ deno run https://example.com/main.ts
undefined
```

## `import.meta.resolve`

Resolve specifiers relative to the current module.

```ts
const worker = new Worker(import.meta.resolve("./worker.ts"));
Expand Down
5 changes: 5 additions & 0 deletions runtime/manual/runtime/program_lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Deno supports browser compatible lifecycle events:
- [`unhandledrejection`](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event):
fired when a promise that has no rejection handler is rejected, ie. a promise
that has no `.catch()` handler or a second argument to `.then()`.
- [`rejectionhandled`](https://developer.mozilla.org/en-US/docs/Web/API/Window/rejectionhandled_event):
fired when a `.catch()` handler is added to a a promise that has already
rejected. This event is fired only if there's `unhandledrejection` listener
installed that prevents propagation of the event (which would result in the
program terminating with an error).

You can use these events to provide setup and cleanup code in your program.

Expand Down
22 changes: 11 additions & 11 deletions runtime/manual/runtime/workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ and this capability by default.

```ts
// Good
new Worker(new URL("./worker.js", import.meta.url).href, { type: "module" });
new Worker(import.meta.resolve("./worker.js"), { type: "module" });

// Bad
new Worker(new URL("./worker.js", import.meta.url).href);
new Worker(new URL("./worker.js", import.meta.url).href, { type: "classic" });
new Worker(import.meta.resolve("./worker.js"));
new Worker(import.meta.resolve("./worker.js"), { type: "classic" });
new Worker("./worker.js", { type: "module" });
```

Expand Down Expand Up @@ -55,7 +55,7 @@ For workers using local modules; `--allow-read` permission is required:
**main.ts**

```ts
new Worker(new URL("./worker.ts", import.meta.url).href, { type: "module" });
new Worker(import.meta.resolve("./worker.ts"), { type: "module" });
```

**worker.ts**
Expand Down Expand Up @@ -105,7 +105,7 @@ hello world
**main.js**

```js
const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
const worker = new Worker(import.meta.resolve("./worker.js"), {
type: "module",
});

Expand Down Expand Up @@ -158,7 +158,7 @@ the `deno.permissions` option in the worker API.
the on/off option you can pass true/false respectively.
```ts
const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
const worker = new Worker(import.meta.resolve("./worker.js"), {
type: "module",
deno: {
permissions: {
Expand Down Expand Up @@ -202,7 +202,7 @@ the `deno.permissions` option in the worker API.
```ts
// This worker will inherit its parent permissions
const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
const worker = new Worker(import.meta.resolve("./worker.js"), {
type: "module",
deno: {
permissions: "inherit",
Expand All @@ -212,7 +212,7 @@ the `deno.permissions` option in the worker API.
```ts
// This worker will inherit only the net permissions of its parent
const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
const worker = new Worker(import.meta.resolve("./worker.js"), {
type: "module",
deno: {
permissions: {
Expand All @@ -233,14 +233,14 @@ the `deno.permissions` option in the worker API.
```ts
// This worker will inherit its parent permissions
const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
const worker = new Worker(import.meta.resolve("./worker.js"), {
type: "module",
});
```
```ts
// This worker will inherit all the permissions of its parent BUT net
const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
const worker = new Worker(import.meta.resolve("./worker.js"), {
type: "module",
deno: {
permissions: {
Expand All @@ -255,7 +255,7 @@ the `deno.permissions` option in the worker API.
```ts
// This worker will not have any permissions enabled
const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
const worker = new Worker(import.meta.resolve("./worker.js"), {
type: "module",
deno: {
permissions: "none",
Expand Down
11 changes: 2 additions & 9 deletions runtime/manual/tools/unstable_flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,10 @@ Enable unstable file system APIs in the `Deno` namespace. These APIs include:
- [`Deno.funlockSync`](https://deno.land/api?unstable=&s=Deno.funlockSync)
- [`Deno.umask`](https://deno.land/api?unstable=&s=Deno.umask)

## `--unstable-http`

Enable unstable HTTP APIs in the `Deno` namespace. These APIs include:

- [`Deno.upgradeHttp`](https://deno.land/api?unstable=&s=Deno.upgradeHttp)

## `--unstable-net`

Enable unstable net APIs in the `Deno` namespace. These APIs include:

- [`Deno.ConnectTlsOptions`](https://deno.land/api?unstable=&s=Deno.ConnectTlsOptions)
- [`Deno.DatagramConn`](https://deno.land/api?unstable=&s=Deno.DatagramConn)
- Many more - for the latest list, check the "Show Unstable API" checkbox in the
[API reference](https://deno.land/api?unstable=)
Expand Down Expand Up @@ -262,5 +255,5 @@ access to these unstable features, you would run your script with:
deno run --unstable your_script.ts
```

No new features will be exposed using this method, and it will be removed in a
future release.
It is recommended that you use the granular unstable flags instead of this, the
`--unstable` flag is now depreacted and will be removed in Deno 2.
8 changes: 8 additions & 0 deletions runtime/tutorials/module_metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
will let you know if the current module is the program entry point.
- The string [import.meta.url](https://deno.land/api?s=ImportMeta#prop_url) will
give you the URL of the current module.
- The string
[import.meta.filename](https://deno.land/api?s=ImportMeta#prop_filename) will
give you the fully resolved path to the current module. _For local modules
only_.
- The string
[import.meta.dirname](https://deno.land/api?s=ImportMeta#prop_dirname) will
give you the fully resolved path to the directory containing the current
module. _For local modules only_.
- The [import.meta.resolve](https://deno.land/api?s=ImportMeta#prop_resolve)
allows you to resolve specifier relative to the current module. This function
takes into account an import map (if one was provided on startup).
Expand Down

0 comments on commit ef0aae8

Please sign in to comment.