Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expose types for esm/api #551

Merged
merged 3 commits into from
May 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: expose types and fix overloads for
antfu committed May 13, 2024
commit 2ed4b433bf3d091efe53888b017a762533c58c29
24 changes: 13 additions & 11 deletions src/esm/api/register.ts
Original file line number Diff line number Diff line change
@@ -3,26 +3,28 @@
import type { Message } from '../types.js';
import { createScopedImport, type ScopedImport } from './scoped-import.js';

export type { ScopedImport } from './scoped-import.js';

export type InitializationOptions = {
namespace?: string;
port?: MessagePort;
};

type Options = {
export type RegisterOptions = {
namespace?: string;
onImport?: (url: string) => void;
};

type Unregister = () => Promise<void>;
type Register = {
(options: {
namespace: string;
onImport?: (url: string) => void;
}): Unregister & {
import: ScopedImport;
unregister: Unregister;
};
(options?: Options): Unregister;
export type NamespacedRegister = {
import: ScopedImport;
unregister: Unregister;
}

Check failure on line 21 in src/esm/api/register.ts

GitHub Actions / Test (ubuntu-latest)

Missing semicolon

export type Unregister = () => Promise<void>;

export type Register = {
(options: RegisterOptions & Pick<Required<RegisterOptions>, 'namespace'>): Unregister & NamespacedRegister

Check failure on line 26 in src/esm/api/register.ts

GitHub Actions / Test (ubuntu-latest)

Expected a semicolon
(options?: RegisterOptions): Unregister;
};

export const register: Register = (

Unchanged files with check annotations Beta

// 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)

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

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)

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

GitHub Actions / Test (ubuntu-latest)

'loaded2' is assigned a value but never used
// Unregister when needed
api.unregister()
```js
import { tsImport } from 'tsx/esm/api'
const loaded = await tsImport('./file.ts', import.meta.url)

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

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)

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

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.
```js
const { tsImport } = require('tsx/esm/api')
const loaded = await tsImport('./file.ts', __filename)

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

GitHub Actions / Test (ubuntu-latest)

'loaded' is assigned a value but never used
```
## Tracking loaded files
```js
const tsx = require('tsx/cjs/api')
const tsLoaded = tsx.require('./file.ts', __filename)

Check warning on line 19 in docs/node/tsx-require.md

GitHub Actions / Test (ubuntu-latest)

'tsLoaded' is assigned a value but never used
const tsFilepath = tsx.require.resolve('./file.ts', __filename)

Check warning on line 20 in docs/node/tsx-require.md

GitHub Actions / Test (ubuntu-latest)

'tsFilepath' is assigned a value but never used
```
## ESM usage
```js
import { require } from 'tsx/cjs/api'
const tsLoaded = require('./file.ts', import.meta.url)

Check warning on line 28 in docs/node/tsx-require.md

GitHub Actions / Test (ubuntu-latest)

'tsLoaded' is assigned a value but never used
const tsFilepath = require.resolve('./file.ts', import.meta.url)

Check warning on line 29 in docs/node/tsx-require.md

GitHub Actions / Test (ubuntu-latest)

'tsFilepath' is assigned a value but never used
```
## Tracking loaded files
import { resolveFilename } from './module-resolve-filename.js';
export const register = () => {
const { sourceMapsEnabled } = process;

Check warning on line 6 in src/cjs/api/global-require-patch.ts

GitHub Actions / Test (ubuntu-latest)

The 'process.sourceMapsEnabled' is still an experimental feature The configured version range is '>=20.12.2'
const { _extensions, _resolveFilename } = Module;
// register