@modern-js/plugin 2.69.4 → 3.0.0-alpha.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/dist/cjs/cli/api.js +119 -0
- package/dist/cjs/cli/context.js +85 -0
- package/dist/cjs/cli/hooks.js +65 -0
- package/dist/cjs/cli/index.js +68 -0
- package/dist/cjs/cli/run/config/createLoadedConfig.js +87 -0
- package/dist/cjs/cli/run/config/createResolvedConfig.js +48 -0
- package/dist/cjs/cli/run/config/loadConfig.js +113 -0
- package/dist/cjs/cli/run/create.js +187 -0
- package/dist/cjs/cli/run/index.js +56 -0
- package/dist/cjs/cli/run/run.js +57 -0
- package/dist/cjs/cli/run/types.js +18 -0
- package/dist/cjs/cli/run/utils/checkIsDuplicationPlugin.js +45 -0
- package/dist/cjs/cli/run/utils/commander.js +56 -0
- package/dist/cjs/cli/run/utils/createFileWatcher.js +112 -0
- package/dist/cjs/cli/run/utils/debug.js +37 -0
- package/dist/cjs/cli/run/utils/initAppDir.js +55 -0
- package/dist/cjs/cli/run/utils/mergeConfig.js +66 -0
- package/dist/cjs/hooks.js +169 -0
- package/dist/cjs/index.js +53 -26
- package/dist/cjs/manager.js +135 -0
- package/dist/cjs/runtime/api.js +95 -0
- package/dist/cjs/runtime/context.js +60 -0
- package/dist/cjs/runtime/hooks.js +46 -0
- package/dist/cjs/runtime/index.js +51 -0
- package/dist/cjs/runtime/run/create.js +79 -0
- package/dist/cjs/runtime/run/index.js +37 -0
- package/dist/cjs/runtime/run/types.js +18 -0
- package/dist/cjs/server/api.js +96 -0
- package/dist/cjs/server/context.js +75 -0
- package/dist/cjs/server/hooks.js +43 -0
- package/dist/cjs/server/index.js +54 -0
- package/dist/cjs/server/run/create.js +79 -0
- package/dist/cjs/server/run/index.js +40 -0
- package/dist/cjs/server/run/types.js +18 -0
- package/dist/cjs/types/cli/api.js +18 -0
- package/dist/cjs/types/cli/context.js +18 -0
- package/dist/cjs/types/cli/hooks.js +18 -0
- package/dist/cjs/types/cli/index.js +18 -0
- package/dist/cjs/types/cli/plugin.js +18 -0
- package/dist/cjs/types/hooks.js +18 -0
- package/dist/cjs/types/index.js +18 -0
- package/dist/cjs/types/plugin.js +18 -0
- package/dist/cjs/types/runtime/api.js +18 -0
- package/dist/cjs/types/runtime/context.js +18 -0
- package/dist/cjs/types/runtime/hooks.js +18 -0
- package/dist/cjs/types/runtime/index.js +18 -0
- package/dist/cjs/types/runtime/plugin.js +18 -0
- package/dist/cjs/types/server/api.js +18 -0
- package/dist/cjs/types/server/context.js +18 -0
- package/dist/cjs/types/server/hooks.js +18 -0
- package/dist/cjs/types/server/index.js +18 -0
- package/dist/cjs/types/server/plugin.js +18 -0
- package/dist/cjs/types/utils.js +18 -0
- package/dist/esm/cli/api.mjs +85 -0
- package/dist/esm/cli/context.mjs +38 -0
- package/dist/esm/cli/hooks.mjs +31 -0
- package/dist/esm/cli/index.mjs +7 -0
- package/dist/esm/cli/run/config/createLoadedConfig.mjs +53 -0
- package/dist/esm/cli/run/config/createResolvedConfig.mjs +14 -0
- package/dist/esm/cli/run/config/loadConfig.mjs +59 -0
- package/dist/esm/cli/run/create.mjs +150 -0
- package/dist/esm/cli/run/index.mjs +7 -0
- package/dist/esm/cli/run/run.mjs +23 -0
- package/dist/esm/cli/run/types.mjs +0 -0
- package/dist/esm/cli/run/utils/checkIsDuplicationPlugin.mjs +11 -0
- package/dist/esm/cli/run/utils/commander.mjs +16 -0
- package/dist/esm/cli/run/utils/createFileWatcher.mjs +68 -0
- package/dist/esm/cli/run/utils/debug.mjs +3 -0
- package/dist/esm/cli/run/utils/initAppDir.mjs +11 -0
- package/dist/esm/cli/run/utils/mergeConfig.mjs +32 -0
- package/dist/esm/hooks.mjs +120 -0
- package/dist/esm/index.mjs +3 -0
- package/dist/esm/manager.mjs +101 -0
- package/dist/esm/runtime/api.mjs +61 -0
- package/dist/esm/runtime/context.mjs +23 -0
- package/dist/esm/runtime/hooks.mjs +12 -0
- package/dist/esm/runtime/index.mjs +5 -0
- package/dist/esm/runtime/run/create.mjs +45 -0
- package/dist/esm/runtime/run/index.mjs +3 -0
- package/dist/esm/runtime/run/types.mjs +0 -0
- package/dist/esm/server/api.mjs +59 -0
- package/dist/esm/server/context.mjs +38 -0
- package/dist/esm/server/hooks.mjs +9 -0
- package/dist/esm/server/index.mjs +5 -0
- package/dist/esm/server/run/create.mjs +45 -0
- package/dist/esm/server/run/index.mjs +3 -0
- package/dist/esm/server/run/types.mjs +0 -0
- package/dist/esm/types/cli/api.mjs +0 -0
- package/dist/esm/types/cli/context.mjs +0 -0
- package/dist/esm/types/cli/hooks.mjs +0 -0
- package/dist/esm/types/cli/index.mjs +0 -0
- package/dist/esm/types/cli/plugin.mjs +0 -0
- package/dist/esm/types/hooks.mjs +0 -0
- package/dist/esm/types/index.mjs +0 -0
- package/dist/esm/types/plugin.mjs +0 -0
- package/dist/esm/types/runtime/api.mjs +0 -0
- package/dist/esm/types/runtime/context.mjs +0 -0
- package/dist/esm/types/runtime/hooks.mjs +0 -0
- package/dist/esm/types/runtime/index.mjs +0 -0
- package/dist/esm/types/runtime/plugin.mjs +0 -0
- package/dist/esm/types/server/api.mjs +0 -0
- package/dist/esm/types/server/context.mjs +0 -0
- package/dist/esm/types/server/hooks.mjs +0 -0
- package/dist/esm/types/server/index.mjs +0 -0
- package/dist/esm/types/server/plugin.mjs +0 -0
- package/dist/esm/types/utils.mjs +0 -0
- package/dist/esm-node/cli/api.mjs +85 -0
- package/dist/esm-node/cli/context.mjs +38 -0
- package/dist/esm-node/cli/hooks.mjs +31 -0
- package/dist/esm-node/cli/index.mjs +7 -0
- package/dist/esm-node/cli/run/config/createLoadedConfig.mjs +53 -0
- package/dist/esm-node/cli/run/config/createResolvedConfig.mjs +14 -0
- package/dist/esm-node/cli/run/config/loadConfig.mjs +59 -0
- package/dist/esm-node/cli/run/create.mjs +150 -0
- package/dist/esm-node/cli/run/index.mjs +7 -0
- package/dist/esm-node/cli/run/run.mjs +23 -0
- package/dist/esm-node/cli/run/utils/checkIsDuplicationPlugin.mjs +11 -0
- package/dist/esm-node/cli/run/utils/commander.mjs +16 -0
- package/dist/esm-node/cli/run/utils/createFileWatcher.mjs +68 -0
- package/dist/esm-node/cli/run/utils/debug.mjs +3 -0
- package/dist/esm-node/cli/run/utils/initAppDir.mjs +11 -0
- package/dist/esm-node/cli/run/utils/mergeConfig.mjs +32 -0
- package/dist/esm-node/hooks.mjs +120 -0
- package/dist/esm-node/index.mjs +3 -0
- package/dist/esm-node/manager.mjs +101 -0
- package/dist/esm-node/runtime/api.mjs +61 -0
- package/dist/esm-node/runtime/context.mjs +23 -0
- package/dist/esm-node/runtime/hooks.mjs +12 -0
- package/dist/esm-node/runtime/index.mjs +5 -0
- package/dist/esm-node/runtime/run/create.mjs +45 -0
- package/dist/esm-node/runtime/run/index.mjs +3 -0
- package/dist/esm-node/runtime/run/types.mjs +0 -0
- package/dist/esm-node/server/api.mjs +59 -0
- package/dist/esm-node/server/context.mjs +38 -0
- package/dist/esm-node/server/hooks.mjs +9 -0
- package/dist/esm-node/server/index.mjs +5 -0
- package/dist/esm-node/server/run/create.mjs +45 -0
- package/dist/esm-node/server/run/index.mjs +3 -0
- package/dist/esm-node/server/run/types.mjs +0 -0
- package/dist/esm-node/types/cli/api.mjs +0 -0
- package/dist/esm-node/types/cli/context.mjs +0 -0
- package/dist/esm-node/types/cli/hooks.mjs +0 -0
- package/dist/esm-node/types/cli/index.mjs +0 -0
- package/dist/esm-node/types/cli/plugin.mjs +0 -0
- package/dist/esm-node/types/hooks.mjs +0 -0
- package/dist/esm-node/types/index.mjs +0 -0
- package/dist/esm-node/types/plugin.mjs +0 -0
- package/dist/esm-node/types/runtime/api.mjs +0 -0
- package/dist/esm-node/types/runtime/context.mjs +0 -0
- package/dist/esm-node/types/runtime/hooks.mjs +0 -0
- package/dist/esm-node/types/runtime/index.mjs +0 -0
- package/dist/esm-node/types/runtime/plugin.mjs +0 -0
- package/dist/esm-node/types/server/api.mjs +0 -0
- package/dist/esm-node/types/server/context.mjs +0 -0
- package/dist/esm-node/types/server/hooks.mjs +0 -0
- package/dist/esm-node/types/server/index.mjs +0 -0
- package/dist/esm-node/types/server/plugin.mjs +0 -0
- package/dist/esm-node/types/utils.mjs +0 -0
- package/dist/types/cli/api.d.ts +8 -0
- package/dist/types/cli/context.d.ts +19 -0
- package/dist/types/cli/hooks.d.ts +42 -0
- package/dist/types/cli/index.d.ts +6 -0
- package/dist/types/cli/run/config/createLoadedConfig.d.ts +2 -0
- package/dist/types/cli/run/config/createResolvedConfig.d.ts +3 -0
- package/dist/types/cli/run/config/loadConfig.d.ts +25 -0
- package/dist/types/cli/run/create.d.ts +16 -0
- package/dist/types/cli/run/index.d.ts +13 -0
- package/dist/types/cli/run/run.d.ts +2 -0
- package/dist/types/cli/run/types.d.ts +32 -0
- package/dist/types/cli/run/utils/checkIsDuplicationPlugin.d.ts +1 -0
- package/dist/types/cli/run/utils/commander.d.ts +5 -0
- package/dist/types/cli/run/utils/createFileWatcher.d.ts +3 -0
- package/dist/types/cli/run/utils/debug.d.ts +1 -0
- package/dist/types/cli/run/utils/initAppDir.d.ts +1 -0
- package/dist/types/cli/run/utils/mergeConfig.d.ts +2 -0
- package/dist/types/hooks.d.ts +7 -0
- package/dist/types/index.d.ts +7 -4
- package/dist/types/manager.d.ts +5 -0
- package/dist/types/runtime/api.d.ts +9 -0
- package/dist/types/runtime/context.d.ts +11 -0
- package/dist/types/runtime/hooks.d.ts +2 -0
- package/dist/types/runtime/index.d.ts +5 -0
- package/dist/types/runtime/run/create.d.ts +7 -0
- package/dist/types/runtime/run/index.d.ts +5 -0
- package/dist/types/runtime/run/types.d.ts +6 -0
- package/dist/types/server/api.d.ts +9 -0
- package/dist/types/server/context.d.ts +13 -0
- package/dist/types/server/hooks.d.ts +9 -0
- package/dist/types/server/index.d.ts +4 -0
- package/dist/types/server/run/create.d.ts +7 -0
- package/dist/types/server/run/index.d.ts +8 -0
- package/dist/types/server/run/types.d.ts +22 -0
- package/dist/types/types/cli/api.d.ts +49 -0
- package/dist/types/types/cli/context.d.ts +42 -0
- package/dist/types/types/cli/hooks.d.ts +72 -0
- package/dist/types/types/cli/index.d.ts +4 -0
- package/dist/types/types/cli/plugin.d.ts +17 -0
- package/dist/types/types/hooks.d.ts +27 -0
- package/dist/types/types/index.d.ts +4 -0
- package/dist/types/types/plugin.d.ts +46 -0
- package/dist/types/types/runtime/api.d.ts +22 -0
- package/dist/types/types/runtime/context.d.ts +13 -0
- package/dist/types/types/runtime/hooks.d.ts +39 -0
- package/dist/types/types/runtime/index.d.ts +4 -0
- package/dist/types/types/runtime/plugin.d.ts +14 -0
- package/dist/types/types/server/api.d.ts +19 -0
- package/dist/types/types/server/context.d.ts +28 -0
- package/dist/types/types/server/hooks.d.ts +24 -0
- package/dist/types/types/server/index.d.ts +4 -0
- package/dist/types/types/server/plugin.d.ts +14 -0
- package/dist/types/types/utils.d.ts +7 -0
- package/package.json +68 -14
- package/rslib.config.mts +4 -0
- package/rstest.config.ts +5 -0
- package/dist/cjs/farrow-pipeline/context.js +0 -44
- package/dist/cjs/farrow-pipeline/counter.js +0 -37
- package/dist/cjs/farrow-pipeline/index.js +0 -87
- package/dist/cjs/manager/async.js +0 -124
- package/dist/cjs/manager/index.js +0 -26
- package/dist/cjs/manager/shared.js +0 -60
- package/dist/cjs/manager/sync.js +0 -194
- package/dist/cjs/manager/types.js +0 -16
- package/dist/cjs/waterfall/async.js +0 -68
- package/dist/cjs/waterfall/index.js +0 -24
- package/dist/cjs/waterfall/sync.js +0 -67
- package/dist/cjs/workflow/async.js +0 -60
- package/dist/cjs/workflow/index.js +0 -30
- package/dist/cjs/workflow/interrupt.js +0 -64
- package/dist/cjs/workflow/parallel.js +0 -53
- package/dist/cjs/workflow/sync.js +0 -56
- package/dist/cjs/workflow/syncParallel.js +0 -55
- package/dist/esm/index.js +0 -882
- package/dist/esm-node/farrow-pipeline/context.js +0 -20
- package/dist/esm-node/farrow-pipeline/counter.js +0 -13
- package/dist/esm-node/farrow-pipeline/index.js +0 -60
- package/dist/esm-node/index.js +0 -4
- package/dist/esm-node/manager/async.js +0 -106
- package/dist/esm-node/manager/index.js +0 -3
- package/dist/esm-node/manager/shared.js +0 -32
- package/dist/esm-node/manager/sync.js +0 -172
- package/dist/esm-node/waterfall/async.js +0 -42
- package/dist/esm-node/waterfall/index.js +0 -2
- package/dist/esm-node/waterfall/sync.js +0 -41
- package/dist/esm-node/workflow/async.js +0 -35
- package/dist/esm-node/workflow/index.js +0 -5
- package/dist/esm-node/workflow/interrupt.js +0 -39
- package/dist/esm-node/workflow/parallel.js +0 -28
- package/dist/esm-node/workflow/sync.js +0 -31
- package/dist/esm-node/workflow/syncParallel.js +0 -30
- package/dist/types/farrow-pipeline/context.d.ts +0 -12
- package/dist/types/farrow-pipeline/counter.d.ts +0 -11
- package/dist/types/farrow-pipeline/index.d.ts +0 -29
- package/dist/types/manager/async.d.ts +0 -54
- package/dist/types/manager/index.d.ts +0 -3
- package/dist/types/manager/shared.d.ts +0 -12
- package/dist/types/manager/sync.d.ts +0 -66
- package/dist/types/manager/types.d.ts +0 -33
- package/dist/types/waterfall/async.d.ts +0 -21
- package/dist/types/waterfall/index.d.ts +0 -2
- package/dist/types/waterfall/sync.d.ts +0 -20
- package/dist/types/workflow/async.d.ts +0 -12
- package/dist/types/workflow/index.d.ts +0 -5
- package/dist/types/workflow/interrupt.d.ts +0 -12
- package/dist/types/workflow/parallel.d.ts +0 -10
- package/dist/types/workflow/sync.d.ts +0 -11
- package/dist/types/workflow/syncParallel.d.ts +0 -10
- /package/dist/esm-node/{manager/types.js → cli/run/types.mjs} +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { merge } from "@modern-js/runtime-utils/merge";
|
|
2
|
+
import { createPluginManager } from "../../manager";
|
|
3
|
+
import { initPluginAPI } from "../../runtime/api";
|
|
4
|
+
import { createRuntimeContext, initRuntimeContext } from "../../runtime/context";
|
|
5
|
+
const createRuntime = ()=>{
|
|
6
|
+
let initOptions;
|
|
7
|
+
const pluginManager = createPluginManager();
|
|
8
|
+
function init(options) {
|
|
9
|
+
pluginManager.clear();
|
|
10
|
+
initOptions = options;
|
|
11
|
+
const { plugins: allPlugins, handleSetupResult } = options;
|
|
12
|
+
pluginManager.addPlugins(allPlugins);
|
|
13
|
+
const plugins = pluginManager.getPlugins();
|
|
14
|
+
const context = createRuntimeContext({
|
|
15
|
+
runtimeContext: initRuntimeContext(),
|
|
16
|
+
config: initOptions.config,
|
|
17
|
+
plugins
|
|
18
|
+
});
|
|
19
|
+
const pluginAPI = initPluginAPI({
|
|
20
|
+
context,
|
|
21
|
+
pluginManager,
|
|
22
|
+
plugins
|
|
23
|
+
});
|
|
24
|
+
context.pluginAPI = pluginAPI;
|
|
25
|
+
for (const plugin of plugins){
|
|
26
|
+
const setupResult = plugin.setup?.(pluginAPI);
|
|
27
|
+
if (handleSetupResult) handleSetupResult(setupResult, pluginAPI);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
runtimeContext: context
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function run(options) {
|
|
34
|
+
const { runtimeContext } = init(options);
|
|
35
|
+
const configs = runtimeContext.hooks.config.call().filter((config)=>Boolean(config));
|
|
36
|
+
runtimeContext.config = merge({}, ...configs, runtimeContext.config || {});
|
|
37
|
+
return {
|
|
38
|
+
runtimeContext
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
run
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
export { createRuntime };
|
|
File without changes
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createDebugger } from "@modern-js/utils";
|
|
2
|
+
import { assign as lodash_assign } from "@modern-js/utils/lodash";
|
|
3
|
+
const debug = createDebugger('plugin-server-v2');
|
|
4
|
+
function initPluginAPI({ context, pluginManager }) {
|
|
5
|
+
const { hooks, extendsHooks, plugins } = context;
|
|
6
|
+
function getServerContext() {
|
|
7
|
+
if (context) {
|
|
8
|
+
const { hooks, extendsHooks, config, pluginAPI, ...serverContext } = context;
|
|
9
|
+
serverContext._internalContext = context;
|
|
10
|
+
return serverContext;
|
|
11
|
+
}
|
|
12
|
+
throw new Error('Cannot access context');
|
|
13
|
+
}
|
|
14
|
+
function getConfig() {
|
|
15
|
+
if (context.config) return context.config;
|
|
16
|
+
throw new Error('Cannot access config');
|
|
17
|
+
}
|
|
18
|
+
function getHooks() {
|
|
19
|
+
return context.hooks;
|
|
20
|
+
}
|
|
21
|
+
const extendsPluginApi = {};
|
|
22
|
+
plugins.forEach((plugin)=>{
|
|
23
|
+
const { _registryApi } = plugin;
|
|
24
|
+
if (_registryApi) {
|
|
25
|
+
const apis = _registryApi(getServerContext, updateServerContext);
|
|
26
|
+
Object.keys(apis).forEach((apiName)=>{
|
|
27
|
+
extendsPluginApi[apiName] = apis[apiName];
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
if (extendsHooks) Object.keys(extendsHooks).forEach((hookName)=>{
|
|
32
|
+
extendsPluginApi[hookName] = extendsHooks[hookName].tap;
|
|
33
|
+
});
|
|
34
|
+
function updateServerContext(updateContext) {
|
|
35
|
+
context = lodash_assign(context, updateContext);
|
|
36
|
+
}
|
|
37
|
+
const pluginAPI = {
|
|
38
|
+
isPluginExists: pluginManager.isPluginExists,
|
|
39
|
+
getServerContext,
|
|
40
|
+
getServerConfig: getConfig,
|
|
41
|
+
getHooks,
|
|
42
|
+
updateServerContext,
|
|
43
|
+
modifyConfig: hooks.modifyConfig.tap,
|
|
44
|
+
onPrepare: hooks.onPrepare.tap,
|
|
45
|
+
onReset: hooks.onReset.tap,
|
|
46
|
+
...extendsPluginApi
|
|
47
|
+
};
|
|
48
|
+
if ("u" < typeof Proxy) return pluginAPI;
|
|
49
|
+
return new Proxy(pluginAPI, {
|
|
50
|
+
get (target, prop) {
|
|
51
|
+
if ('then' === prop) return;
|
|
52
|
+
if (prop in target) return target[prop];
|
|
53
|
+
return ()=>{
|
|
54
|
+
debug(`api.${prop.toString()} not exist`);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export { debug, initPluginAPI };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { initHooks } from "./hooks";
|
|
2
|
+
function initServerContext(params) {
|
|
3
|
+
const { options, plugins } = params;
|
|
4
|
+
return {
|
|
5
|
+
routes: options.routes || [],
|
|
6
|
+
appDirectory: options.appContext.appDirectory || '',
|
|
7
|
+
apiDirectory: options.appContext.apiDirectory,
|
|
8
|
+
lambdaDirectory: options.appContext.lambdaDirectory,
|
|
9
|
+
internalDirectory: options.appContext.internalDirectory || '',
|
|
10
|
+
sharedDirectory: options.appContext.sharedDirectory || '',
|
|
11
|
+
distDirectory: options.pwd,
|
|
12
|
+
metaName: options.metaName || 'modern-js',
|
|
13
|
+
plugins: plugins,
|
|
14
|
+
middlewares: [],
|
|
15
|
+
bffRuntimeFramework: options.appContext.bffRuntimeFramework,
|
|
16
|
+
renderMiddlewares: []
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function createServerContext({ serverContext, config }) {
|
|
20
|
+
const { plugins } = serverContext;
|
|
21
|
+
const extendsHooks = {};
|
|
22
|
+
plugins.forEach((plugin)=>{
|
|
23
|
+
const { registryHooks = {} } = plugin;
|
|
24
|
+
Object.keys(registryHooks).forEach((hookName)=>{
|
|
25
|
+
extendsHooks[hookName] = registryHooks[hookName];
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
...serverContext,
|
|
30
|
+
hooks: {
|
|
31
|
+
...initHooks(),
|
|
32
|
+
...extendsHooks
|
|
33
|
+
},
|
|
34
|
+
extendsHooks,
|
|
35
|
+
config
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export { createServerContext, initServerContext };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { initPluginAPI } from "./api";
|
|
2
|
+
import { createServerContext, initServerContext } from "./context";
|
|
3
|
+
import { initHooks } from "./hooks";
|
|
4
|
+
import { createServer, server } from "./run";
|
|
5
|
+
export { createServer, createServerContext, initHooks, initPluginAPI, initServerContext, server };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createPluginManager } from "../../manager";
|
|
2
|
+
import { initPluginAPI } from "../../server/api";
|
|
3
|
+
import { createServerContext, initServerContext } from "../../server/context";
|
|
4
|
+
const createServer = ()=>{
|
|
5
|
+
let initOptions;
|
|
6
|
+
const pluginManager = createPluginManager();
|
|
7
|
+
function init(options) {
|
|
8
|
+
pluginManager.clear();
|
|
9
|
+
initOptions = options;
|
|
10
|
+
const { plugins: allPlugins, options: runOptions, handleSetupResult } = initOptions;
|
|
11
|
+
pluginManager.addPlugins(allPlugins);
|
|
12
|
+
const plugins = pluginManager.getPlugins();
|
|
13
|
+
const context = createServerContext({
|
|
14
|
+
serverContext: initServerContext({
|
|
15
|
+
plugins,
|
|
16
|
+
options: runOptions
|
|
17
|
+
}),
|
|
18
|
+
config: initOptions.config
|
|
19
|
+
});
|
|
20
|
+
const pluginAPI = initPluginAPI({
|
|
21
|
+
context,
|
|
22
|
+
pluginManager
|
|
23
|
+
});
|
|
24
|
+
context.pluginAPI = pluginAPI;
|
|
25
|
+
for (const plugin of plugins){
|
|
26
|
+
const setupResult = plugin.setup?.(pluginAPI);
|
|
27
|
+
if (handleSetupResult) handleSetupResult(setupResult, pluginAPI);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
serverContext: context
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
async function run(options) {
|
|
34
|
+
const { serverContext } = init(options);
|
|
35
|
+
const config = await serverContext.hooks.modifyConfig.call(serverContext.config);
|
|
36
|
+
serverContext.config = config;
|
|
37
|
+
return {
|
|
38
|
+
serverContext
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
run
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
export { createServer };
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { assign as lodash_assign } from "@modern-js/utils/lodash";
|
|
2
|
+
import { debug } from "./run/utils/debug.mjs";
|
|
3
|
+
function initPluginAPI({ context, pluginManager }) {
|
|
4
|
+
const { hooks, extendsHooks, plugins } = context;
|
|
5
|
+
function getAppContext() {
|
|
6
|
+
if (context) {
|
|
7
|
+
const { hooks, extendsHooks, config, normalizedConfig, pluginAPI, ...appContext } = context;
|
|
8
|
+
appContext._internalContext = context;
|
|
9
|
+
return appContext;
|
|
10
|
+
}
|
|
11
|
+
throw new Error('Cannot access context');
|
|
12
|
+
}
|
|
13
|
+
function getConfig() {
|
|
14
|
+
if (context.config) return context.config;
|
|
15
|
+
throw new Error('Cannot access config');
|
|
16
|
+
}
|
|
17
|
+
function getNormalizedConfig() {
|
|
18
|
+
if (context.normalizedConfig) return context.normalizedConfig;
|
|
19
|
+
throw new Error('Cannot access normalized config');
|
|
20
|
+
}
|
|
21
|
+
function getHooks() {
|
|
22
|
+
return context.hooks;
|
|
23
|
+
}
|
|
24
|
+
const extendsPluginApi = {};
|
|
25
|
+
plugins.forEach((plugin)=>{
|
|
26
|
+
const { _registryApi } = plugin;
|
|
27
|
+
if (_registryApi) {
|
|
28
|
+
const apis = _registryApi(getAppContext, updateAppContext);
|
|
29
|
+
Object.keys(apis).forEach((apiName)=>{
|
|
30
|
+
extendsPluginApi[apiName] = apis[apiName];
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
if (extendsHooks) Object.keys(extendsHooks).forEach((hookName)=>{
|
|
35
|
+
extendsPluginApi[hookName] = extendsHooks[hookName].tap;
|
|
36
|
+
});
|
|
37
|
+
function updateAppContext(updateContext) {
|
|
38
|
+
context = lodash_assign(context, updateContext);
|
|
39
|
+
}
|
|
40
|
+
const pluginAPI = {
|
|
41
|
+
isPluginExists: pluginManager.isPluginExists,
|
|
42
|
+
getAppContext,
|
|
43
|
+
getConfig,
|
|
44
|
+
getNormalizedConfig,
|
|
45
|
+
getHooks,
|
|
46
|
+
updateAppContext,
|
|
47
|
+
config: hooks.config.tap,
|
|
48
|
+
modifyConfig: hooks.modifyConfig.tap,
|
|
49
|
+
modifyResolvedConfig: hooks.modifyResolvedConfig.tap,
|
|
50
|
+
modifyRsbuildConfig: hooks.modifyRsbuildConfig.tap,
|
|
51
|
+
modifyBundlerChain: hooks.modifyBundlerChain.tap,
|
|
52
|
+
modifyRspackConfig: hooks.modifyRspackConfig.tap,
|
|
53
|
+
modifyHtmlPartials: hooks.modifyHtmlPartials.tap,
|
|
54
|
+
addCommand: hooks.addCommand.tap,
|
|
55
|
+
onPrepare: hooks.onPrepare.tap,
|
|
56
|
+
addWatchFiles: hooks.addWatchFiles.tap,
|
|
57
|
+
onFileChanged: hooks.onFileChanged.tap,
|
|
58
|
+
onBeforeRestart: hooks.onBeforeRestart.tap,
|
|
59
|
+
onBeforeCreateCompiler: hooks.onBeforeCreateCompiler.tap,
|
|
60
|
+
onDevCompileDone: hooks.onDevCompileDone.tap,
|
|
61
|
+
onAfterCreateCompiler: hooks.onAfterCreateCompiler.tap,
|
|
62
|
+
onBeforeBuild: hooks.onBeforeBuild.tap,
|
|
63
|
+
onAfterBuild: hooks.onAfterBuild.tap,
|
|
64
|
+
onBeforeDev: hooks.onBeforeDev.tap,
|
|
65
|
+
onAfterDev: hooks.onAfterDev.tap,
|
|
66
|
+
onBeforeDeploy: hooks.onBeforeDeploy.tap,
|
|
67
|
+
onAfterDeploy: hooks.onAfterDeploy.tap,
|
|
68
|
+
onBeforeExit: hooks.onBeforeExit.tap,
|
|
69
|
+
_internalRuntimePlugins: hooks._internalRuntimePlugins.tap,
|
|
70
|
+
_internalServerPlugins: hooks._internalServerPlugins.tap,
|
|
71
|
+
modifyServerRoutes: hooks.modifyServerRoutes.tap,
|
|
72
|
+
...extendsPluginApi
|
|
73
|
+
};
|
|
74
|
+
if ("u" < typeof Proxy) return pluginAPI;
|
|
75
|
+
return new Proxy(pluginAPI, {
|
|
76
|
+
get (target, prop) {
|
|
77
|
+
if ('then' === prop) return;
|
|
78
|
+
if (prop in target) return target[prop];
|
|
79
|
+
return ()=>{
|
|
80
|
+
debug(`api.${prop.toString()} not exist`);
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
export { initPluginAPI };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { initHooks } from "./hooks.mjs";
|
|
3
|
+
function initAppContext(params) {
|
|
4
|
+
const { appDirectory, srcDir = 'src', distDir = 'dist' } = params;
|
|
5
|
+
return {
|
|
6
|
+
metaName: params.metaName,
|
|
7
|
+
packageName: params.packageName,
|
|
8
|
+
configFile: params.configFile,
|
|
9
|
+
command: params.command,
|
|
10
|
+
isProd: 'production' === process.env.NODE_ENV,
|
|
11
|
+
appDirectory: appDirectory,
|
|
12
|
+
srcDirectory: path.resolve(appDirectory, srcDir),
|
|
13
|
+
distDirectory: '',
|
|
14
|
+
nodeModulesDirectory: path.resolve(appDirectory, 'node_modules'),
|
|
15
|
+
plugins: params.plugins
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
async function createContext({ appContext, config, normalizedConfig }) {
|
|
19
|
+
const { plugins } = appContext;
|
|
20
|
+
const extendsHooks = {};
|
|
21
|
+
plugins.forEach((plugin)=>{
|
|
22
|
+
const { registryHooks = {} } = plugin;
|
|
23
|
+
Object.keys(registryHooks).forEach((hookName)=>{
|
|
24
|
+
extendsHooks[hookName] = registryHooks[hookName];
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
...appContext,
|
|
29
|
+
hooks: {
|
|
30
|
+
...initHooks(),
|
|
31
|
+
...extendsHooks
|
|
32
|
+
},
|
|
33
|
+
extendsHooks,
|
|
34
|
+
config,
|
|
35
|
+
normalizedConfig
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export { createContext, initAppContext };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createAsyncHook, createCollectAsyncHook } from "../hooks.mjs";
|
|
2
|
+
function initHooks() {
|
|
3
|
+
return {
|
|
4
|
+
config: createCollectAsyncHook(),
|
|
5
|
+
modifyConfig: createAsyncHook(),
|
|
6
|
+
modifyResolvedConfig: createAsyncHook(),
|
|
7
|
+
modifyRsbuildConfig: createAsyncHook(),
|
|
8
|
+
modifyBundlerChain: createAsyncHook(),
|
|
9
|
+
modifyRspackConfig: createAsyncHook(),
|
|
10
|
+
modifyHtmlPartials: createAsyncHook(),
|
|
11
|
+
addCommand: createAsyncHook(),
|
|
12
|
+
addWatchFiles: createCollectAsyncHook(),
|
|
13
|
+
onPrepare: createAsyncHook(),
|
|
14
|
+
onFileChanged: createAsyncHook(),
|
|
15
|
+
onBeforeRestart: createAsyncHook(),
|
|
16
|
+
onBeforeCreateCompiler: createAsyncHook(),
|
|
17
|
+
onAfterCreateCompiler: createAsyncHook(),
|
|
18
|
+
onDevCompileDone: createAsyncHook(),
|
|
19
|
+
onBeforeBuild: createAsyncHook(),
|
|
20
|
+
onAfterBuild: createAsyncHook(),
|
|
21
|
+
onBeforeDev: createAsyncHook(),
|
|
22
|
+
onAfterDev: createAsyncHook(),
|
|
23
|
+
onBeforeDeploy: createAsyncHook(),
|
|
24
|
+
onAfterDeploy: createAsyncHook(),
|
|
25
|
+
onBeforeExit: createAsyncHook(),
|
|
26
|
+
_internalRuntimePlugins: createAsyncHook(),
|
|
27
|
+
_internalServerPlugins: createAsyncHook(),
|
|
28
|
+
modifyServerRoutes: createAsyncHook()
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export { initHooks };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { initPluginAPI } from "./api.mjs";
|
|
2
|
+
import { createContext, initAppContext } from "./context.mjs";
|
|
3
|
+
import { initHooks } from "./hooks.mjs";
|
|
4
|
+
import { cli, createCli, createLoadedConfig, initAppDir } from "./run/index.mjs";
|
|
5
|
+
import { createStorybookOptions } from "./run/create.mjs";
|
|
6
|
+
import { mergeConfig } from "./run/utils/mergeConfig.mjs";
|
|
7
|
+
export { cli, createCli, createContext, createLoadedConfig, createStorybookOptions, initAppContext, initAppDir, initHooks, initPluginAPI, mergeConfig };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { CONFIG_FILE_EXTENSIONS, chalk, fs, getCommand, getNodeEnv, isDevCommand, logger } from "@modern-js/utils";
|
|
2
|
+
import { mergeConfig } from "../utils/mergeConfig.mjs";
|
|
3
|
+
import { getConfigFilePath, loadConfig } from "./loadConfig.mjs";
|
|
4
|
+
async function getConfigObject(config) {
|
|
5
|
+
if ('function' == typeof config) return await config({
|
|
6
|
+
env: getNodeEnv(),
|
|
7
|
+
command: getCommand()
|
|
8
|
+
}) || {};
|
|
9
|
+
return config || {};
|
|
10
|
+
}
|
|
11
|
+
async function loadLocalConfig(appDirectory, configFile) {
|
|
12
|
+
let localConfigFile = false;
|
|
13
|
+
if ('string' == typeof configFile) {
|
|
14
|
+
for (const ext of CONFIG_FILE_EXTENSIONS)if (configFile.endsWith(ext)) {
|
|
15
|
+
const replacedPath = configFile.replace(ext, `.local${ext}`);
|
|
16
|
+
if (fs.existsSync(replacedPath)) localConfigFile = replacedPath;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (localConfigFile) {
|
|
20
|
+
const loaded = await loadConfig(appDirectory, localConfigFile);
|
|
21
|
+
return getConfigObject(loaded.config);
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
async function createLoadedConfig(appDirectory, configFilePath, otherConfig) {
|
|
26
|
+
const configFile = getConfigFilePath(appDirectory, configFilePath);
|
|
27
|
+
const loaded = await loadConfig(appDirectory, configFile);
|
|
28
|
+
if (!loaded.config && !loaded.pkgConfig) {
|
|
29
|
+
logger.warn("Can not find any config file in the current project, please check if you have a correct config file.");
|
|
30
|
+
logger.warn(`Current project path: ${chalk.yellow(appDirectory)}`);
|
|
31
|
+
}
|
|
32
|
+
const config = await getConfigObject(loaded.config);
|
|
33
|
+
let mergedConfig = config;
|
|
34
|
+
if (isDevCommand()) {
|
|
35
|
+
const localConfig = await loadLocalConfig(appDirectory, configFile);
|
|
36
|
+
if (localConfig) mergedConfig = mergeConfig([
|
|
37
|
+
mergedConfig,
|
|
38
|
+
localConfig
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
if (otherConfig) mergedConfig = mergeConfig([
|
|
42
|
+
mergedConfig,
|
|
43
|
+
otherConfig
|
|
44
|
+
]);
|
|
45
|
+
return {
|
|
46
|
+
packageName: loaded.packageName,
|
|
47
|
+
config: mergedConfig,
|
|
48
|
+
configFile: loaded.configFile,
|
|
49
|
+
pkgConfig: loaded.pkgConfig,
|
|
50
|
+
jsConfig: loaded.config
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export { createLoadedConfig };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createDebugger } from "@modern-js/utils";
|
|
2
|
+
import { mergeConfig } from "../utils/mergeConfig.mjs";
|
|
3
|
+
const debug = createDebugger('resolve-config');
|
|
4
|
+
const createResolveConfig = async (loaded, configs)=>{
|
|
5
|
+
const { config: userConfig } = loaded;
|
|
6
|
+
const resolved = mergeConfig([
|
|
7
|
+
{},
|
|
8
|
+
...configs,
|
|
9
|
+
userConfig
|
|
10
|
+
]);
|
|
11
|
+
debug('resolved %o', resolved);
|
|
12
|
+
return resolved;
|
|
13
|
+
};
|
|
14
|
+
export { createResolveConfig };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs, globby } from "@modern-js/utils";
|
|
3
|
+
import jiti from "jiti";
|
|
4
|
+
const getConfigFilePath = (appDirectory, configFilePath)=>{
|
|
5
|
+
if (path.isAbsolute(configFilePath)) return configFilePath;
|
|
6
|
+
return path.resolve(appDirectory, configFilePath);
|
|
7
|
+
};
|
|
8
|
+
const clearFilesOverTime = async (targetDir, overtime)=>{
|
|
9
|
+
try {
|
|
10
|
+
const files = await globby(`${targetDir}/**/*`, {
|
|
11
|
+
stats: true,
|
|
12
|
+
absolute: true
|
|
13
|
+
});
|
|
14
|
+
const currentTime = Date.now();
|
|
15
|
+
if (files.length > 0) {
|
|
16
|
+
for (const file of files)if (currentTime - file.stats.birthtimeMs >= 1000 * overtime) fs.unlinkSync(file.path);
|
|
17
|
+
}
|
|
18
|
+
} catch (err) {}
|
|
19
|
+
};
|
|
20
|
+
function loadConfigContent(configFile) {
|
|
21
|
+
const _require = jiti(__filename, {
|
|
22
|
+
esmResolve: true,
|
|
23
|
+
requireCache: false,
|
|
24
|
+
interopDefault: true
|
|
25
|
+
});
|
|
26
|
+
if (!fs.existsSync(configFile)) throw new Error(`Configuration file does not exist: ${configFile}`);
|
|
27
|
+
try {
|
|
28
|
+
const config = _require(configFile);
|
|
29
|
+
return config.default || config;
|
|
30
|
+
} catch (e) {
|
|
31
|
+
if (e instanceof Error) e.message = `Get Error while loading config file: ${configFile}, please check it and retry.\n${e.message || ''}`;
|
|
32
|
+
throw e;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const loadTypeScriptFile = (filePath)=>{
|
|
36
|
+
const _require = jiti(__filename, {
|
|
37
|
+
esmResolve: true,
|
|
38
|
+
requireCache: false,
|
|
39
|
+
interopDefault: true
|
|
40
|
+
});
|
|
41
|
+
if (!fs.existsSync(filePath)) throw new Error(`TypeScript file does not exist: ${filePath}`);
|
|
42
|
+
try {
|
|
43
|
+
return _require(filePath);
|
|
44
|
+
} catch (e) {
|
|
45
|
+
if (e instanceof Error) e.message = `Get Error while loading TypeScript file: ${filePath}, please check it and retry.\n${e.message || ''}`;
|
|
46
|
+
throw e;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const loadConfig = async (appDirectory, configFile)=>{
|
|
50
|
+
const packageName = require(path.resolve(appDirectory, './package.json')).name;
|
|
51
|
+
let config;
|
|
52
|
+
if (configFile) config = loadConfigContent(configFile);
|
|
53
|
+
return {
|
|
54
|
+
packageName,
|
|
55
|
+
configFile,
|
|
56
|
+
config
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
export { clearFilesOverTime, getConfigFilePath, loadConfig, loadTypeScriptFile };
|