diff --git a/rollup.config.js b/rollup.config.js index b08e71c2..1d1dd304 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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'; diff --git a/scripts/generate-cts.js b/scripts/generate-cts.js new file mode 100644 index 00000000..eed0e795 --- /dev/null +++ b/scripts/generate-cts.js @@ -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 +}; \ No newline at end of file diff --git a/src/babel-plugin-factories.json b/src/babel-plugin-factories.json index 5588f10f..7a63c8a4 100644 --- a/src/babel-plugin-factories.json +++ b/src/babel-plugin-factories.json @@ -18,6 +18,7 @@ "patronum/or", "patronum/pending", "patronum/previous", + "patronum/readonly", "patronum/reset", "patronum/reshape", "patronum/snapshot", @@ -46,6 +47,7 @@ "or": "or", "pending": "pending", "previous": "previous", + "readonly": "readonly", "reset": "reset", "reshape": "reshape", "snapshot": "snapshot", diff --git a/src/debounce/debounce.fork.test.ts b/src/debounce/debounce.fork.test.ts index 130d076f..0d89ff0b 100644 --- a/src/debounce/debounce.fork.test.ts +++ b/src/debounce/debounce.fork.test.ts @@ -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); - }) + }); }); diff --git a/src/empty/empty.fork.test.ts b/src/empty/empty.fork.test.ts index 542f99d2..1958ec10 100644 --- a/src/empty/empty.fork.test.ts +++ b/src/empty/empty.fork.test.ts @@ -39,11 +39,14 @@ test('strings', async () => { allSettled(set, { scope, params: 'hello' }); expect(scope.getState($empty)).toBe(false); -}) +}); test('void', async () => { const set = createEvent(); - const $str = createStore(null, {skipVoid: false}).on(set, (_, str) => str); + const $str = createStore(null, { skipVoid: false }).on( + set, + (_, str) => str, + ); const $empty = empty($str); const scope = fork(); diff --git a/src/interval/interval.test.ts b/src/interval/interval.test.ts index d90557b6..7745f88b 100644 --- a/src/interval/interval.test.ts +++ b/src/interval/interval.test.ts @@ -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(); }); diff --git a/src/once/index.ts b/src/once/index.ts index 4a70863d..e413e007 100644 --- a/src/once/index.ts +++ b/src/once/index.ts @@ -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(config: { source: Unit; @@ -39,5 +32,5 @@ export function once( $canTrigger.reset(reset); } - return trigger + return trigger; } diff --git a/src/pending/pending.fork.test.ts b/src/pending/pending.fork.test.ts index 2d1d1621..1cf2f44a 100644 --- a/src/pending/pending.fork.test.ts +++ b/src/pending/pending.fork.test.ts @@ -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`); @@ -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`); diff --git a/src/snapshot/index.ts b/src/snapshot/index.ts index 30b60ab7..210240d2 100644 --- a/src/snapshot/index.ts +++ b/src/snapshot/index.ts @@ -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 extends any ? 0 : never]; diff --git a/src/throttle/throttle.fork.test.ts b/src/throttle/throttle.fork.test.ts index 7d43bcc6..2ac32753 100644 --- a/src/throttle/throttle.fork.test.ts +++ b/src/throttle/throttle.fork.test.ts @@ -30,7 +30,7 @@ test('throttle works in forked scope', async () => { }); expect(serialize(scope)).toMatchObject({ - [$counter.sid!]: 1 + [$counter.sid!]: 1, }); }); @@ -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, }); }); @@ -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`); @@ -156,4 +156,4 @@ describe('edge cases', () => { expect(listener).toBeCalledTimes(1); expect(listener).toBeCalledWith('two'); }); -}) +});