@modern-js/core 1.4.5 → 1.6.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.
@@ -1,111 +1,22 @@
1
1
  import { INTERNAL_PLUGINS } from '@modern-js/utils';
2
- import { ParallelWorkflow, AsyncWorkflow, Progresses2Runners, AsyncWaterfall } from '@modern-js/plugin';
3
2
  import type { Hooks } from '@modern-js/types';
4
3
  import { ErrorObject } from 'ajv';
5
- import { Command } from './utils/commander';
6
- import { TransformPlugin } from './loadPlugins';
7
- import { AppContext, ConfigContext, IAppContext, initAppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from './context';
8
- import { NormalizedConfig } from './config/mergeConfig';
4
+ import { IAppContext, initAppContext } from './context';
5
+ import type { NormalizedConfig } from './config/mergeConfig';
9
6
  export type { Hooks };
10
7
  export * from './config';
11
8
  export * from '@modern-js/plugin';
12
9
  export * from '@modern-js/plugin/node';
13
- export declare type HooksRunner = Progresses2Runners<{
14
- config: ParallelWorkflow<void>;
15
- resolvedConfig: AsyncWaterfall<{
16
- resolved: NormalizedConfig;
17
- }>;
18
- validateSchema: ParallelWorkflow<void>;
19
- prepare: AsyncWorkflow<void, void>;
20
- commands: AsyncWorkflow<{
21
- program: Command;
22
- }, void>;
23
- watchFiles: ParallelWorkflow<void>;
24
- fileChange: AsyncWorkflow<{
25
- filename: string;
26
- eventType: 'add' | 'change' | 'unlink';
27
- }, void>;
28
- beforeExit: AsyncWorkflow<void, void>;
29
- }>;
30
- export declare const manager: import("@modern-js/plugin").AsyncManager<Hooks, {
31
- config: ParallelWorkflow<void, unknown>;
32
- resolvedConfig: AsyncWaterfall<{
33
- resolved: NormalizedConfig;
34
- }>;
35
- validateSchema: ParallelWorkflow<void, unknown>;
36
- prepare: AsyncWorkflow<void, void>;
37
- commands: AsyncWorkflow<{
38
- program: Command;
39
- }, void>;
40
- watchFiles: ParallelWorkflow<void, unknown>;
41
- fileChange: AsyncWorkflow<{
42
- filename: string;
43
- eventType: 'add' | 'change' | 'unlink';
44
- }, void>;
45
- beforeExit: AsyncWorkflow<void, void>;
46
- beforeRestart: AsyncWorkflow<void, void>;
47
- }>;
48
- export declare const createPlugin: (initializer: import("@modern-js/plugin").AsyncInitializer<Partial<import("@modern-js/plugin").Progresses2Threads<{
49
- config: ParallelWorkflow<void, unknown>;
50
- resolvedConfig: AsyncWaterfall<{
51
- resolved: NormalizedConfig;
52
- }>;
53
- validateSchema: ParallelWorkflow<void, unknown>;
54
- prepare: AsyncWorkflow<void, void>;
55
- commands: AsyncWorkflow<{
56
- program: Command;
57
- }, void>;
58
- watchFiles: ParallelWorkflow<void, unknown>;
59
- fileChange: AsyncWorkflow<{
60
- filename: string;
61
- eventType: 'add' | 'change' | 'unlink';
62
- }, void>;
63
- beforeExit: AsyncWorkflow<void, void>;
64
- beforeRestart: AsyncWorkflow<void, void>;
65
- } & import("@modern-js/plugin").ClearDraftProgress<Hooks>>>>, options?: import("@modern-js/plugin").PluginOptions | undefined) => import("@modern-js/plugin").AsyncPlugin<Partial<import("@modern-js/plugin").Progresses2Threads<{
66
- config: ParallelWorkflow<void, unknown>;
67
- resolvedConfig: AsyncWaterfall<{
68
- resolved: NormalizedConfig;
69
- }>;
70
- validateSchema: ParallelWorkflow<void, unknown>;
71
- prepare: AsyncWorkflow<void, void>;
72
- commands: AsyncWorkflow<{
73
- program: Command;
74
- }, void>;
75
- watchFiles: ParallelWorkflow<void, unknown>;
76
- fileChange: AsyncWorkflow<{
77
- filename: string;
78
- eventType: 'add' | 'change' | 'unlink';
79
- }, void>;
80
- beforeExit: AsyncWorkflow<void, void>;
81
- beforeRestart: AsyncWorkflow<void, void>;
82
- } & import("@modern-js/plugin").ClearDraftProgress<Hooks>>>>, registerHook: (newShape: Partial<Hooks>) => void, mountHook: () => Progresses2Runners<{
83
- config: ParallelWorkflow<void, unknown>;
84
- resolvedConfig: AsyncWaterfall<{
85
- resolved: NormalizedConfig;
86
- }>;
87
- validateSchema: ParallelWorkflow<void, unknown>;
88
- prepare: AsyncWorkflow<void, void>;
89
- commands: AsyncWorkflow<{
90
- program: Command;
91
- }, void>;
92
- watchFiles: ParallelWorkflow<void, unknown>;
93
- fileChange: AsyncWorkflow<{
94
- filename: string;
95
- eventType: 'add' | 'change' | 'unlink';
96
- }, void>;
97
- beforeExit: AsyncWorkflow<void, void>;
98
- beforeRestart: AsyncWorkflow<void, void>;
99
- } & import("@modern-js/plugin").ClearDraftProgress<Hooks>>;
100
- export declare const usePlugins: (plugins: string[]) => void;
101
- export { AppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext, ConfigContext };
10
+ export { manager, mountHook, usePlugins, createPlugin, registerHook } from './manager';
11
+ export type { CliHooks, CliPlugin, CliHookCallbacks } from './manager';
12
+ export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from './pluginAPI';
13
+ export type { PluginAPI } from './pluginAPI';
102
14
  export type { NormalizedConfig, IAppContext };
103
15
  declare const initAppDir: (cwd?: string | undefined) => Promise<string>;
104
16
  export interface CoreOptions {
105
17
  configFile?: string;
106
18
  packageJsonConfig?: string;
107
19
  plugins?: typeof INTERNAL_PLUGINS;
108
- transformPlugin?: TransformPlugin;
109
20
  onSchemaError?: (error: ErrorObject) => void;
110
21
  options?: {
111
22
  metaName?: string;
@@ -1,4 +1,4 @@
1
1
  import chokidar from 'chokidar';
2
2
  import { LoadedConfig } from './config';
3
- import { HooksRunner } from '.';
3
+ import { HooksRunner } from './manager';
4
4
  export declare const initWatcher: (loaded: LoadedConfig, appDirectory: string, configDir: string | undefined, hooksRunner: HooksRunner, argv: string[]) => Promise<chokidar.FSWatcher | undefined>;
@@ -1,30 +1,41 @@
1
1
  import { INTERNAL_PLUGINS } from '@modern-js/utils';
2
2
  import type { UserConfig } from './config';
3
- declare type Plugin = string | [string, any];
3
+ import { CliPlugin } from './manager';
4
+ declare type PluginItem = string | [string, any];
4
5
  export declare type LoadedPlugin = {
5
- cli?: any;
6
- cliPkg?: string;
7
- server?: any;
6
+ cli?: CliPlugin;
7
+ server?: string;
8
8
  serverPkg?: string;
9
9
  };
10
- export declare type PluginConfigItem = {
11
- cli?: Plugin;
12
- server?: Plugin;
13
- } | Plugin;
14
- export declare type PluginConfig = Array<PluginConfigItem>;
15
- export declare type TransformPlugin = (plugin: PluginConfig, resolvedConfig: UserConfig, pluginOptions?: any) => PluginConfig;
16
- export declare function getAppPlugins(appDirectory: string, pluginConfig: PluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): PluginConfigItem[];
10
+ /**
11
+ * @deprecated
12
+ * Using NewPluginConfig insteand.
13
+ */
14
+
15
+ declare type OldPluginConfig = Array<PluginItem | {
16
+ cli?: PluginItem;
17
+ server?: PluginItem;
18
+ }>;
19
+ declare type NewPluginConfig = CliPlugin[] | {
20
+ cli?: CliPlugin[];
21
+ /** Custom server plugin is not supported yet. */
22
+
23
+ server?: never;
24
+ };
25
+ export declare type PluginConfig = OldPluginConfig | NewPluginConfig;
26
+ export declare function getAppPlugins(appDirectory: string, oldPluginConfig: OldPluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): (PluginItem | {
27
+ cli?: PluginItem | undefined;
28
+ server?: PluginItem | undefined;
29
+ })[];
17
30
  /**
18
31
  * Load internal plugins which in @modern-js scope and user's custom plugins.
19
32
  * @param appDirectory - Application root directory.
20
33
  * @param userConfig - Resolved user config.
21
34
  * @param options.internalPlugins - Internal plugins.
22
- * @param options.transformPlugin - transform plugin before using it.
23
35
  * @returns Plugin Objects has been required.
24
36
  */
25
37
 
26
38
  export declare const loadPlugins: (appDirectory: string, userConfig: UserConfig, options?: {
27
39
  internalPlugins?: typeof INTERNAL_PLUGINS;
28
- transformPlugin?: TransformPlugin;
29
40
  }) => LoadedPlugin[];
30
41
  export {};
@@ -0,0 +1,74 @@
1
+ import { ToThreads, ToRunners, AsyncSetup, PluginOptions, AsyncWorkflow, AsyncWaterfall, ParallelWorkflow } from '@modern-js/plugin';
2
+ import type { Hooks } from '@modern-js/types';
3
+ import type { Command } from './utils/commander';
4
+ import type { NormalizedConfig } from './config/mergeConfig';
5
+ import { pluginAPI } from './pluginAPI';
6
+ export declare type HooksRunner = ToRunners<{
7
+ config: ParallelWorkflow<void>;
8
+ resolvedConfig: AsyncWaterfall<{
9
+ resolved: NormalizedConfig;
10
+ }>;
11
+ validateSchema: ParallelWorkflow<void>;
12
+ prepare: AsyncWorkflow<void, void>;
13
+ commands: AsyncWorkflow<{
14
+ program: Command;
15
+ }, void>;
16
+ watchFiles: ParallelWorkflow<void>;
17
+ fileChange: AsyncWorkflow<{
18
+ filename: string;
19
+ eventType: 'add' | 'change' | 'unlink';
20
+ }, void>;
21
+ beforeExit: AsyncWorkflow<void, void>;
22
+ beforeRestart: AsyncWorkflow<void, void>;
23
+ }>;
24
+ declare const baseHooks: {
25
+ config: ParallelWorkflow<void, unknown>;
26
+ resolvedConfig: AsyncWaterfall<{
27
+ resolved: NormalizedConfig;
28
+ }>;
29
+ validateSchema: ParallelWorkflow<void, unknown>;
30
+ prepare: AsyncWorkflow<void, void>;
31
+ commands: AsyncWorkflow<{
32
+ program: Command;
33
+ }, void>;
34
+ watchFiles: ParallelWorkflow<void, unknown>;
35
+ fileChange: AsyncWorkflow<{
36
+ filename: string;
37
+ eventType: 'add' | 'change' | 'unlink';
38
+ }, void>;
39
+ beforeExit: AsyncWorkflow<void, void>;
40
+ beforeRestart: AsyncWorkflow<void, void>;
41
+ };
42
+ /** All hooks of cli plugin. */
43
+
44
+ export declare type CliHooks = typeof baseHooks & Hooks;
45
+ /** All hook callbacks of cli plugin. */
46
+
47
+ export declare type CliHookCallbacks = ToThreads<CliHooks>;
48
+ export declare const manager: import("@modern-js/plugin").AsyncManager<CliHooks, {
49
+ setAppContext: (value: import("@modern-js/types").IAppContext) => void;
50
+ useAppContext: () => import("@modern-js/types").IAppContext;
51
+ useConfigContext: () => import("./config").UserConfig;
52
+ useResolvedConfigContext: () => NormalizedConfig;
53
+ }>;
54
+ /** Plugin options of a cli plugin. */
55
+
56
+ export declare type CliPlugin = PluginOptions<CliHooks, AsyncSetup<CliHooks, typeof pluginAPI>>;
57
+ export declare const createPlugin: (setup?: AsyncSetup<CliHooks, {
58
+ setAppContext: (value: import("@modern-js/types").IAppContext) => void;
59
+ useAppContext: () => import("@modern-js/types").IAppContext;
60
+ useConfigContext: () => import("./config").UserConfig;
61
+ useResolvedConfigContext: () => NormalizedConfig;
62
+ }> | undefined, options?: PluginOptions<CliHooks, AsyncSetup<CliHooks, {
63
+ setAppContext: (value: import("@modern-js/types").IAppContext) => void;
64
+ useAppContext: () => import("@modern-js/types").IAppContext;
65
+ useConfigContext: () => import("./config").UserConfig;
66
+ useResolvedConfigContext: () => NormalizedConfig;
67
+ }>> | undefined) => import("@modern-js/plugin").AsyncPlugin<CliHooks, {
68
+ setAppContext: (value: import("@modern-js/types").IAppContext) => void;
69
+ useAppContext: () => import("@modern-js/types").IAppContext;
70
+ useConfigContext: () => import("./config").UserConfig;
71
+ useResolvedConfigContext: () => NormalizedConfig;
72
+ }>, registerHook: (newHooks: Partial<CliHooks>) => void, mountHook: () => ToRunners<CliHooks>;
73
+ export declare const usePlugins: (plugins: string[]) => void;
74
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { CommonAPI } from '@modern-js/plugin';
2
+ import type { CliHooks } from './manager';
3
+ import { AppContext, ConfigContext, useAppContext, useConfigContext, ResolvedConfigContext, useResolvedConfigContext } from './context';
4
+ export declare const pluginAPI: {
5
+ setAppContext: (value: import("@modern-js/types/cli").IAppContext) => void;
6
+ useAppContext: () => import("@modern-js/types/cli").IAppContext;
7
+ useConfigContext: () => import("./config").UserConfig;
8
+ useResolvedConfigContext: () => import(".").NormalizedConfig;
9
+ };
10
+ /** all apis for cli plugin */
11
+
12
+ export declare type PluginAPI = typeof pluginAPI & CommonAPI<CliHooks>;
13
+ export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.5",
14
+ "version": "1.6.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -43,8 +43,8 @@
43
43
  "@babel/code-frame": "^7.14.5",
44
44
  "@babel/runtime": "^7",
45
45
  "@modern-js/load-config": "^1.2.2",
46
- "@modern-js/plugin": "^1.2.1",
47
- "@modern-js/utils": "^1.3.3",
46
+ "@modern-js/plugin": "^1.3.2",
47
+ "@modern-js/utils": "^1.3.6",
48
48
  "address": "^1.1.2",
49
49
  "ajv": "^8.6.2",
50
50
  "ajv-keywords": "^5.0.0",
@@ -61,7 +61,7 @@
61
61
  "devDependencies": {
62
62
  "btsm": "2.2.2",
63
63
  "@types/babel__code-frame": "^7.0.3",
64
- "@modern-js/types": "^1.3.4",
64
+ "@modern-js/types": "^1.3.5",
65
65
  "@types/jest": "^26",
66
66
  "@types/lodash.clonedeep": "^4.5.6",
67
67
  "@types/lodash.mergewith": "^4.6.6",
@@ -82,8 +82,7 @@
82
82
  },
83
83
  "publishConfig": {
84
84
  "registry": "https://registry.npmjs.org/",
85
- "access": "public",
86
- "types": "./dist/types/index.d.ts"
85
+ "access": "public"
87
86
  },
88
87
  "scripts": {
89
88
  "new": "modern new",
@@ -8,7 +8,7 @@ const mockAppDirectory = path.join(__dirname, './fixtures/index-test');
8
8
  const mockConfigDir = './config';
9
9
  const mockSrcDirectory = path.join(mockAppDirectory, './src');
10
10
 
11
- describe('initWatcher', () => {
11
+ describe.skip('initWatcher', () => {
12
12
  afterAll(() => {
13
13
  const file = path.join(mockSrcDirectory, './index.ts');
14
14
  if (fs.existsSync(file)) {
@@ -1,4 +1,5 @@
1
1
  import path from 'path';
2
+ import { CliPlugin } from '../src';
2
3
  import { loadPlugins, getAppPlugins } from '../src/loadPlugins';
3
4
 
4
5
  describe('load plugins', () => {
@@ -33,14 +34,10 @@ describe('load plugins', () => {
33
34
  {
34
35
  cli: {
35
36
  name: 'a',
36
- pluginPath: path.join(fixture, './test-plugin-a.js'),
37
37
  },
38
- cliPkg: path.join(fixture, './test-plugin-a.js'),
39
38
  },
40
39
  {
41
- server: {
42
- pluginPath: path.join(fixture, './test-plugin-b.js'),
43
- },
40
+ server: './test-plugin-b',
44
41
  serverPkg: './test-plugin-b',
45
42
  },
46
43
  ]);
@@ -56,22 +53,8 @@ describe('load plugins', () => {
56
53
  plugins: [{ cli: ['./test-plugin-c', 'c'] }, ['./test-plugin-c', 'c2']],
57
54
  });
58
55
 
59
- expect(plugins).toEqual([
60
- {
61
- cli: {
62
- name: 'c',
63
- pluginPath: path.join(fixture, './test-plugin-c.js'),
64
- },
65
- cliPkg: './test-plugin-c',
66
- },
67
- {
68
- cli: {
69
- name: 'c2',
70
- pluginPath: path.join(fixture, './test-plugin-c.js'),
71
- },
72
- cliPkg: './test-plugin-c',
73
- },
74
- ]);
56
+ expect(plugins[0].cli!.name).toEqual('c');
57
+ expect(plugins[1].cli!.name).toEqual('c2');
75
58
  });
76
59
 
77
60
  test('should load user string plugin successfully', () => {
@@ -88,33 +71,11 @@ describe('load plugins', () => {
88
71
  {
89
72
  cli: {
90
73
  name: 'a',
91
- pluginPath: path.join(fixture, './test-plugin-a.js'),
92
74
  },
93
- cliPkg: path.join(fixture, './test-plugin-a.js'),
94
75
  },
95
76
  ]);
96
77
  });
97
78
 
98
- test('should call transformPlugin', () => {
99
- const fixture = path.resolve(
100
- __dirname,
101
- './fixtures/load-plugin/user-plugins',
102
- );
103
-
104
- const options = {
105
- transformPlugin: jest.fn(),
106
- };
107
- options.transformPlugin.mockImplementation((plugins, _) => plugins);
108
-
109
- loadPlugins(
110
- fixture,
111
- { plugins: [{ cli: path.join(fixture, './test-plugin-a.js') }] },
112
- options,
113
- );
114
-
115
- expect(options.transformPlugin).toHaveBeenCalled();
116
- });
117
-
118
79
  test(`should throw error when plugin not found `, () => {
119
80
  const fixture = path.resolve(__dirname, './fixtures/load-plugin/not-found');
120
81
 
@@ -124,4 +85,32 @@ describe('load plugins', () => {
124
85
  });
125
86
  }).toThrowError(/^Can not find plugin /);
126
87
  });
88
+
89
+ test(`should load new plugin array correctly`, () => {
90
+ const appDirectory = path.resolve(
91
+ __dirname,
92
+ './fixtures/load-plugin/user-plugins',
93
+ );
94
+ const plugin = (): CliPlugin => ({
95
+ name: 'foo',
96
+ });
97
+ const userConfig = { plugins: [plugin()] };
98
+ const loadedPlugins = loadPlugins(appDirectory, userConfig);
99
+
100
+ expect(loadedPlugins[0].cli?.name).toEqual('foo');
101
+ });
102
+
103
+ test(`should load new plugin object correctly`, () => {
104
+ const appDirectory = path.resolve(
105
+ __dirname,
106
+ './fixtures/load-plugin/user-plugins',
107
+ );
108
+ const plugin = (): CliPlugin => ({
109
+ name: 'foo',
110
+ });
111
+ const userConfig = { plugins: { cli: [plugin()] } };
112
+ const loadedPlugins = loadPlugins(appDirectory, userConfig);
113
+
114
+ expect(loadedPlugins[0].cli?.name).toEqual('foo');
115
+ });
127
116
  });
@@ -0,0 +1,19 @@
1
+ import { CliPlugin, manager } from '../src';
2
+
3
+ describe('pluginAPI', () => {
4
+ it('api.setAppContext', done => {
5
+ const plugin = (): CliPlugin => ({
6
+ setup(api) {
7
+ api.setAppContext({
8
+ ...api.useAppContext(),
9
+ packageName: 'foo',
10
+ });
11
+
12
+ expect(api.useAppContext().packageName).toEqual('foo');
13
+ done();
14
+ },
15
+ });
16
+
17
+ manager.clone().usePlugin(plugin).init();
18
+ });
19
+ });