diff --git a/docs/node/cjs.md b/docs/node/cjs.md index b60e40df1..d1673d0e1 100644 --- a/docs/node/cjs.md +++ b/docs/node/cjs.md @@ -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`. @@ -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 @@ -30,7 +29,7 @@ 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"); ``` ### Registration & Unregistration @@ -38,11 +37,11 @@ require('tsx/cjs') To manually register and unregister the tsx enhancement: ```js -const tsx = require('tsx/cjs/api') +const tsx = require("tsx/cjs/api"); // Register tsx enhancement -const unregister = tsx.register() +const unregister = tsx.register(); // Unregister when needed -unregister() +unregister(); ``` diff --git a/docs/node/esm.md b/docs/node/esm.md index f7ffb56c3..333dba405 100644 --- a/docs/node/esm.md +++ b/docs/node/esm.md @@ -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 @@ -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"; // register tsx enhancement -const unregister = register() +const unregister = register(); // Unregister when needed -unregister() +unregister(); ``` #### 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); // 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); // Unregister when needed -api.unregister() +api.unregister(); ``` diff --git a/docs/node/ts-import.md b/docs/node/ts-import.md index 52cb05ffc..2db9b7daa 100644 --- a/docs/node/ts-import.md +++ b/docs/node/ts-import.md @@ -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); // 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); ``` -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); ``` ## 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 + }, +}); ```