Skip to content

Commit 3ebe65c

Browse files
authored
Re-organize types for maximum compatibility
Add shims for TypeScript's 'node' module resolution algorithm. The exports map is only used with the `node16` algorithm. Write types in CommonJS, so they can be exported from ESM files. The other way around is prone to, at the very least, confusing warnings.
1 parent 2d3f394 commit 3ebe65c

11 files changed

+105
-97
lines changed

entrypoints/main.d.cts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1-
export {default} from './main.js';
2-
export * from './main.js';
1+
import type {TestFn} from '../types/test-fn.cjs';
2+
3+
export * from '../types/assertions.cjs';
4+
export * from '../types/try-fn.cjs';
5+
export * from '../types/test-fn.cjs';
6+
export * from '../types/subscribable.cjs';
7+
8+
/** Call to declare a test, or chain to declare hooks or test modifiers */
9+
declare const test: TestFn;
10+
11+
/** Call to declare a test, or chain to declare hooks or test modifiers */
12+
export default test;

entrypoints/main.d.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
1-
import type {TestFn} from '../types/test-fn.js';
2-
3-
export * from '../types/assertions.js';
4-
export * from '../types/try-fn.js';
5-
export * from '../types/test-fn.js';
6-
export * from '../types/subscribable.js';
7-
8-
/** Call to declare a test, or chain to declare hooks or test modifiers */
9-
declare const test: TestFn;
10-
11-
/** Call to declare a test, or chain to declare hooks or test modifiers */
12-
export default test;
1+
export * from './main.cjs';
2+
export {default} from './main.cjs';

entrypoints/plugin.d.cts

+77-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,77 @@
1-
export * from './plugin.js'
1+
import type {URL} from 'node:url';
2+
3+
export namespace SharedWorker {
4+
export type ProtocolIdentifier = 'ava-4';
5+
6+
export type FactoryOptions = {
7+
negotiateProtocol <Data = unknown>(supported: readonly ['ava-4']): Protocol<Data>;
8+
// Add overloads for additional protocols.
9+
};
10+
11+
export type Factory = (options: FactoryOptions) => void;
12+
13+
export type Protocol<Data = unknown> = {
14+
readonly initialData: Data;
15+
readonly protocol: 'ava-4';
16+
broadcast: (data: Data) => BroadcastMessage<Data>;
17+
ready: () => Protocol<Data>;
18+
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
19+
testWorkers: () => AsyncIterableIterator<TestWorker<Data>>;
20+
};
21+
22+
export type BroadcastMessage<Data = unknown> = {
23+
readonly id: string;
24+
replies: () => AsyncIterableIterator<ReceivedMessage<Data>>;
25+
};
26+
27+
export type PublishedMessage<Data = unknown> = {
28+
readonly id: string;
29+
replies: () => AsyncIterableIterator<ReceivedMessage<Data>>;
30+
};
31+
32+
export type ReceivedMessage<Data = unknown> = {
33+
readonly data: Data;
34+
readonly id: string;
35+
readonly testWorker: TestWorker;
36+
reply: (data: Data) => PublishedMessage<Data>;
37+
};
38+
39+
export type TestWorker<Data = unknown> = {
40+
readonly id: string;
41+
readonly file: string;
42+
publish: (data: Data) => PublishedMessage<Data>;
43+
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
44+
teardown: (fn: (() => Promise<void>) | (() => void)) => () => Promise<void>;
45+
};
46+
47+
export namespace Plugin {
48+
export type RegistrationOptions<Identifier extends ProtocolIdentifier, Data = unknown> = {
49+
readonly filename: string | URL;
50+
readonly initialData?: Data;
51+
readonly supportedProtocols: readonly Identifier[];
52+
readonly teardown?: () => void;
53+
};
54+
55+
export type Protocol<Data = unknown> = {
56+
readonly available: Promise<void>;
57+
readonly currentlyAvailable: boolean;
58+
readonly protocol: 'ava-4';
59+
publish: (data: Data) => PublishedMessage<Data>;
60+
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
61+
};
62+
63+
export type PublishedMessage<Data = unknown> = {
64+
readonly id: string;
65+
replies: () => AsyncIterableIterator<ReceivedMessage<Data>>;
66+
};
67+
68+
export type ReceivedMessage<Data = unknown> = {
69+
readonly data: Data;
70+
readonly id: string;
71+
reply: (data: Data) => PublishedMessage<Data>;
72+
};
73+
}
74+
}
75+
76+
export function registerSharedWorker<Data = unknown>(options: SharedWorker.Plugin.RegistrationOptions<'ava-4', Data>): SharedWorker.Plugin.Protocol<Data>;
77+
// Add overloads for additional protocols.

entrypoints/plugin.d.ts

+1-77
Original file line numberDiff line numberDiff line change
@@ -1,77 +1 @@
1-
import type {URL} from 'node:url';
2-
3-
export namespace SharedWorker {
4-
export type ProtocolIdentifier = 'ava-4';
5-
6-
export type FactoryOptions = {
7-
negotiateProtocol <Data = unknown>(supported: readonly ['ava-4']): Protocol<Data>;
8-
// Add overloads for additional protocols.
9-
};
10-
11-
export type Factory = (options: FactoryOptions) => void;
12-
13-
export type Protocol<Data = unknown> = {
14-
readonly initialData: Data;
15-
readonly protocol: 'ava-4';
16-
broadcast: (data: Data) => BroadcastMessage<Data>;
17-
ready: () => Protocol<Data>;
18-
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
19-
testWorkers: () => AsyncIterableIterator<TestWorker<Data>>;
20-
};
21-
22-
export type BroadcastMessage<Data = unknown> = {
23-
readonly id: string;
24-
replies: () => AsyncIterableIterator<ReceivedMessage<Data>>;
25-
};
26-
27-
export type PublishedMessage<Data = unknown> = {
28-
readonly id: string;
29-
replies: () => AsyncIterableIterator<ReceivedMessage<Data>>;
30-
};
31-
32-
export type ReceivedMessage<Data = unknown> = {
33-
readonly data: Data;
34-
readonly id: string;
35-
readonly testWorker: TestWorker;
36-
reply: (data: Data) => PublishedMessage<Data>;
37-
};
38-
39-
export type TestWorker<Data = unknown> = {
40-
readonly id: string;
41-
readonly file: string;
42-
publish: (data: Data) => PublishedMessage<Data>;
43-
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
44-
teardown: (fn: (() => Promise<void>) | (() => void)) => () => Promise<void>;
45-
};
46-
47-
export namespace Plugin {
48-
export type RegistrationOptions<Identifier extends ProtocolIdentifier, Data = unknown> = {
49-
readonly filename: string | URL;
50-
readonly initialData?: Data;
51-
readonly supportedProtocols: readonly Identifier[];
52-
readonly teardown?: () => void;
53-
};
54-
55-
export type Protocol<Data = unknown> = {
56-
readonly available: Promise<void>;
57-
readonly currentlyAvailable: boolean;
58-
readonly protocol: 'ava-4';
59-
publish: (data: Data) => PublishedMessage<Data>;
60-
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
61-
};
62-
63-
export type PublishedMessage<Data = unknown> = {
64-
readonly id: string;
65-
replies: () => AsyncIterableIterator<ReceivedMessage<Data>>;
66-
};
67-
68-
export type ReceivedMessage<Data = unknown> = {
69-
readonly data: Data;
70-
readonly id: string;
71-
reply: (data: Data) => PublishedMessage<Data>;
72-
};
73-
}
74-
}
75-
76-
export function registerSharedWorker<Data = unknown>(options: SharedWorker.Plugin.RegistrationOptions<'ava-4', Data>): SharedWorker.Plugin.Protocol<Data>;
77-
// Add overloads for additional protocols.
1+
export type * from './plugin.cjs';

index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// For compatibility with resolution algorithms other than Node16.
2+
3+
export * from './entrypoints/main.cjs';
4+
export {default} from './entrypoints/main.cjs';

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"files": [
4343
"entrypoints",
4444
"lib",
45-
"types"
45+
"types",
46+
"*.d.ts"
4647
],
4748
"keywords": [
4849
"🦄",

plugin.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// For compatibility with resolution algorithms other than Node16.
2+
3+
export * from './entrypoints/plugin.cjs';
File renamed without changes.
File renamed without changes.

types/test-fn.d.ts types/test-fn.d.cts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type {Assertions} from './assertions.js';
2-
import type {Subscribable} from './subscribable.js';
3-
import type {TryFn} from './try-fn.js';
1+
import type {Assertions} from './assertions.cjs';
2+
import type {Subscribable} from './subscribable.cjs';
3+
import type {TryFn} from './try-fn.cjs';
44

55
/** The `t` value passed to test & hook implementations. */
66
export type ExecutionContext<Context = unknown> = {

types/try-fn.d.ts types/try-fn.d.cts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {Implementation} from './test-fn.js';
1+
import type {Implementation} from './test-fn.cjs';
22

33
export type CommitDiscardOptions = {
44
/**

0 commit comments

Comments
 (0)