@modern-js/plugin 1.2.1 → 1.3.2

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 +17 -0
  2. package/dist/js/modern/manager/async.js +59 -28
  3. package/dist/js/modern/manager/index.js +2 -1
  4. package/dist/js/modern/manager/sync.js +79 -60
  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 +57 -25
  11. package/dist/js/node/manager/index.js +13 -0
  12. package/dist/js/node/manager/sync.js +84 -58
  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 +63 -34
  19. package/dist/js/treeshaking/manager/index.js +2 -1
  20. package/dist/js/treeshaking/manager/sync.js +85 -62
  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 +166 -22
  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 +158 -21
@@ -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();
@@ -410,21 +427,6 @@ describe('async manager', () => {
410
427
  expect(manager1.isPlugin('' as any)).toBeFalsy();
411
428
  });
412
429
 
413
- it('usePlugin should only add exclusive plugins of manager', async () => {
414
- const manager0 = createManager();
415
- const manager1 = createAsyncManager();
416
-
417
- let count = 0;
418
- const plugin = manager0.createPlugin(() => {
419
- count += 1;
420
- });
421
-
422
- manager1.usePlugin(plugin as any);
423
- await manager1.init();
424
-
425
- expect(count).toBe(0);
426
- });
427
-
428
430
  it('should support clear plugins', async () => {
429
431
  const manager = createAsyncManager();
430
432
 
@@ -501,11 +503,11 @@ describe('async manager', () => {
501
503
 
502
504
  runner.pipeline({});
503
505
  await runner.asyncPipeline({});
504
- runner.waterfall({});
505
- await runner.asyncWaterfall({});
506
- runner.workflow({});
507
- await runner.asyncWorkflow({});
508
- await runner.parallelWorkflow({});
506
+ runner.waterfall();
507
+ await runner.asyncWaterfall();
508
+ runner.workflow();
509
+ await runner.asyncWorkflow();
510
+ await runner.parallelWorkflow();
509
511
 
510
512
  expect(list).toStrictEqual([
511
513
  'pipeline',
@@ -591,4 +593,146 @@ describe('async manager', () => {
591
593
  );
592
594
  });
593
595
  });
596
+
597
+ describe('setup api', () => {
598
+ it('should allow to access api.useHookRunners by default', done => {
599
+ const manager = createAsyncManager<TestAsyncHooks>();
600
+ const plugin: TestAsyncPlugin = {
601
+ name: 'plugin',
602
+ setup: api => {
603
+ expect(api.useHookRunners).toBeTruthy();
604
+ done();
605
+ },
606
+ };
607
+ manager.usePlugin(plugin);
608
+ manager.init();
609
+ });
610
+
611
+ it('should allow to register extra api', done => {
612
+ type API = { foo: () => void };
613
+
614
+ const manager = createAsyncManager<TestAsyncHooks, API>(
615
+ {},
616
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
617
+ { foo: () => {} },
618
+ );
619
+
620
+ const plugin: PluginOptions<
621
+ TestAsyncHooks,
622
+ AsyncSetup<TestAsyncHooks, API>
623
+ > = {
624
+ name: 'plugin',
625
+ setup: api => {
626
+ expect(api.foo).toBeTruthy();
627
+ done();
628
+ },
629
+ };
630
+
631
+ manager.usePlugin(plugin);
632
+ manager.init();
633
+ });
634
+ });
635
+
636
+ describe('usePlugins option', () => {
637
+ it('should allow to use single plugin', async () => {
638
+ const manager = createAsyncManager<TestAsyncHooks>();
639
+
640
+ const list: number[] = [];
641
+ const plugin0: TestAsyncPlugin = {
642
+ name: 'plugin0',
643
+ setup: () => {
644
+ list.push(0);
645
+ },
646
+ };
647
+
648
+ const plugin1: TestAsyncPlugin = {
649
+ name: 'plugin1',
650
+ usePlugins: [plugin0],
651
+ setup: () => {
652
+ list.push(1);
653
+ },
654
+ };
655
+
656
+ manager.usePlugin(plugin1);
657
+
658
+ await manager.init();
659
+
660
+ expect(list).toStrictEqual([0, 1]);
661
+ });
662
+
663
+ it('should allow to use multiple plugins', async () => {
664
+ const manager = createAsyncManager<TestAsyncHooks>();
665
+
666
+ const list: number[] = [];
667
+ const plugin0: TestAsyncPlugin = {
668
+ name: 'plugin0',
669
+ setup: () => {
670
+ list.push(0);
671
+ },
672
+ };
673
+
674
+ const plugin1 = {
675
+ name: 'plugin1',
676
+ usePlugins: [plugin0],
677
+ setup: () => {
678
+ list.push(1);
679
+ },
680
+ };
681
+
682
+ const plugin2 = {
683
+ name: 'plugin2',
684
+ usePlugins: [plugin1],
685
+ setup: () => {
686
+ list.push(2);
687
+ },
688
+ };
689
+
690
+ manager.usePlugin(plugin2);
691
+
692
+ await manager.init();
693
+
694
+ expect(list).toStrictEqual([0, 1, 2]);
695
+ });
696
+
697
+ it('should allow to use plugin without setup function', async () => {
698
+ const manager = createAsyncManager<TestAsyncHooks>();
699
+
700
+ const list: number[] = [];
701
+ const plugin0: TestAsyncPlugin = {
702
+ name: 'plugin0',
703
+ setup: () => {
704
+ list.push(0);
705
+ },
706
+ };
707
+
708
+ const plugin1 = {
709
+ name: 'plugin1',
710
+ usePlugins: [plugin0],
711
+ };
712
+
713
+ manager.usePlugin(plugin1);
714
+
715
+ await manager.init();
716
+
717
+ expect(list).toStrictEqual([0]);
718
+ });
719
+
720
+ it('should allow to use function plugin', async () => {
721
+ const manager = createAsyncManager<TestAsyncHooks>();
722
+
723
+ const list: number[] = [];
724
+ const plugin0: TestAsyncPlugin = {
725
+ name: 'plugin0',
726
+ setup: () => {
727
+ list.push(0);
728
+ },
729
+ };
730
+
731
+ manager.usePlugin(() => plugin0);
732
+
733
+ await manager.init();
734
+
735
+ expect(list).toStrictEqual([0]);
736
+ });
737
+ });
594
738
  });
@@ -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();
@@ -404,21 +421,6 @@ describe('sync manager', () => {
404
421
  expect(manager1.isPlugin('' as any)).toBeFalsy();
405
422
  });
406
423
 
407
- it('usePlugin: exclusive plugins of manager', () => {
408
- const manager0 = createAsyncManager();
409
- const manager1 = createManager();
410
-
411
- let count = 0;
412
- const plugin = manager0.createPlugin(() => {
413
- count += 1;
414
- });
415
-
416
- manager1.usePlugin(plugin as any);
417
- manager1.init();
418
-
419
- expect(count).toBe(0);
420
- });
421
-
422
424
  it('should support clear plugins', () => {
423
425
  setNumber(0);
424
426
 
@@ -497,11 +499,11 @@ describe('sync manager', () => {
497
499
 
498
500
  runner.pipeline({});
499
501
  await runner.asyncPipeline({});
500
- runner.waterfall({});
501
- await runner.asyncWaterfall({});
502
- runner.workflow({});
503
- await runner.asyncWorkflow({});
504
- await runner.parallelWorkflow({});
502
+ runner.waterfall();
503
+ await runner.asyncWaterfall();
504
+ runner.workflow();
505
+ await runner.asyncWorkflow();
506
+ await runner.parallelWorkflow();
505
507
 
506
508
  expect(list).toStrictEqual([
507
509
  'pipeline',
@@ -561,4 +563,139 @@ describe('sync manager', () => {
561
563
  );
562
564
  });
563
565
  });
566
+
567
+ describe('setup api', () => {
568
+ it('should allow to access api.useHookRunners by default', done => {
569
+ const manager = createManager<TestHooks>();
570
+ const plugin: TestPlugin = {
571
+ name: 'plugin',
572
+ setup: api => {
573
+ expect(api.useHookRunners).toBeTruthy();
574
+ done();
575
+ },
576
+ };
577
+ manager.usePlugin(plugin);
578
+ manager.init();
579
+ });
580
+
581
+ it('should allow to register extra api', done => {
582
+ type API = { foo: () => void };
583
+
584
+ const manager = createAsyncManager<TestHooks, API>(
585
+ {},
586
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
587
+ { foo: () => {} },
588
+ );
589
+
590
+ const plugin: PluginOptions<TestHooks, Setup<TestHooks, API>> = {
591
+ name: 'plugin',
592
+ setup: api => {
593
+ expect(api.foo).toBeTruthy();
594
+ done();
595
+ },
596
+ };
597
+
598
+ manager.usePlugin(plugin);
599
+ manager.init();
600
+ });
601
+ });
602
+
603
+ describe('usePlugins option', () => {
604
+ it('should allow to use single plugin', async () => {
605
+ const manager = createManager<TestHooks>();
606
+
607
+ const list: number[] = [];
608
+ const plugin0: TestPlugin = {
609
+ name: 'plugin0',
610
+ setup: () => {
611
+ list.push(0);
612
+ },
613
+ };
614
+
615
+ const plugin1: TestPlugin = {
616
+ name: 'plugin1',
617
+ usePlugins: [plugin0],
618
+ setup: () => {
619
+ list.push(1);
620
+ },
621
+ };
622
+
623
+ manager.usePlugin(plugin1);
624
+ manager.init();
625
+
626
+ expect(list).toStrictEqual([0, 1]);
627
+ });
628
+
629
+ it('should allow to use multiple plugins', async () => {
630
+ const manager = createManager<TestHooks>();
631
+
632
+ const list: number[] = [];
633
+ const plugin0: TestPlugin = {
634
+ name: 'plugin0',
635
+ setup: () => {
636
+ list.push(0);
637
+ },
638
+ };
639
+
640
+ const plugin1: TestPlugin = {
641
+ name: 'plugin1',
642
+ usePlugins: [plugin0],
643
+ setup: () => {
644
+ list.push(1);
645
+ },
646
+ };
647
+
648
+ const plugin2: TestPlugin = {
649
+ name: 'plugin2',
650
+ usePlugins: [plugin1],
651
+ setup: () => {
652
+ list.push(2);
653
+ },
654
+ };
655
+
656
+ manager.usePlugin(plugin2);
657
+ manager.init();
658
+
659
+ expect(list).toStrictEqual([0, 1, 2]);
660
+ });
661
+
662
+ it('should allow to use plugin without setup function', async () => {
663
+ const manager = createManager<TestHooks>();
664
+
665
+ const list: number[] = [];
666
+ const plugin0: TestPlugin = {
667
+ name: 'plugin0',
668
+ setup: () => {
669
+ list.push(0);
670
+ },
671
+ };
672
+
673
+ const plugin1: TestPlugin = {
674
+ name: 'plugin1',
675
+ usePlugins: [plugin0],
676
+ };
677
+
678
+ manager.usePlugin(plugin1);
679
+ manager.init();
680
+
681
+ expect(list).toStrictEqual([0]);
682
+ });
683
+
684
+ it('should allow to use function plugin', async () => {
685
+ const manager = createManager<TestHooks>();
686
+
687
+ const list: number[] = [];
688
+ const plugin0: TestPlugin = {
689
+ name: 'plugin0',
690
+ setup: () => {
691
+ list.push(0);
692
+ },
693
+ };
694
+
695
+ manager.usePlugin(() => plugin0);
696
+ manager.init();
697
+
698
+ expect(list).toStrictEqual([0]);
699
+ });
700
+ });
564
701
  });