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

Add .d.cts types #341

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const terser = require('@rollup/plugin-terser');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const { typesPlugin } = require('./scripts/generate-cts');

const plugins = [
nodeResolve({ jsnext: true, skip: ['effector'], extensions: ['.js', '.mjs'] }),
commonjs({ extensions: ['.js', '.mjs'] }),
terser(),
typesPlugin(),
];

const input = 'dist/index.cjs';
Expand Down
40 changes: 40 additions & 0 deletions scripts/generate-cts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { readdir, copyFile, stat } = require('node:fs/promises');
const path = require('node:path');

async function getAllFiles(dir) {
const subdirs = await readdir(dir);
const files = await Promise.all(
subdirs.map(async (subdir) => {
const res = path.resolve(dir, subdir);
return (await stat(res)).isDirectory() ? getAllFiles(res) : res;
})
);
return files.reduce((a, f) => a.concat(f), []);
}

function typesPlugin() {
return {
name: 'afterBuild-plugin',
async writeBundle() {
try {
const files = await getAllFiles('dist');
const dtsFiles = files.filter((file) => file.endsWith('.d.ts'));

await Promise.all(
dtsFiles.map((file) => {
const newFile = file.replace('.d.ts', '.d.cts');
return copyFile(file, newFile);
})
);

console.log('Copied .d.ts files to .d.cts');
} catch (error) {
console.error('Error in afterBuild plugin:', error);
}
},
};
}

module.exports = {
typesPlugin
};
2 changes: 2 additions & 0 deletions src/babel-plugin-factories.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"patronum/or",
"patronum/pending",
"patronum/previous",
"patronum/readonly",
"patronum/reset",
"patronum/reshape",
"patronum/snapshot",
Expand Down Expand Up @@ -46,6 +47,7 @@
"or": "or",
"pending": "pending",
"previous": "previous",
"readonly": "readonly",
"reset": "reset",
"reshape": "reshape",
"snapshot": "snapshot",
Expand Down
8 changes: 4 additions & 4 deletions src/debounce/debounce.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,21 @@ describe('edge cases', () => {

const listener = jest.fn();
const triggerListener = jest.fn();
const scope = fork()
const scope = fork();
createWatch({
unit: db,
fn: listener,
scope,
})
});
createWatch({
unit: trigger,
fn: triggerListener,
scope,
})
});

await allSettled(changeTimeout, { scope, params: 10 });

expect(listener).toBeCalledTimes(0);
expect(triggerListener).toBeCalledTimes(0);
})
});
});
7 changes: 5 additions & 2 deletions src/empty/empty.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ test('strings', async () => {
allSettled(set, { scope, params: 'hello' });

expect(scope.getState($empty)).toBe(false);
})
});

test('void', async () => {
const set = createEvent<any>();
const $str = createStore<null | undefined>(null, {skipVoid: false}).on(set, (_, str) => str);
const $str = createStore<null | undefined>(null, { skipVoid: false }).on(
set,
(_, str) => str,
);
const $empty = empty($str);

const scope = fork();
Expand Down
2 changes: 1 addition & 1 deletion src/interval/interval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('timeout', () => {
increaseTimeout(); // timeout will change to 40 for next tick
await wait(32); // t=80
expect(fn).toBeCalledTimes(4); // ticked at t=60
// and next tick should be at t=100
// and next tick should be at t=100

stop();
});
Expand Down
11 changes: 2 additions & 9 deletions src/once/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
Unit,
Event,
EventAsReturnType,
is,
sample,
createStore,
} from 'effector';
import { Unit, Event, EventAsReturnType, is, sample, createStore } from 'effector';

export function once<T>(config: {
source: Unit<T>;
Expand Down Expand Up @@ -39,5 +32,5 @@ export function once<T>(
$canTrigger.reset(reset);
}

return trigger
return trigger;
}
4 changes: 2 additions & 2 deletions src/pending/pending.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test('concurrent run of different effects', async () => {
});
const $pending = pending({ effects: [effect1, effect2] });
const run = app.createEvent();
sample({ clock: run, target: [effect1, effect2] });
sample({ clock: run, target: [effect1, effect2] });

const scope = fork();
expect(scope.getState($pending)).toMatchInlineSnapshot(`false`);
Expand All @@ -72,7 +72,7 @@ test('concurrent run of different effects with domain', async () => {
});
const $pending = pending({ domain: app });
const run = app.createEvent();
sample({ clock: run, target: [effect1, effect2] });
sample({ clock: run, target: [effect1, effect2] });

const scope = fork();
expect(scope.getState($pending)).toMatchInlineSnapshot(`false`);
Expand Down
10 changes: 9 additions & 1 deletion src/snapshot/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Effect, Event, Store, StoreWritable, Unit, createStore, sample } from 'effector';
import {
Effect,
Event,
Store,
StoreWritable,
Unit,
createStore,
sample,
} from 'effector';

type NoInfer<T> = [T][T extends any ? 0 : never];

Expand Down
10 changes: 5 additions & 5 deletions src/throttle/throttle.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test('throttle works in forked scope', async () => {
});

expect(serialize(scope)).toMatchObject({
[$counter.sid!]: 1
[$counter.sid!]: 1,
});
});

Expand Down Expand Up @@ -68,11 +68,11 @@ test('throttle do not affect another forks', async () => {
});

expect(serialize(scopeA)).toMatchObject({
[$counter.sid!]: 2
[$counter.sid!]: 2,
});

expect(serialize(scopeB)).toMatchObject({
[$counter.sid!]: 200
[$counter.sid!]: 200,
});
});

Expand All @@ -98,7 +98,7 @@ test('throttle do not affect original store value', async () => {
});

expect(serialize(scope)).toMatchObject({
[$counter.sid!]: 2
[$counter.sid!]: 2,
});

expect($counter.getState()).toMatchInlineSnapshot(`0`);
Expand Down Expand Up @@ -156,4 +156,4 @@ describe('edge cases', () => {
expect(listener).toBeCalledTimes(1);
expect(listener).toBeCalledWith('two');
});
})
});