@modern-js/core 1.4.4 → 1.5.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.
- package/.eslintrc.js +10 -0
- package/CHANGELOG.md +31 -0
- package/dist/js/modern/config/mergeConfig.js +8 -6
- package/dist/js/modern/config/schema/deploy.js +0 -3
- package/dist/js/modern/config/schema/server.js +2 -2
- package/dist/js/modern/context.js +18 -0
- package/dist/js/modern/index.js +9 -28
- package/dist/js/modern/loadPlugins.js +7 -12
- package/dist/js/modern/manager.js +28 -0
- package/dist/js/modern/pluginAPI.js +11 -0
- package/dist/js/node/config/mergeConfig.js +8 -6
- package/dist/js/node/config/schema/deploy.js +0 -3
- package/dist/js/node/config/schema/server.js +2 -2
- package/dist/js/node/context.js +22 -1
- package/dist/js/node/index.js +78 -67
- package/dist/js/node/loadPlugins.js +8 -12
- package/dist/js/node/manager.js +45 -0
- package/dist/js/node/pluginAPI.js +54 -0
- package/dist/types/config/index.d.ts +3 -3
- package/dist/types/config/schema/deploy.d.ts +0 -3
- package/dist/types/config/schema/index.d.ts +2 -5
- package/dist/types/config/schema/server.d.ts +2 -2
- package/dist/types/context.d.ts +18 -0
- package/dist/types/index.d.ts +6 -95
- package/dist/types/initWatcher.d.ts +1 -1
- package/dist/types/loadPlugins.d.ts +1 -4
- package/dist/types/manager.d.ts +74 -0
- package/dist/types/pluginAPI.d.ts +13 -0
- package/package.json +4 -4
- package/tests/loadPlugin.test.ts +4 -33
- package/tests/mergeConfig.test.ts +22 -3
- package/tests/pluginAPI.test.ts +19 -0
- package/tests/schema.test.ts +2 -4
|
@@ -8,6 +8,8 @@ exports.loadPlugins = void 0;
|
|
|
8
8
|
|
|
9
9
|
var _utils = require("@modern-js/utils");
|
|
10
10
|
|
|
11
|
+
var _manager = require("./manager");
|
|
12
|
+
|
|
11
13
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
12
14
|
|
|
13
15
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
@@ -60,15 +62,13 @@ function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
|
|
|
60
62
|
* @param appDirectory - Application root directory.
|
|
61
63
|
* @param userConfig - Resolved user config.
|
|
62
64
|
* @param options.internalPlugins - Internal plugins.
|
|
63
|
-
* @param options.transformPlugin - transform plugin before using it.
|
|
64
65
|
* @returns Plugin Objects has been required.
|
|
65
66
|
*/
|
|
66
67
|
|
|
67
68
|
|
|
68
69
|
const loadPlugins = (appDirectory, userConfig, options = {}) => {
|
|
69
70
|
const {
|
|
70
|
-
internalPlugins
|
|
71
|
-
transformPlugin
|
|
71
|
+
internalPlugins
|
|
72
72
|
} = options;
|
|
73
73
|
|
|
74
74
|
const resolvePlugin = p => {
|
|
@@ -77,10 +77,9 @@ const loadPlugins = (appDirectory, userConfig, options = {}) => {
|
|
|
77
77
|
let module = (0, _utils.compatRequire)(path);
|
|
78
78
|
const pluginOptions = Array.isArray(p) ? p[1] : undefined;
|
|
79
79
|
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
module = typeof module === 'function' ? module(pluginOptions) : module;
|
|
80
|
+
if (typeof module === 'function') {
|
|
81
|
+
const plugin = module(pluginOptions);
|
|
82
|
+
module = (0, _manager.createPlugin)(plugin.setup, plugin);
|
|
84
83
|
}
|
|
85
84
|
|
|
86
85
|
return {
|
|
@@ -92,7 +91,7 @@ const loadPlugins = (appDirectory, userConfig, options = {}) => {
|
|
|
92
91
|
|
|
93
92
|
const plugins = getAppPlugins(appDirectory, userConfig.plugins || [], internalPlugins);
|
|
94
93
|
return plugins.map(plugin => {
|
|
95
|
-
const _plugin = typeof plugin === 'string' ? {
|
|
94
|
+
const _plugin = typeof plugin === 'string' || Array.isArray(plugin) ? {
|
|
96
95
|
cli: plugin
|
|
97
96
|
} : plugin;
|
|
98
97
|
|
|
@@ -116,10 +115,7 @@ const loadPlugins = (appDirectory, userConfig, options = {}) => {
|
|
|
116
115
|
|
|
117
116
|
|
|
118
117
|
if (server && typeof server === 'string') {
|
|
119
|
-
|
|
120
|
-
loadedPlugin.server = {
|
|
121
|
-
pluginPath: path
|
|
122
|
-
};
|
|
118
|
+
loadedPlugin.server = server;
|
|
123
119
|
loadedPlugin.serverPkg = server;
|
|
124
120
|
}
|
|
125
121
|
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.usePlugins = exports.registerHook = exports.mountHook = exports.manager = exports.createPlugin = void 0;
|
|
7
|
+
|
|
8
|
+
var _plugin = require("@modern-js/plugin");
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
var _pluginAPI = require("./pluginAPI");
|
|
13
|
+
|
|
14
|
+
const baseHooks = {
|
|
15
|
+
config: (0, _plugin.createParallelWorkflow)(),
|
|
16
|
+
resolvedConfig: (0, _plugin.createAsyncWaterfall)(),
|
|
17
|
+
validateSchema: (0, _plugin.createParallelWorkflow)(),
|
|
18
|
+
prepare: (0, _plugin.createAsyncWorkflow)(),
|
|
19
|
+
commands: (0, _plugin.createAsyncWorkflow)(),
|
|
20
|
+
watchFiles: (0, _plugin.createParallelWorkflow)(),
|
|
21
|
+
fileChange: (0, _plugin.createAsyncWorkflow)(),
|
|
22
|
+
beforeExit: (0, _plugin.createAsyncWorkflow)(),
|
|
23
|
+
beforeRestart: (0, _plugin.createAsyncWorkflow)()
|
|
24
|
+
};
|
|
25
|
+
/** All hooks of cli plugin. */
|
|
26
|
+
|
|
27
|
+
const manager = (0, _plugin.createAsyncManager)(baseHooks, _pluginAPI.pluginAPI);
|
|
28
|
+
/** Plugin options of a cli plugin. */
|
|
29
|
+
|
|
30
|
+
exports.manager = manager;
|
|
31
|
+
const {
|
|
32
|
+
createPlugin,
|
|
33
|
+
registerHook,
|
|
34
|
+
useRunner: mountHook
|
|
35
|
+
} = manager;
|
|
36
|
+
exports.mountHook = mountHook;
|
|
37
|
+
exports.registerHook = registerHook;
|
|
38
|
+
exports.createPlugin = createPlugin;
|
|
39
|
+
|
|
40
|
+
const usePlugins = plugins => plugins.forEach(pluginPath => {
|
|
41
|
+
const module = (0, _utils.compatRequire)(require.resolve(pluginPath));
|
|
42
|
+
manager.usePlugin(module);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
exports.usePlugins = usePlugins;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "AppContext", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _context.AppContext;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "ConfigContext", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _context.ConfigContext;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "ResolvedConfigContext", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _context.ResolvedConfigContext;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
exports.pluginAPI = void 0;
|
|
25
|
+
Object.defineProperty(exports, "useAppContext", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _context.useAppContext;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "useConfigContext", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () {
|
|
34
|
+
return _context.useConfigContext;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(exports, "useResolvedConfigContext", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return _context.useResolvedConfigContext;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
var _context = require("./context");
|
|
45
|
+
|
|
46
|
+
const pluginAPI = {
|
|
47
|
+
setAppContext: _context.setAppContext,
|
|
48
|
+
useAppContext: _context.useAppContext,
|
|
49
|
+
useConfigContext: _context.useConfigContext,
|
|
50
|
+
useResolvedConfigContext: _context.useResolvedConfigContext
|
|
51
|
+
};
|
|
52
|
+
/** all apis for cli plugin */
|
|
53
|
+
|
|
54
|
+
exports.pluginAPI = pluginAPI;
|
|
@@ -65,7 +65,7 @@ interface OutputConfig {
|
|
|
65
65
|
enableTsLoader?: boolean;
|
|
66
66
|
}
|
|
67
67
|
interface ServerConfig {
|
|
68
|
-
routes?: Record<string, string | {
|
|
68
|
+
routes?: Record<string, string | string[] | {
|
|
69
69
|
route: string | string[];
|
|
70
70
|
disableSpa?: boolean;
|
|
71
71
|
}>;
|
|
@@ -76,8 +76,8 @@ interface ServerConfig {
|
|
|
76
76
|
ssrByEntries?: Record<string, boolean | Record<string, unknown>>;
|
|
77
77
|
baseUrl?: string | Array<string>;
|
|
78
78
|
port?: number;
|
|
79
|
-
logger?: Record<string, any>;
|
|
80
|
-
metrics?: Record<string, any>;
|
|
79
|
+
logger?: boolean | Record<string, any>;
|
|
80
|
+
metrics?: boolean | Record<string, any>;
|
|
81
81
|
enableMicroFrontendDebug?: boolean;
|
|
82
82
|
}
|
|
83
83
|
interface DevConfig {
|
|
@@ -380,10 +380,10 @@ export declare const patchSchema: (pluginSchemas: Array<PluginValidateSchema | P
|
|
|
380
380
|
instanceof: string;
|
|
381
381
|
};
|
|
382
382
|
logger: {
|
|
383
|
-
type: string;
|
|
383
|
+
type: string[];
|
|
384
384
|
};
|
|
385
385
|
metrics: {
|
|
386
|
-
type: string;
|
|
386
|
+
type: string[];
|
|
387
387
|
};
|
|
388
388
|
proxy: {
|
|
389
389
|
type: string;
|
|
@@ -396,9 +396,6 @@ export declare const patchSchema: (pluginSchemas: Array<PluginValidateSchema | P
|
|
|
396
396
|
deploy: {
|
|
397
397
|
type: string;
|
|
398
398
|
properties: {
|
|
399
|
-
microFrontend: {
|
|
400
|
-
type: string[];
|
|
401
|
-
};
|
|
402
399
|
domain: {
|
|
403
400
|
type: string[];
|
|
404
401
|
};
|
package/dist/types/context.d.ts
CHANGED
|
@@ -6,8 +6,26 @@ export type { IAppContext };
|
|
|
6
6
|
export declare const AppContext: import("@modern-js/plugin").Context<IAppContext>;
|
|
7
7
|
export declare const ConfigContext: import("@modern-js/plugin").Context<UserConfig>;
|
|
8
8
|
export declare const ResolvedConfigContext: import("@modern-js/plugin").Context<NormalizedConfig>;
|
|
9
|
+
/**
|
|
10
|
+
* Set app context.
|
|
11
|
+
* @param value new app context. It will override previous app context.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export declare const setAppContext: (value: IAppContext) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Get app context, including directories, plugins and some static infos.
|
|
17
|
+
*/
|
|
18
|
+
|
|
9
19
|
export declare const useAppContext: () => IAppContext;
|
|
20
|
+
/**
|
|
21
|
+
* Get original content of user config.
|
|
22
|
+
*/
|
|
23
|
+
|
|
10
24
|
export declare const useConfigContext: () => UserConfig;
|
|
25
|
+
/**
|
|
26
|
+
* Get normalized content of user config.
|
|
27
|
+
*/
|
|
28
|
+
|
|
11
29
|
export declare const useResolvedConfigContext: () => NormalizedConfig;
|
|
12
30
|
export declare const initAppContext: (appDirectory: string, plugins: Array<LoadedPlugin>, configFile: string | false, options?: {
|
|
13
31
|
metaName?: string | undefined;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -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 {
|
|
6
|
-
import {
|
|
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
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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>;
|
|
@@ -10,21 +10,18 @@ export declare type LoadedPlugin = {
|
|
|
10
10
|
export declare type PluginConfigItem = {
|
|
11
11
|
cli?: Plugin;
|
|
12
12
|
server?: Plugin;
|
|
13
|
-
} |
|
|
13
|
+
} | Plugin;
|
|
14
14
|
export declare type PluginConfig = Array<PluginConfigItem>;
|
|
15
|
-
export declare type TransformPlugin = (plugin: PluginConfig, resolvedConfig: UserConfig, pluginOptions?: any) => PluginConfig;
|
|
16
15
|
export declare function getAppPlugins(appDirectory: string, pluginConfig: PluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): PluginConfigItem[];
|
|
17
16
|
/**
|
|
18
17
|
* Load internal plugins which in @modern-js scope and user's custom plugins.
|
|
19
18
|
* @param appDirectory - Application root directory.
|
|
20
19
|
* @param userConfig - Resolved user config.
|
|
21
20
|
* @param options.internalPlugins - Internal plugins.
|
|
22
|
-
* @param options.transformPlugin - transform plugin before using it.
|
|
23
21
|
* @returns Plugin Objects has been required.
|
|
24
22
|
*/
|
|
25
23
|
|
|
26
24
|
export declare const loadPlugins: (appDirectory: string, userConfig: UserConfig, options?: {
|
|
27
25
|
internalPlugins?: typeof INTERNAL_PLUGINS;
|
|
28
|
-
transformPlugin?: TransformPlugin;
|
|
29
26
|
}) => LoadedPlugin[];
|
|
30
27
|
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.
|
|
14
|
+
"version": "1.5.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.
|
|
47
|
-
"@modern-js/utils": "^1.3.
|
|
46
|
+
"@modern-js/plugin": "^1.3.0",
|
|
47
|
+
"@modern-js/utils": "^1.3.5",
|
|
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.
|
|
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",
|
package/tests/loadPlugin.test.ts
CHANGED
|
@@ -38,9 +38,7 @@ describe('load plugins', () => {
|
|
|
38
38
|
cliPkg: path.join(fixture, './test-plugin-a.js'),
|
|
39
39
|
},
|
|
40
40
|
{
|
|
41
|
-
server:
|
|
42
|
-
pluginPath: path.join(fixture, './test-plugin-b.js'),
|
|
43
|
-
},
|
|
41
|
+
server: './test-plugin-b',
|
|
44
42
|
serverPkg: './test-plugin-b',
|
|
45
43
|
},
|
|
46
44
|
]);
|
|
@@ -53,18 +51,11 @@ describe('load plugins', () => {
|
|
|
53
51
|
);
|
|
54
52
|
|
|
55
53
|
const plugins = loadPlugins(fixture, {
|
|
56
|
-
plugins: [{ cli: ['./test-plugin-c', 'c'] }],
|
|
54
|
+
plugins: [{ cli: ['./test-plugin-c', 'c'] }, ['./test-plugin-c', 'c2']],
|
|
57
55
|
});
|
|
58
56
|
|
|
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
|
-
]);
|
|
57
|
+
expect(plugins[0].cli.name).toEqual('c');
|
|
58
|
+
expect(plugins[1].cli.name).toEqual('c2');
|
|
68
59
|
});
|
|
69
60
|
|
|
70
61
|
test('should load user string plugin successfully', () => {
|
|
@@ -88,26 +79,6 @@ describe('load plugins', () => {
|
|
|
88
79
|
]);
|
|
89
80
|
});
|
|
90
81
|
|
|
91
|
-
test('should call transformPlugin', () => {
|
|
92
|
-
const fixture = path.resolve(
|
|
93
|
-
__dirname,
|
|
94
|
-
'./fixtures/load-plugin/user-plugins',
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
const options = {
|
|
98
|
-
transformPlugin: jest.fn(),
|
|
99
|
-
};
|
|
100
|
-
options.transformPlugin.mockImplementation((plugins, _) => plugins);
|
|
101
|
-
|
|
102
|
-
loadPlugins(
|
|
103
|
-
fixture,
|
|
104
|
-
{ plugins: [{ cli: path.join(fixture, './test-plugin-a.js') }] },
|
|
105
|
-
options,
|
|
106
|
-
);
|
|
107
|
-
|
|
108
|
-
expect(options.transformPlugin).toHaveBeenCalled();
|
|
109
|
-
});
|
|
110
|
-
|
|
111
82
|
test(`should throw error when plugin not found `, () => {
|
|
112
83
|
const fixture = path.resolve(__dirname, './fixtures/load-plugin/not-found');
|
|
113
84
|
|
|
@@ -19,7 +19,7 @@ describe('load plugins', () => {
|
|
|
19
19
|
});
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
-
test(`should set value when property value is undefined `, () => {
|
|
22
|
+
test(`should set value when property value is not undefined `, () => {
|
|
23
23
|
expect(
|
|
24
24
|
mergeConfig([
|
|
25
25
|
{ source: { entries: { app: './App.tsx' } } },
|
|
@@ -37,6 +37,22 @@ describe('load plugins', () => {
|
|
|
37
37
|
});
|
|
38
38
|
});
|
|
39
39
|
|
|
40
|
+
test(`should ignore undefined property`, () => {
|
|
41
|
+
const config = mergeConfig([
|
|
42
|
+
{ source: { entries: { app: './App.tsx' } } },
|
|
43
|
+
{ source: { entries: undefined } },
|
|
44
|
+
{ tools: { webpack: () => ({}) } },
|
|
45
|
+
{ tools: { webpack: undefined } },
|
|
46
|
+
]);
|
|
47
|
+
expect(config.source).toEqual({
|
|
48
|
+
entries: {
|
|
49
|
+
app: './App.tsx',
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
expect(Array.isArray(config.tools.webpack)).toBe(true);
|
|
53
|
+
expect(config.tools.webpack?.length).toBe(1);
|
|
54
|
+
});
|
|
55
|
+
|
|
40
56
|
test(`should merge array value`, () => {
|
|
41
57
|
expect(
|
|
42
58
|
mergeConfig([
|
|
@@ -67,12 +83,15 @@ describe('load plugins', () => {
|
|
|
67
83
|
{ source: { alias: { a: 'b' } } },
|
|
68
84
|
{ source: { alias: () => ({ c: 'd' }) } },
|
|
69
85
|
{ tools: { webpack: () => ({}) } },
|
|
86
|
+
{ tools: { webpack: { name: 'test' } } },
|
|
87
|
+
{ tools: { webpack: () => ({}) } },
|
|
70
88
|
]);
|
|
71
89
|
expect(Array.isArray(config.source.alias)).toBe(true);
|
|
72
90
|
expect(config?.source?.alias?.length).toBe(2);
|
|
73
91
|
expect(typeof (config.source.alias as Array<any>)[1]).toBe('function');
|
|
74
|
-
|
|
75
92
|
expect(Array.isArray(config.tools.webpack)).toBe(true);
|
|
76
|
-
expect(config.tools.webpack?.length).toBe(
|
|
93
|
+
expect(config.tools.webpack?.length).toBe(3);
|
|
94
|
+
expect(typeof (config.tools.webpack as Array<any>)[0]).toBe('function');
|
|
95
|
+
expect(typeof (config.tools.webpack as Array<any>)[2]).toBe('function');
|
|
77
96
|
});
|
|
78
97
|
});
|
|
@@ -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
|
+
});
|
package/tests/schema.test.ts
CHANGED
|
@@ -8,16 +8,14 @@ describe('patch schemas', () => {
|
|
|
8
8
|
schema: { type: 'string' },
|
|
9
9
|
},
|
|
10
10
|
{
|
|
11
|
-
target: 'deploy.
|
|
11
|
+
target: 'deploy.foo',
|
|
12
12
|
schema: { type: 'number' },
|
|
13
13
|
},
|
|
14
14
|
]);
|
|
15
15
|
|
|
16
16
|
expect(schema.properties).toHaveProperty('foo');
|
|
17
17
|
|
|
18
|
-
expect(
|
|
19
|
-
schema.properties.deploy.properties.microFrontend.properties,
|
|
20
|
-
).toHaveProperty('foo');
|
|
18
|
+
expect(schema.properties.deploy.properties).toHaveProperty('foo');
|
|
21
19
|
});
|
|
22
20
|
|
|
23
21
|
test('should throw error when node is undefined', () => {
|