Skip to content

Commit

Permalink
fix: broken links in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
saikatmitra91 committed May 15, 2024
1 parent e2afc60 commit 3f0b0d0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
11 changes: 5 additions & 6 deletions docs/node/cjs.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Only CommonJS mode

Node.js runs files in CommonJS mode when the file extension is `.cjs` (or `.cts` if TypeScript), or `.js` when [`package.json#type`](https://nodejs.org/api/packages.html#type) is undefined or set to `commonjs`.
Expand All @@ -8,7 +7,7 @@ This section is only for adding tsx in CommonJS mode (doesn't affect `.mjs` or `
::: warning Not for 3rd-party packages
This enhances the entire runtime so it may not be suitable for loading TypeScript files from a 3rd-party package as it may lead to unexpected behavior in user code.

For importing TypeScript files in CommonJS mode without affecting the environment, see [`tsx.require()`](http://localhost:5173/node/tsx-require).
For importing TypeScript files in CommonJS mode without affecting the environment, see [`tsx.require()`](/node/tsx-require).
:::

## Command-line API
Expand All @@ -30,19 +29,19 @@ NODE_OPTIONS='--require tsx/cjs' npx some-binary
Load `tsx/cjs` at the top of your entry-file:

```js
require('tsx/cjs')
require("tsx/cjs");

Check failure on line 32 in docs/node/cjs.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Strings must use singlequote

Check failure on line 32 in docs/node/cjs.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Extra semicolon
```

### Registration & Unregistration

To manually register and unregister the tsx enhancement:

```js
const tsx = require('tsx/cjs/api')
const tsx = require("tsx/cjs/api");

Check failure on line 40 in docs/node/cjs.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Strings must use singlequote

Check failure on line 40 in docs/node/cjs.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Extra semicolon

// Register tsx enhancement
const unregister = tsx.register()
const unregister = tsx.register();

Check failure on line 43 in docs/node/cjs.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Extra semicolon

// Unregister when needed
unregister()
unregister();

Check failure on line 46 in docs/node/cjs.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Extra semicolon
```
44 changes: 24 additions & 20 deletions docs/node/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This section is only for adding tsx in Module mode (doesn't affect `.cjs` or `.c
::: warning Not for 3rd-party packages
This enhances the entire runtime so it may not be suitable for loading TypeScript files from a 3rd-party package as it may lead to unexpected behavior in user code.

For importing TypeScript files in Module mode without affecting the environment, see the *Scoped registration* section below or [`tsImport()`](http://localhost:5173/node/ts-import).
For importing TypeScript files in Module mode without affecting the environment, see the _Scoped registration_ section below or [`tsImport()`](/node/ts-import).
:::

## Command-line API
Expand All @@ -33,58 +33,62 @@ NODE_OPTIONS='--loader tsx/esm' npx some-binary
## Programmatic API

### Registration & Unregistration

```js
import { register } from 'tsx/esm/api'
import { register } from "tsx/esm/api";

Check failure on line 38 in docs/node/esm.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Strings must use singlequote

Check failure on line 38 in docs/node/esm.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Extra semicolon

// register tsx enhancement
const unregister = register()
const unregister = register();

Check failure on line 41 in docs/node/esm.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Extra semicolon

// Unregister when needed
unregister()
unregister();

Check failure on line 44 in docs/node/esm.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Extra semicolon
```

#### Tracking loaded files

Detect files that get loaded with the `onImport` hook:

```ts
register({
onImport: (file: string) => {
console.log(file)
// file:///foo.ts
}
})
onImport: (file: string) => {
console.log(file);
// file:///foo.ts
},
});
```

#### Tracking loaded files

Detect files that get loaded with the `onImport` hook:

```ts
register({
onImport: (file: string) => {
console.log(file)
// file:///foo.ts
}
})
onImport: (file: string) => {
console.log(file);
// file:///foo.ts
},
});
```

### Scoped registration

If you want to register tsx without affecting the environment, you can add a namespace.

```js
import { register } from 'tsx/esm/api'
import { register } from "tsx/esm/api";

// register tsx enhancement
const api = register({
namespace: Date.now().toString()
})
namespace: Date.now().toString(),
});

// You get a private `import()` function to load TypeScript files
// Since this is namespaced, it will not cache hit from prior imports
const loaded = await api.import('./file.ts', import.meta.url)
const loaded = await api.import("./file.ts", import.meta.url);

Check warning on line 87 in docs/node/esm.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'loaded' is assigned a value but never used

// This is using the same namespace as above, so it will yield a cache hit
const loaded2 = await api.import('./file.ts', import.meta.url)
const loaded2 = await api.import("./file.ts", import.meta.url);

Check warning on line 90 in docs/node/esm.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'loaded2' is assigned a value but never used

// Unregister when needed
api.unregister()
api.unregister();
```
28 changes: 14 additions & 14 deletions docs/node/ts-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@ CommonJS files are currently not enhanced due to this [Node.js bug](https://gith

## ESM usage


```js
import { tsImport } from 'tsx/esm/api'
import { tsImport } from "tsx/esm/api";

const loaded = await tsImport('./file.ts', import.meta.url)
const loaded = await tsImport("./file.ts", import.meta.url);

Check warning on line 20 in docs/node/ts-import.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'loaded' is assigned a value but never used

// If tsImport is used to load file.ts again,
// it does not yield a cache-hit and re-loads it
const loadedAgain = await tsImport('./file.ts', import.meta.url)
const loadedAgain = await tsImport("./file.ts", import.meta.url);

Check warning on line 24 in docs/node/ts-import.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'loadedAgain' is assigned a value but never used
```
If you'd like to leverage module caching, see the [ESM scoped registration](http://localhost:5173/node/esm#scoped-registration) section.
If you'd like to leverage module caching, see the [ESM scoped registration](/node/esm#scoped-registration) section.
## CommonJS usage
```js
const { tsImport } = require('tsx/esm/api')
const { tsImport } = require("tsx/esm/api");

const loaded = await tsImport('./file.ts', __filename)
const loaded = await tsImport("./file.ts", __filename);

Check warning on line 34 in docs/node/ts-import.md

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'loaded' is assigned a value but never used
```
## Tracking loaded files
Detect files that get loaded with the `onImport` hook:
```ts
tsImport('./file.ts', {
parentURL: import.meta.url,
onImport: (file: string) => {
console.log(file)
// file:///foo.ts
}
})
tsImport("./file.ts", {
parentURL: import.meta.url,
onImport: (file: string) => {
console.log(file);
// file:///foo.ts
},
});
```

0 comments on commit 3f0b0d0

Please sign in to comment.