@modern-js/plugin 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/js/modern/manager/async.js +51 -24
  3. package/dist/js/modern/manager/index.js +2 -1
  4. package/dist/js/modern/manager/sync.js +71 -56
  5. package/dist/js/modern/manager/types.js +0 -0
  6. package/dist/js/modern/waterfall/async.js +0 -1
  7. package/dist/js/modern/waterfall/sync.js +0 -1
  8. package/dist/js/modern/workflow/async.js +1 -2
  9. package/dist/js/modern/workflow/sync.js +0 -1
  10. package/dist/js/node/manager/async.js +49 -21
  11. package/dist/js/node/manager/index.js +13 -0
  12. package/dist/js/node/manager/sync.js +76 -54
  13. package/dist/js/node/manager/types.js +0 -0
  14. package/dist/js/node/waterfall/async.js +0 -1
  15. package/dist/js/node/waterfall/sync.js +0 -1
  16. package/dist/js/node/workflow/async.js +1 -2
  17. package/dist/js/node/workflow/sync.js +0 -1
  18. package/dist/js/treeshaking/manager/async.js +55 -30
  19. package/dist/js/treeshaking/manager/index.js +2 -1
  20. package/dist/js/treeshaking/manager/sync.js +77 -58
  21. package/dist/js/treeshaking/manager/types.js +0 -0
  22. package/dist/js/treeshaking/waterfall/async.js +0 -1
  23. package/dist/js/treeshaking/waterfall/sync.js +0 -1
  24. package/dist/js/treeshaking/workflow/async.js +1 -2
  25. package/dist/js/treeshaking/workflow/sync.js +0 -1
  26. package/dist/types/manager/async.d.ts +60 -21
  27. package/dist/types/manager/index.d.ts +2 -1
  28. package/dist/types/manager/sync.d.ts +74 -43
  29. package/dist/types/manager/types.d.ts +41 -0
  30. package/package.json +3 -3
  31. package/tests/async.test.ts +125 -7
  32. package/tests/fixtures/async/core/index.ts +12 -4
  33. package/tests/fixtures/async/dynamic/foo.ts +2 -2
  34. package/tests/fixtures/sync/core/index.ts +9 -4
  35. package/tests/fixtures/sync/dynamic/foo.ts +2 -2
  36. package/tests/sync.test.ts +119 -6
@@ -7,6 +7,7 @@ import {
7
7
  createContext,
8
8
  createContainer,
9
9
  } from 'farrow-pipeline';
10
+ import type { PluginOptions, AsyncSetup } from '../src';
10
11
  import { createManager, createAsyncManager, useRunner } from '../src/manager';
11
12
  import { createWaterfall, createAsyncWaterfall } from '../src/waterfall';
12
13
  import {
@@ -14,7 +15,7 @@ import {
14
15
  createAsyncWorkflow,
15
16
  createParallelWorkflow,
16
17
  } from '../src/workflow';
17
- import { main } from './fixtures/async/core';
18
+ import { main, TestAsyncHooks, TestAsyncPlugin } from './fixtures/async/core';
18
19
  import foo from './fixtures/async/base/foo';
19
20
  import bar, { getBar } from './fixtures/async/base/bar';
20
21
  import dFoo from './fixtures/async/dynamic/foo';
@@ -40,7 +41,7 @@ describe('async manager', () => {
40
41
  expect(getBar()).toBe(3);
41
42
  });
42
43
 
43
- it('should support async initializer', async () => {
44
+ it('should support async setup function', async () => {
44
45
  const manager = createAsyncManager();
45
46
 
46
47
  const countContext = createContext(0);
@@ -397,6 +398,22 @@ describe('async manager', () => {
397
398
  expect(count).toBe(2);
398
399
  });
399
400
 
401
+ it('should support manager clone and override pluginAPI', done => {
402
+ const myAPI = { hello: () => 1 };
403
+ const manager = createAsyncManager({}, myAPI);
404
+ const plugin = {
405
+ setup(api: typeof myAPI) {
406
+ expect(api.hello()).toEqual(2);
407
+ done();
408
+ },
409
+ };
410
+ const clonedManager = manager.clone({
411
+ hello: () => 2,
412
+ });
413
+
414
+ clonedManager.usePlugin(() => plugin).init();
415
+ });
416
+
400
417
  it('isPlugin if exclusive plugins of manager', () => {
401
418
  const manager0 = createManager();
402
419
  const manager1 = createAsyncManager();
@@ -501,11 +518,11 @@ describe('async manager', () => {
501
518
 
502
519
  runner.pipeline({});
503
520
  await runner.asyncPipeline({});
504
- runner.waterfall({});
505
- await runner.asyncWaterfall({});
506
- runner.workflow({});
507
- await runner.asyncWorkflow({});
508
- await runner.parallelWorkflow({});
521
+ runner.waterfall();
522
+ await runner.asyncWaterfall();
523
+ runner.workflow();
524
+ await runner.asyncWorkflow();
525
+ await runner.parallelWorkflow();
509
526
 
510
527
  expect(list).toStrictEqual([
511
528
  'pipeline',
@@ -591,4 +608,105 @@ describe('async manager', () => {
591
608
  );
592
609
  });
593
610
  });
611
+
612
+ describe('setup api', () => {
613
+ it('should allow to access api.useHookRunners by default', done => {
614
+ const manager = createAsyncManager<TestAsyncHooks>();
615
+ const plugin: TestAsyncPlugin = {
616
+ name: 'plugin',
617
+ setup: api => {
618
+ expect(api.useHookRunners).toBeTruthy();
619
+ done();
620
+ },
621
+ };
622
+ manager.usePlugin(() => plugin);
623
+ manager.init();
624
+ });
625
+
626
+ it('should allow to register extra api', done => {
627
+ type API = { foo: () => void };
628
+
629
+ const manager = createAsyncManager<TestAsyncHooks, API>(
630
+ {},
631
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
632
+ { foo: () => {} },
633
+ );
634
+
635
+ const plugin: PluginOptions<
636
+ TestAsyncHooks,
637
+ AsyncSetup<TestAsyncHooks, API>
638
+ > = {
639
+ name: 'plugin',
640
+ setup: api => {
641
+ expect(api.foo).toBeTruthy();
642
+ done();
643
+ },
644
+ };
645
+
646
+ manager.usePlugin(() => plugin);
647
+ manager.init();
648
+ });
649
+ });
650
+
651
+ describe('usePlugins option', () => {
652
+ it('should allow to use single plugin', async () => {
653
+ const manager = createAsyncManager<TestAsyncHooks>();
654
+
655
+ const list: number[] = [];
656
+ const plugin0: TestAsyncPlugin = {
657
+ name: 'plugin0',
658
+ setup: () => {
659
+ list.push(0);
660
+ },
661
+ };
662
+
663
+ const plugin1: TestAsyncPlugin = {
664
+ name: 'plugin1',
665
+ usePlugins: [plugin0],
666
+ setup: () => {
667
+ list.push(1);
668
+ },
669
+ };
670
+
671
+ manager.usePlugin(() => plugin1);
672
+
673
+ await manager.init();
674
+
675
+ expect(list).toStrictEqual([0, 1]);
676
+ });
677
+
678
+ it('should allow to use multiple plugins', async () => {
679
+ const manager = createAsyncManager<TestAsyncHooks>();
680
+
681
+ const list: number[] = [];
682
+ const plugin0: TestAsyncPlugin = {
683
+ name: 'plugin0',
684
+ setup: () => {
685
+ list.push(0);
686
+ },
687
+ };
688
+
689
+ const plugin1 = {
690
+ name: 'plugin1',
691
+ usePlugins: [plugin0],
692
+ setup: () => {
693
+ list.push(1);
694
+ },
695
+ };
696
+
697
+ const plugin2 = {
698
+ name: 'plugin2',
699
+ usePlugins: [plugin1],
700
+ setup: () => {
701
+ list.push(2);
702
+ },
703
+ };
704
+
705
+ manager.usePlugin(() => plugin2);
706
+
707
+ await manager.init();
708
+
709
+ expect(list).toStrictEqual([0, 1, 2]);
710
+ });
711
+ });
594
712
  });
@@ -3,6 +3,8 @@ import {
3
3
  createWaterfall,
4
4
  createWorkflow,
5
5
  createContext,
6
+ PluginOptions,
7
+ AsyncSetup,
6
8
  } from '../../../../src';
7
9
 
8
10
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -99,9 +101,15 @@ const lifecircle = {
99
101
  preBuild,
100
102
  postBuild,
101
103
  };
102
- export const main = createAsyncManager<ExternalProgress, typeof lifecircle>(
103
- lifecircle,
104
- );
104
+
105
+ export type TestAsyncHooks = ExternalProgress & typeof lifecircle;
106
+
107
+ export const main = createAsyncManager<TestAsyncHooks>(lifecircle);
108
+
109
+ export type TestAsyncPlugin = PluginOptions<
110
+ TestAsyncHooks,
111
+ AsyncSetup<TestAsyncHooks>
112
+ >;
105
113
 
106
114
  export const { createPlugin } = main;
107
115
 
@@ -111,7 +119,7 @@ export const { usePlugin } = main;
111
119
 
112
120
  export const initPlugins = main.init;
113
121
 
114
- export const registeManager = main.registe;
122
+ export const { registerHook } = main;
115
123
 
116
124
  export const { useRunner } = main;
117
125
 
@@ -1,5 +1,5 @@
1
1
  import { createAsyncWaterfall } from '../../../../src';
2
- import { createPlugin, registeManager, useRunner } from '../core';
2
+ import { createPlugin, registerHook, useRunner } from '../core';
3
3
 
4
4
  // declare new lifecircle type
5
5
  declare module '../core' {
@@ -13,7 +13,7 @@ const fooWaterfall = createAsyncWaterfall();
13
13
 
14
14
  const foo = createPlugin(() => {
15
15
  // registe new lifecircle
16
- registeManager({ fooWaterfall });
16
+ registerHook({ fooWaterfall });
17
17
 
18
18
  return {
19
19
  preDev: () => {
@@ -1,8 +1,10 @@
1
1
  import {
2
+ Setup,
2
3
  createManager,
3
4
  createWaterfall,
4
5
  createWorkflow,
5
6
  createContext,
7
+ PluginOptions,
6
8
  } from '../../../../src';
7
9
 
8
10
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -99,9 +101,12 @@ const lifecircle = {
99
101
  preBuild,
100
102
  postBuild,
101
103
  };
102
- export const main = createManager<ExternalProgress, typeof lifecircle>(
103
- lifecircle,
104
- );
104
+
105
+ export type TestHooks = ExternalProgress & typeof lifecircle;
106
+
107
+ export const main = createManager<TestHooks>(lifecircle);
108
+
109
+ export type TestPlugin = PluginOptions<TestHooks, Setup<TestHooks>>;
105
110
 
106
111
  export const { createPlugin } = main;
107
112
 
@@ -111,7 +116,7 @@ export const { usePlugin } = main;
111
116
 
112
117
  export const initPlugins = main.init;
113
118
 
114
- export const registeManager = main.registe;
119
+ export const { registerHook } = main;
115
120
 
116
121
  export const { useRunner } = main;
117
122
 
@@ -1,5 +1,5 @@
1
1
  import { createAsyncWaterfall } from '../../../../src';
2
- import { createPlugin, registeManager, useRunner } from '../core';
2
+ import { createPlugin, registerHook, useRunner } from '../core';
3
3
 
4
4
  // declare new lifecircle type
5
5
  declare module '../core' {
@@ -13,7 +13,7 @@ const fooWaterfall = createAsyncWaterfall();
13
13
 
14
14
  const foo = createPlugin(() => {
15
15
  // registe new lifecircle
16
- registeManager({ fooWaterfall });
16
+ registerHook({ fooWaterfall });
17
17
 
18
18
  return {
19
19
  preDev: () => {
@@ -6,6 +6,7 @@ import {
6
6
  createContext,
7
7
  createContainer,
8
8
  } from 'farrow-pipeline';
9
+ import type { PluginOptions, Setup } from '../src';
9
10
  import { createManager, createAsyncManager, useRunner } from '../src/manager';
10
11
  import { createWaterfall, createAsyncWaterfall } from '../src/waterfall';
11
12
  import {
@@ -13,7 +14,7 @@ import {
13
14
  createAsyncWorkflow,
14
15
  createParallelWorkflow,
15
16
  } from '../src/workflow';
16
- import { main } from './fixtures/sync/core';
17
+ import { main, TestHooks, TestPlugin } from './fixtures/sync/core';
17
18
  import foo from './fixtures/sync/base/foo';
18
19
  import bar, { getBar } from './fixtures/sync/base/bar';
19
20
  import dFoo from './fixtures/sync/dynamic/foo';
@@ -391,6 +392,22 @@ describe('sync manager', () => {
391
392
  expect(count).toBe(1);
392
393
  });
393
394
 
395
+ it('should support manager clone and override pluginAPI', done => {
396
+ const myAPI = { hello: () => 1 };
397
+ const manager = createManager({}, myAPI);
398
+ const plugin = {
399
+ setup(api: typeof myAPI) {
400
+ expect(api.hello()).toEqual(2);
401
+ done();
402
+ },
403
+ };
404
+ const clonedManager = manager.clone({
405
+ hello: () => 2,
406
+ });
407
+
408
+ clonedManager.usePlugin(() => plugin).init();
409
+ });
410
+
394
411
  it('isPlugin: exclusive plugins of manager', () => {
395
412
  const manager0 = createAsyncManager();
396
413
  const manager1 = createManager();
@@ -497,11 +514,11 @@ describe('sync manager', () => {
497
514
 
498
515
  runner.pipeline({});
499
516
  await runner.asyncPipeline({});
500
- runner.waterfall({});
501
- await runner.asyncWaterfall({});
502
- runner.workflow({});
503
- await runner.asyncWorkflow({});
504
- await runner.parallelWorkflow({});
517
+ runner.waterfall();
518
+ await runner.asyncWaterfall();
519
+ runner.workflow();
520
+ await runner.asyncWorkflow();
521
+ await runner.parallelWorkflow();
505
522
 
506
523
  expect(list).toStrictEqual([
507
524
  'pipeline',
@@ -561,4 +578,100 @@ describe('sync manager', () => {
561
578
  );
562
579
  });
563
580
  });
581
+
582
+ describe('setup api', () => {
583
+ it('should allow to access api.useHookRunners by default', done => {
584
+ const manager = createManager<TestHooks>();
585
+ const plugin: TestPlugin = {
586
+ name: 'plugin',
587
+ setup: api => {
588
+ expect(api.useHookRunners).toBeTruthy();
589
+ done();
590
+ },
591
+ };
592
+ manager.usePlugin(() => plugin);
593
+ manager.init();
594
+ });
595
+
596
+ it('should allow to register extra api', done => {
597
+ type API = { foo: () => void };
598
+
599
+ const manager = createAsyncManager<TestHooks, API>(
600
+ {},
601
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
602
+ { foo: () => {} },
603
+ );
604
+
605
+ const plugin: PluginOptions<TestHooks, Setup<TestHooks, API>> = {
606
+ name: 'plugin',
607
+ setup: api => {
608
+ expect(api.foo).toBeTruthy();
609
+ done();
610
+ },
611
+ };
612
+
613
+ manager.usePlugin(() => plugin);
614
+ manager.init();
615
+ });
616
+ });
617
+
618
+ describe('usePlugins option', () => {
619
+ it('should allow to use single plugin', async () => {
620
+ const manager = createManager<TestHooks>();
621
+
622
+ const list: number[] = [];
623
+ const plugin0: TestPlugin = {
624
+ name: 'plugin0',
625
+ setup: () => {
626
+ list.push(0);
627
+ },
628
+ };
629
+
630
+ const plugin1: TestPlugin = {
631
+ name: 'plugin1',
632
+ usePlugins: [plugin0],
633
+ setup: () => {
634
+ list.push(1);
635
+ },
636
+ };
637
+
638
+ manager.usePlugin(() => plugin1);
639
+ manager.init();
640
+
641
+ expect(list).toStrictEqual([0, 1]);
642
+ });
643
+
644
+ it('should allow to use multiple plugins', async () => {
645
+ const manager = createManager<TestHooks>();
646
+
647
+ const list: number[] = [];
648
+ const plugin0: TestPlugin = {
649
+ name: 'plugin0',
650
+ setup: () => {
651
+ list.push(0);
652
+ },
653
+ };
654
+
655
+ const plugin1: TestPlugin = {
656
+ name: 'plugin1',
657
+ usePlugins: [plugin0],
658
+ setup: () => {
659
+ list.push(1);
660
+ },
661
+ };
662
+
663
+ const plugin2: TestPlugin = {
664
+ name: 'plugin2',
665
+ usePlugins: [plugin1],
666
+ setup: () => {
667
+ list.push(2);
668
+ },
669
+ };
670
+
671
+ manager.usePlugin(() => plugin2);
672
+ manager.init();
673
+
674
+ expect(list).toStrictEqual([0, 1, 2]);
675
+ });
676
+ });
564
677
  });