@modern-js/server-core 1.2.2 → 1.2.5

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 ADDED
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ extends: ['@modern-js'],
3
+ parserOptions: {
4
+ project: require.resolve('./tsconfig.json'),
5
+ },
6
+ };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,39 @@
1
1
  # @modern-js/server-plugin
2
2
 
3
+ ## 1.2.5
4
+
5
+ ### Patch Changes
6
+
7
+ - bebb39b6: chore: improve devDependencies and peerDependencies
8
+ - Updated dependencies [132f7b53]
9
+ - @modern-js/utils@1.3.7
10
+
11
+ ## 1.2.4
12
+
13
+ ### Patch Changes
14
+
15
+ - a8df060e: support setup dev middleware first step
16
+ - 18db013a: support load plugin instace
17
+ - Updated dependencies [c2046f37]
18
+ - Updated dependencies [dc88abf9]
19
+ - Updated dependencies [0462ff77]
20
+ - @modern-js/utils@1.3.6
21
+ - @modern-js/plugin@1.3.2
22
+
23
+ ## 1.2.3
24
+
25
+ ### Patch Changes
26
+
27
+ - d95f28c3: should enable babel register before server plugin require
28
+ - 2e8dec93: feat: adust `ServerPlugin` type to define new plugin
29
+ - 2008fdbd: convert two packages server part, support server load plugin itself
30
+ - 2e8dec93: add `useAppContext`、`useConfigContext` to plugin api
31
+ - Updated dependencies [5bf5868d]
32
+ - Updated dependencies [80d8ddfe]
33
+ - Updated dependencies [491145e3]
34
+ - @modern-js/utils@1.3.5
35
+ - @modern-js/plugin@1.3.0
36
+
3
37
  ## 1.2.2
4
38
 
5
39
  ### Patch Changes
@@ -1,2 +1,3 @@
1
1
  export * from "./plugin";
2
- export * from '@modern-js/plugin';
2
+ export * from '@modern-js/plugin';
3
+ export * from "./loadPlugins";
@@ -0,0 +1,67 @@
1
+ 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; }
2
+
3
+ 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; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import { compatRequire } from '@modern-js/utils';
8
+ import { createPlugin } from "./plugin";
9
+
10
+ const tryResolve = (name, appDirectory) => {
11
+ let filePath = '';
12
+
13
+ try {
14
+ filePath = require.resolve(name, {
15
+ paths: [appDirectory]
16
+ });
17
+ delete require.cache[filePath];
18
+ } catch (err) {
19
+ if (err.code === 'MODULE_NOT_FOUND') {
20
+ throw new Error(`Can not find plugin ${name}.`);
21
+ }
22
+
23
+ throw err;
24
+ }
25
+
26
+ return filePath;
27
+ };
28
+
29
+ export const loadPlugins = (plugins, appDirectory) => {
30
+ const resolvePlugin = p => {
31
+ const isPluginInstance = typeof p !== 'string' && !Array.isArray(p);
32
+
33
+ if (isPluginInstance) {
34
+ return {
35
+ module: createPlugin(p.setup, p)
36
+ };
37
+ }
38
+
39
+ const [pkg, options] = typeof p === 'string' ? [p, undefined] : p;
40
+ const pluginPath = tryResolve(pkg, appDirectory);
41
+ let module = compatRequire(pluginPath);
42
+ const useNewSyntax = typeof module === 'function';
43
+
44
+ if (useNewSyntax) {
45
+ const plugin = module(options);
46
+ module = createPlugin(plugin.setup, plugin);
47
+ }
48
+
49
+ return {
50
+ pkg,
51
+ path: pluginPath,
52
+ module
53
+ };
54
+ };
55
+
56
+ return plugins.map(plugin => {
57
+ const {
58
+ pkg,
59
+ path,
60
+ module
61
+ } = resolvePlugin(plugin);
62
+ return _objectSpread(_objectSpread({}, module), {}, {
63
+ pluginPath: path,
64
+ pkg
65
+ });
66
+ });
67
+ };
@@ -1,4 +1,4 @@
1
- import { createAsyncManager, createAsyncPipeline, createParallelWorkflow, createAsyncWaterfall, createContext } from '@modern-js/plugin';
1
+ import { createContext, createAsyncManager, createAsyncPipeline, createAsyncWaterfall, createParallelWorkflow } from '@modern-js/plugin';
2
2
  import { enable } from '@modern-js/plugin/node';
3
3
  enable(); // collect all middleware register in server plugins
4
4
 
@@ -6,11 +6,9 @@ const gather = createParallelWorkflow();
6
6
  const create = createAsyncPipeline();
7
7
  const prepareWebServer = createAsyncPipeline();
8
8
  const prepareApiServer = createAsyncPipeline();
9
- const preDevServerInit = createParallelWorkflow(); // eslint-disable-next-line @typescript-eslint/ban-types
10
-
9
+ const beforeDevServer = createParallelWorkflow();
11
10
  const setupCompiler = createParallelWorkflow();
12
- const postDevServerInit = createParallelWorkflow(); // TODO FIXME
13
- // eslint-disable-next-line @typescript-eslint/ban-types
11
+ const afterDevServer = createParallelWorkflow(); // TODO FIXME
14
12
 
15
13
  const beforeRouteSet = createAsyncPipeline();
16
14
  const afterRouteSet = createAsyncPipeline();
@@ -24,10 +22,8 @@ const extendContext = createAsyncPipeline();
24
22
  const handleError = createParallelWorkflow();
25
23
  const beforeMatch = createAsyncPipeline();
26
24
  const afterMatch = createAsyncPipeline(); // TODO FIXME
27
- // eslint-disable-next-line @typescript-eslint/ban-types
28
25
 
29
26
  const prefetch = createParallelWorkflow(); // TODO FIXME
30
- // eslint-disable-next-line @typescript-eslint/ban-types
31
27
 
32
28
  const renderToString = createAsyncPipeline();
33
29
  const beforeRender = createAsyncPipeline();
@@ -35,15 +31,31 @@ const afterRender = createAsyncPipeline();
35
31
  const beforeSend = createAsyncPipeline();
36
32
  const afterSend = createParallelWorkflow();
37
33
  const reset = createParallelWorkflow();
38
- export const createServerManager = () => createAsyncManager({
34
+ export const AppContext = createContext({});
35
+ export const ConfigContext = createContext({});
36
+ /**
37
+ * Get original content of user config.
38
+ */
39
+
40
+ export const useConfigContext = () => ConfigContext.use().value;
41
+ /**
42
+ * Get app context, including directories, plugins and some static infos.
43
+ */
44
+
45
+ export const useAppContext = () => AppContext.use().value;
46
+ const pluginAPI = {
47
+ useAppContext,
48
+ useConfigContext
49
+ };
50
+ const serverHooks = {
39
51
  // server hook
40
52
  gather,
41
53
  create,
42
54
  prepareWebServer,
43
55
  prepareApiServer,
44
- preDevServerInit,
56
+ beforeDevServer,
45
57
  setupCompiler,
46
- postDevServerInit,
58
+ afterDevServer,
47
59
  beforeRouteSet,
48
60
  afterRouteSet,
49
61
  preServerInit,
@@ -64,12 +76,13 @@ export const createServerManager = () => createAsyncManager({
64
76
  beforeSend,
65
77
  afterSend,
66
78
  reset
67
- });
79
+ };
80
+ /** All hooks of server plugin. */
81
+
82
+ export const createServerManager = () => createAsyncManager(serverHooks, pluginAPI);
68
83
  export const serverManager = createServerManager();
84
+ /** Plugin options of a server plugin. */
85
+
69
86
  export const {
70
87
  createPlugin
71
- } = serverManager;
72
- export const AppContext = createContext({});
73
- export const ConfigContext = createContext({});
74
- export const useConfigContext = () => ConfigContext.use().value;
75
- export const useAppContext = () => AppContext.use().value;
88
+ } = serverManager;
@@ -28,4 +28,17 @@ Object.keys(_plugin2).forEach(function (key) {
28
28
  return _plugin2[key];
29
29
  }
30
30
  });
31
+ });
32
+
33
+ var _loadPlugins = require("./loadPlugins");
34
+
35
+ Object.keys(_loadPlugins).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _loadPlugins[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function () {
41
+ return _loadPlugins[key];
42
+ }
43
+ });
31
44
  });
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.loadPlugins = void 0;
7
+
8
+ var _utils = require("@modern-js/utils");
9
+
10
+ var _plugin = require("./plugin");
11
+
12
+ 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; }
13
+
14
+ 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; }
15
+
16
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
+
18
+ const tryResolve = (name, appDirectory) => {
19
+ let filePath = '';
20
+
21
+ try {
22
+ filePath = require.resolve(name, {
23
+ paths: [appDirectory]
24
+ });
25
+ delete require.cache[filePath];
26
+ } catch (err) {
27
+ if (err.code === 'MODULE_NOT_FOUND') {
28
+ throw new Error(`Can not find plugin ${name}.`);
29
+ }
30
+
31
+ throw err;
32
+ }
33
+
34
+ return filePath;
35
+ };
36
+
37
+ const loadPlugins = (plugins, appDirectory) => {
38
+ const resolvePlugin = p => {
39
+ const isPluginInstance = typeof p !== 'string' && !Array.isArray(p);
40
+
41
+ if (isPluginInstance) {
42
+ return {
43
+ module: (0, _plugin.createPlugin)(p.setup, p)
44
+ };
45
+ }
46
+
47
+ const [pkg, options] = typeof p === 'string' ? [p, undefined] : p;
48
+ const pluginPath = tryResolve(pkg, appDirectory);
49
+ let module = (0, _utils.compatRequire)(pluginPath);
50
+ const useNewSyntax = typeof module === 'function';
51
+
52
+ if (useNewSyntax) {
53
+ const plugin = module(options);
54
+ module = (0, _plugin.createPlugin)(plugin.setup, plugin);
55
+ }
56
+
57
+ return {
58
+ pkg,
59
+ path: pluginPath,
60
+ module
61
+ };
62
+ };
63
+
64
+ return plugins.map(plugin => {
65
+ const {
66
+ pkg,
67
+ path,
68
+ module
69
+ } = resolvePlugin(plugin);
70
+ return _objectSpread(_objectSpread({}, module), {}, {
71
+ pluginPath: path,
72
+ pkg
73
+ });
74
+ });
75
+ };
76
+
77
+ exports.loadPlugins = loadPlugins;
@@ -15,11 +15,9 @@ const gather = (0, _plugin.createParallelWorkflow)();
15
15
  const create = (0, _plugin.createAsyncPipeline)();
16
16
  const prepareWebServer = (0, _plugin.createAsyncPipeline)();
17
17
  const prepareApiServer = (0, _plugin.createAsyncPipeline)();
18
- const preDevServerInit = (0, _plugin.createParallelWorkflow)(); // eslint-disable-next-line @typescript-eslint/ban-types
19
-
18
+ const beforeDevServer = (0, _plugin.createParallelWorkflow)();
20
19
  const setupCompiler = (0, _plugin.createParallelWorkflow)();
21
- const postDevServerInit = (0, _plugin.createParallelWorkflow)(); // TODO FIXME
22
- // eslint-disable-next-line @typescript-eslint/ban-types
20
+ const afterDevServer = (0, _plugin.createParallelWorkflow)(); // TODO FIXME
23
21
 
24
22
  const beforeRouteSet = (0, _plugin.createAsyncPipeline)();
25
23
  const afterRouteSet = (0, _plugin.createAsyncPipeline)();
@@ -33,10 +31,8 @@ const extendContext = (0, _plugin.createAsyncPipeline)();
33
31
  const handleError = (0, _plugin.createParallelWorkflow)();
34
32
  const beforeMatch = (0, _plugin.createAsyncPipeline)();
35
33
  const afterMatch = (0, _plugin.createAsyncPipeline)(); // TODO FIXME
36
- // eslint-disable-next-line @typescript-eslint/ban-types
37
34
 
38
35
  const prefetch = (0, _plugin.createParallelWorkflow)(); // TODO FIXME
39
- // eslint-disable-next-line @typescript-eslint/ban-types
40
36
 
41
37
  const renderToString = (0, _plugin.createAsyncPipeline)();
42
38
  const beforeRender = (0, _plugin.createAsyncPipeline)();
@@ -44,16 +40,39 @@ const afterRender = (0, _plugin.createAsyncPipeline)();
44
40
  const beforeSend = (0, _plugin.createAsyncPipeline)();
45
41
  const afterSend = (0, _plugin.createParallelWorkflow)();
46
42
  const reset = (0, _plugin.createParallelWorkflow)();
43
+ const AppContext = (0, _plugin.createContext)({});
44
+ exports.AppContext = AppContext;
45
+ const ConfigContext = (0, _plugin.createContext)({});
46
+ /**
47
+ * Get original content of user config.
48
+ */
49
+
50
+ exports.ConfigContext = ConfigContext;
51
+
52
+ const useConfigContext = () => ConfigContext.use().value;
53
+ /**
54
+ * Get app context, including directories, plugins and some static infos.
55
+ */
56
+
57
+
58
+ exports.useConfigContext = useConfigContext;
47
59
 
48
- const createServerManager = () => (0, _plugin.createAsyncManager)({
60
+ const useAppContext = () => AppContext.use().value;
61
+
62
+ exports.useAppContext = useAppContext;
63
+ const pluginAPI = {
64
+ useAppContext,
65
+ useConfigContext
66
+ };
67
+ const serverHooks = {
49
68
  // server hook
50
69
  gather,
51
70
  create,
52
71
  prepareWebServer,
53
72
  prepareApiServer,
54
- preDevServerInit,
73
+ beforeDevServer,
55
74
  setupCompiler,
56
- postDevServerInit,
75
+ afterDevServer,
57
76
  beforeRouteSet,
58
77
  afterRouteSet,
59
78
  preServerInit,
@@ -74,24 +93,17 @@ const createServerManager = () => (0, _plugin.createAsyncManager)({
74
93
  beforeSend,
75
94
  afterSend,
76
95
  reset
77
- });
96
+ };
97
+ /** All hooks of server plugin. */
98
+
99
+ const createServerManager = () => (0, _plugin.createAsyncManager)(serverHooks, pluginAPI);
78
100
 
79
101
  exports.createServerManager = createServerManager;
80
102
  const serverManager = createServerManager();
103
+ /** Plugin options of a server plugin. */
104
+
81
105
  exports.serverManager = serverManager;
82
106
  const {
83
107
  createPlugin
84
108
  } = serverManager;
85
- exports.createPlugin = createPlugin;
86
- const AppContext = (0, _plugin.createContext)({});
87
- exports.AppContext = AppContext;
88
- const ConfigContext = (0, _plugin.createContext)({});
89
- exports.ConfigContext = ConfigContext;
90
-
91
- const useConfigContext = () => ConfigContext.use().value;
92
-
93
- exports.useConfigContext = useConfigContext;
94
-
95
- const useAppContext = () => AppContext.use().value;
96
-
97
- exports.useAppContext = useAppContext;
109
+ exports.createPlugin = createPlugin;
@@ -1,2 +1,3 @@
1
1
  export * from './plugin';
2
- export * from '@modern-js/plugin';
2
+ export * from '@modern-js/plugin';
3
+ export * from './loadPlugins';
@@ -0,0 +1,4 @@
1
+ import { ServerPlugin } from './plugin';
2
+ declare type Plugin = string | [string, any] | ServerPlugin;
3
+ export declare const loadPlugins: (plugins: Plugin[], appDirectory: string) => any[];
4
+ export {};
@@ -1,10 +1,10 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingMessage, ServerResponse } from 'http';
3
3
  import type { Component } from 'react';
4
- import { PluginFromAsyncManager } from '@modern-js/plugin';
4
+ import { CommonAPI, ToThreads, AsyncSetup, PluginOptions } from '@modern-js/plugin';
5
5
  import type { ModernServerContext, BaseSSRServerContext, Metrics, Logger } from '@modern-js/types/server';
6
6
  import type { NormalizedConfig, UserConfig } from '@modern-js/core';
7
- import type { IAppContext } from '@modern-js/types';
7
+ import type { ISAppContext } from '@modern-js/types';
8
8
  declare type ServerInitInput = {
9
9
  loggerOptions: any;
10
10
  metricsOptions: any;
@@ -26,13 +26,91 @@ export declare type APIServerStartInput = {
26
26
  middleware?: Array<any>;
27
27
  };
28
28
  };
29
- export declare type Route = {};
29
+ export declare type Route = Record<string, unknown>;
30
30
  export declare type RequestResult = {
31
31
  isfinish: boolean;
32
32
  };
33
- export declare type SSRServerContext = {};
34
- export declare type RenderContext = {};
35
- export declare const createServerManager: () => import("@modern-js/plugin").AsyncManager<{}, {
33
+ export declare type SSRServerContext = Record<string, unknown>;
34
+ export declare type RenderContext = Record<string, unknown>;
35
+ export declare const AppContext: import("@modern-js/plugin").Context<ISAppContext>;
36
+ export declare const ConfigContext: import("@modern-js/plugin").Context<UserConfig>;
37
+ /**
38
+ * Get original content of user config.
39
+ */
40
+
41
+ export declare const useConfigContext: () => UserConfig;
42
+ /**
43
+ * Get app context, including directories, plugins and some static infos.
44
+ */
45
+
46
+ export declare const useAppContext: () => ISAppContext;
47
+ declare const pluginAPI: {
48
+ useAppContext: () => ISAppContext;
49
+ useConfigContext: () => UserConfig;
50
+ };
51
+ declare const serverHooks: {
52
+ gather: import("@modern-js/plugin").ParallelWorkflow<{
53
+ addWebMiddleware: (_input: any) => void;
54
+ addAPIMiddleware: (_input: any) => void;
55
+ }, unknown>;
56
+ create: import("@modern-js/plugin").AsyncPipeline<ServerInitInput, InitExtension>;
57
+ prepareWebServer: import("@modern-js/plugin").AsyncPipeline<WebServerStartInput, Adapter>;
58
+ prepareApiServer: import("@modern-js/plugin").AsyncPipeline<APIServerStartInput, Adapter>;
59
+ beforeDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
60
+ setupCompiler: import("@modern-js/plugin").ParallelWorkflow<Record<string, unknown>, any[]>;
61
+ afterDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
62
+ beforeRouteSet: import("@modern-js/plugin").AsyncPipeline<Route[], Route[]>;
63
+ afterRouteSet: import("@modern-js/plugin").AsyncPipeline<unknown, unknown>;
64
+ preServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
65
+ postServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
66
+ listen: import("@modern-js/plugin").ParallelWorkflow<{
67
+ ip: string;
68
+ port: number;
69
+ }, any[]>;
70
+ beforeServerReset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
71
+ afterServerReset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
72
+ extendSSRContext: import("@modern-js/plugin").AsyncWaterfall<BaseSSRServerContext>;
73
+ extendContext: import("@modern-js/plugin").AsyncPipeline<ModernServerContext, ModernServerContext>;
74
+ handleError: import("@modern-js/plugin").ParallelWorkflow<{
75
+ error: Error;
76
+ }, unknown>;
77
+ beforeMatch: import("@modern-js/plugin").AsyncPipeline<{
78
+ context: ModernServerContext;
79
+ }, any>;
80
+ afterMatch: import("@modern-js/plugin").AsyncPipeline<{
81
+ context: ModernServerContext;
82
+ routeAPI: any;
83
+ }, any>;
84
+ prefetch: import("@modern-js/plugin").ParallelWorkflow<{
85
+ context: SSRServerContext;
86
+ }, unknown>;
87
+ renderToString: import("@modern-js/plugin").AsyncPipeline<{
88
+ App: Component;
89
+ context: RenderContext;
90
+ }, string>;
91
+ beforeRender: import("@modern-js/plugin").AsyncPipeline<{
92
+ context: ModernServerContext;
93
+ }, any>;
94
+ afterRender: import("@modern-js/plugin").AsyncPipeline<{
95
+ context: ModernServerContext;
96
+ templateAPI: any;
97
+ }, any>;
98
+ beforeSend: import("@modern-js/plugin").AsyncPipeline<ModernServerContext, RequestResult>;
99
+ afterSend: import("@modern-js/plugin").ParallelWorkflow<{
100
+ context: ModernServerContext;
101
+ }, unknown>;
102
+ reset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
103
+ };
104
+ /** All hooks of server plugin. */
105
+
106
+ export declare type ServerHooks = typeof serverHooks;
107
+ /** All hook callbacks of server plugin. */
108
+
109
+ export declare type ServerHookCallbacks = ToThreads<ServerHooks>;
110
+ /** All apis for server plugin. */
111
+
112
+ export declare type PluginAPI = typeof pluginAPI & CommonAPI<ServerHooks>;
113
+ export declare const createServerManager: () => import("@modern-js/plugin").AsyncManager<{
36
114
  gather: import("@modern-js/plugin").ParallelWorkflow<{
37
115
  addWebMiddleware: (_input: any) => void;
38
116
  addAPIMiddleware: (_input: any) => void;
@@ -40,9 +118,9 @@ export declare const createServerManager: () => import("@modern-js/plugin").Asyn
40
118
  create: import("@modern-js/plugin").AsyncPipeline<ServerInitInput, InitExtension>;
41
119
  prepareWebServer: import("@modern-js/plugin").AsyncPipeline<WebServerStartInput, Adapter>;
42
120
  prepareApiServer: import("@modern-js/plugin").AsyncPipeline<APIServerStartInput, Adapter>;
43
- preDevServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
44
- setupCompiler: import("@modern-js/plugin").ParallelWorkflow<{}, any[]>;
45
- postDevServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
121
+ beforeDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
122
+ setupCompiler: import("@modern-js/plugin").ParallelWorkflow<Record<string, unknown>, any[]>;
123
+ afterDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
46
124
  beforeRouteSet: import("@modern-js/plugin").AsyncPipeline<Route[], Route[]>;
47
125
  afterRouteSet: import("@modern-js/plugin").AsyncPipeline<unknown, unknown>;
48
126
  preServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
@@ -84,8 +162,11 @@ export declare const createServerManager: () => import("@modern-js/plugin").Asyn
84
162
  context: ModernServerContext;
85
163
  }, unknown>;
86
164
  reset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
165
+ }, {
166
+ useAppContext: () => ISAppContext;
167
+ useConfigContext: () => UserConfig;
87
168
  }>;
88
- export declare const serverManager: import("@modern-js/plugin").AsyncManager<{}, {
169
+ export declare const serverManager: import("@modern-js/plugin").AsyncManager<{
89
170
  gather: import("@modern-js/plugin").ParallelWorkflow<{
90
171
  addWebMiddleware: (_input: any) => void;
91
172
  addAPIMiddleware: (_input: any) => void;
@@ -93,9 +174,9 @@ export declare const serverManager: import("@modern-js/plugin").AsyncManager<{},
93
174
  create: import("@modern-js/plugin").AsyncPipeline<ServerInitInput, InitExtension>;
94
175
  prepareWebServer: import("@modern-js/plugin").AsyncPipeline<WebServerStartInput, Adapter>;
95
176
  prepareApiServer: import("@modern-js/plugin").AsyncPipeline<APIServerStartInput, Adapter>;
96
- preDevServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
97
- setupCompiler: import("@modern-js/plugin").ParallelWorkflow<{}, any[]>;
98
- postDevServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
177
+ beforeDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
178
+ setupCompiler: import("@modern-js/plugin").ParallelWorkflow<Record<string, unknown>, any[]>;
179
+ afterDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
99
180
  beforeRouteSet: import("@modern-js/plugin").AsyncPipeline<Route[], Route[]>;
100
181
  afterRouteSet: import("@modern-js/plugin").AsyncPipeline<unknown, unknown>;
101
182
  preServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
@@ -137,9 +218,14 @@ export declare const serverManager: import("@modern-js/plugin").AsyncManager<{},
137
218
  context: ModernServerContext;
138
219
  }, unknown>;
139
220
  reset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
221
+ }, {
222
+ useAppContext: () => ISAppContext;
223
+ useConfigContext: () => UserConfig;
140
224
  }>;
141
- export declare type ServerPlugin = PluginFromAsyncManager<typeof serverManager>;
142
- export declare const createPlugin: (initializer: import("@modern-js/plugin").AsyncInitializer<Partial<import("@modern-js/plugin").Progresses2Threads<{
225
+ /** Plugin options of a server plugin. */
226
+
227
+ export declare type ServerPlugin = PluginOptions<ServerHooks, AsyncSetup<ServerHooks, PluginAPI>>;
228
+ export declare const createPlugin: (setup?: AsyncSetup<{
143
229
  gather: import("@modern-js/plugin").ParallelWorkflow<{
144
230
  addWebMiddleware: (_input: any) => void;
145
231
  addAPIMiddleware: (_input: any) => void;
@@ -147,9 +233,9 @@ export declare const createPlugin: (initializer: import("@modern-js/plugin").Asy
147
233
  create: import("@modern-js/plugin").AsyncPipeline<ServerInitInput, InitExtension>;
148
234
  prepareWebServer: import("@modern-js/plugin").AsyncPipeline<WebServerStartInput, Adapter>;
149
235
  prepareApiServer: import("@modern-js/plugin").AsyncPipeline<APIServerStartInput, Adapter>;
150
- preDevServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
151
- setupCompiler: import("@modern-js/plugin").ParallelWorkflow<{}, any[]>;
152
- postDevServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
236
+ beforeDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
237
+ setupCompiler: import("@modern-js/plugin").ParallelWorkflow<Record<string, unknown>, any[]>;
238
+ afterDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
153
239
  beforeRouteSet: import("@modern-js/plugin").AsyncPipeline<Route[], Route[]>;
154
240
  afterRouteSet: import("@modern-js/plugin").AsyncPipeline<unknown, unknown>;
155
241
  preServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
@@ -191,7 +277,10 @@ export declare const createPlugin: (initializer: import("@modern-js/plugin").Asy
191
277
  context: ModernServerContext;
192
278
  }, unknown>;
193
279
  reset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
194
- } & import("@modern-js/plugin").ClearDraftProgress<{}>>>>, options?: import("@modern-js/plugin").PluginOptions | undefined) => import("@modern-js/plugin").AsyncPlugin<Partial<import("@modern-js/plugin").Progresses2Threads<{
280
+ }, {
281
+ useAppContext: () => ISAppContext;
282
+ useConfigContext: () => UserConfig;
283
+ }> | undefined, options?: PluginOptions<{
195
284
  gather: import("@modern-js/plugin").ParallelWorkflow<{
196
285
  addWebMiddleware: (_input: any) => void;
197
286
  addAPIMiddleware: (_input: any) => void;
@@ -199,9 +288,9 @@ export declare const createPlugin: (initializer: import("@modern-js/plugin").Asy
199
288
  create: import("@modern-js/plugin").AsyncPipeline<ServerInitInput, InitExtension>;
200
289
  prepareWebServer: import("@modern-js/plugin").AsyncPipeline<WebServerStartInput, Adapter>;
201
290
  prepareApiServer: import("@modern-js/plugin").AsyncPipeline<APIServerStartInput, Adapter>;
202
- preDevServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
203
- setupCompiler: import("@modern-js/plugin").ParallelWorkflow<{}, any[]>;
204
- postDevServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
291
+ beforeDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
292
+ setupCompiler: import("@modern-js/plugin").ParallelWorkflow<Record<string, unknown>, any[]>;
293
+ afterDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
205
294
  beforeRouteSet: import("@modern-js/plugin").AsyncPipeline<Route[], Route[]>;
206
295
  afterRouteSet: import("@modern-js/plugin").AsyncPipeline<unknown, unknown>;
207
296
  preServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
@@ -243,9 +332,115 @@ export declare const createPlugin: (initializer: import("@modern-js/plugin").Asy
243
332
  context: ModernServerContext;
244
333
  }, unknown>;
245
334
  reset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
246
- } & import("@modern-js/plugin").ClearDraftProgress<{}>>>>;
247
- export declare const AppContext: import("@modern-js/plugin").Context<IAppContext>;
248
- export declare const ConfigContext: import("@modern-js/plugin").Context<UserConfig>;
249
- export declare const useConfigContext: () => UserConfig;
250
- export declare const useAppContext: () => IAppContext;
335
+ }, AsyncSetup<{
336
+ gather: import("@modern-js/plugin").ParallelWorkflow<{
337
+ addWebMiddleware: (_input: any) => void;
338
+ addAPIMiddleware: (_input: any) => void;
339
+ }, unknown>;
340
+ create: import("@modern-js/plugin").AsyncPipeline<ServerInitInput, InitExtension>;
341
+ prepareWebServer: import("@modern-js/plugin").AsyncPipeline<WebServerStartInput, Adapter>;
342
+ prepareApiServer: import("@modern-js/plugin").AsyncPipeline<APIServerStartInput, Adapter>;
343
+ beforeDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
344
+ setupCompiler: import("@modern-js/plugin").ParallelWorkflow<Record<string, unknown>, any[]>;
345
+ afterDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
346
+ beforeRouteSet: import("@modern-js/plugin").AsyncPipeline<Route[], Route[]>;
347
+ afterRouteSet: import("@modern-js/plugin").AsyncPipeline<unknown, unknown>;
348
+ preServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
349
+ postServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
350
+ listen: import("@modern-js/plugin").ParallelWorkflow<{
351
+ ip: string;
352
+ port: number;
353
+ }, any[]>;
354
+ beforeServerReset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
355
+ afterServerReset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
356
+ extendSSRContext: import("@modern-js/plugin").AsyncWaterfall<BaseSSRServerContext>;
357
+ extendContext: import("@modern-js/plugin").AsyncPipeline<ModernServerContext, ModernServerContext>;
358
+ handleError: import("@modern-js/plugin").ParallelWorkflow<{
359
+ error: Error;
360
+ }, unknown>;
361
+ beforeMatch: import("@modern-js/plugin").AsyncPipeline<{
362
+ context: ModernServerContext;
363
+ }, any>;
364
+ afterMatch: import("@modern-js/plugin").AsyncPipeline<{
365
+ context: ModernServerContext;
366
+ routeAPI: any;
367
+ }, any>;
368
+ prefetch: import("@modern-js/plugin").ParallelWorkflow<{
369
+ context: SSRServerContext;
370
+ }, unknown>;
371
+ renderToString: import("@modern-js/plugin").AsyncPipeline<{
372
+ App: Component;
373
+ context: RenderContext;
374
+ }, string>;
375
+ beforeRender: import("@modern-js/plugin").AsyncPipeline<{
376
+ context: ModernServerContext;
377
+ }, any>;
378
+ afterRender: import("@modern-js/plugin").AsyncPipeline<{
379
+ context: ModernServerContext;
380
+ templateAPI: any;
381
+ }, any>;
382
+ beforeSend: import("@modern-js/plugin").AsyncPipeline<ModernServerContext, RequestResult>;
383
+ afterSend: import("@modern-js/plugin").ParallelWorkflow<{
384
+ context: ModernServerContext;
385
+ }, unknown>;
386
+ reset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
387
+ }, {
388
+ useAppContext: () => ISAppContext;
389
+ useConfigContext: () => UserConfig;
390
+ }>> | undefined) => import("@modern-js/plugin").AsyncPlugin<{
391
+ gather: import("@modern-js/plugin").ParallelWorkflow<{
392
+ addWebMiddleware: (_input: any) => void;
393
+ addAPIMiddleware: (_input: any) => void;
394
+ }, unknown>;
395
+ create: import("@modern-js/plugin").AsyncPipeline<ServerInitInput, InitExtension>;
396
+ prepareWebServer: import("@modern-js/plugin").AsyncPipeline<WebServerStartInput, Adapter>;
397
+ prepareApiServer: import("@modern-js/plugin").AsyncPipeline<APIServerStartInput, Adapter>;
398
+ beforeDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
399
+ setupCompiler: import("@modern-js/plugin").ParallelWorkflow<Record<string, unknown>, any[]>;
400
+ afterDevServer: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
401
+ beforeRouteSet: import("@modern-js/plugin").AsyncPipeline<Route[], Route[]>;
402
+ afterRouteSet: import("@modern-js/plugin").AsyncPipeline<unknown, unknown>;
403
+ preServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
404
+ postServerInit: import("@modern-js/plugin").ParallelWorkflow<NormalizedConfig, any>;
405
+ listen: import("@modern-js/plugin").ParallelWorkflow<{
406
+ ip: string;
407
+ port: number;
408
+ }, any[]>;
409
+ beforeServerReset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
410
+ afterServerReset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
411
+ extendSSRContext: import("@modern-js/plugin").AsyncWaterfall<BaseSSRServerContext>;
412
+ extendContext: import("@modern-js/plugin").AsyncPipeline<ModernServerContext, ModernServerContext>;
413
+ handleError: import("@modern-js/plugin").ParallelWorkflow<{
414
+ error: Error;
415
+ }, unknown>;
416
+ beforeMatch: import("@modern-js/plugin").AsyncPipeline<{
417
+ context: ModernServerContext;
418
+ }, any>;
419
+ afterMatch: import("@modern-js/plugin").AsyncPipeline<{
420
+ context: ModernServerContext;
421
+ routeAPI: any;
422
+ }, any>;
423
+ prefetch: import("@modern-js/plugin").ParallelWorkflow<{
424
+ context: SSRServerContext;
425
+ }, unknown>;
426
+ renderToString: import("@modern-js/plugin").AsyncPipeline<{
427
+ App: Component;
428
+ context: RenderContext;
429
+ }, string>;
430
+ beforeRender: import("@modern-js/plugin").AsyncPipeline<{
431
+ context: ModernServerContext;
432
+ }, any>;
433
+ afterRender: import("@modern-js/plugin").AsyncPipeline<{
434
+ context: ModernServerContext;
435
+ templateAPI: any;
436
+ }, any>;
437
+ beforeSend: import("@modern-js/plugin").AsyncPipeline<ModernServerContext, RequestResult>;
438
+ afterSend: import("@modern-js/plugin").ParallelWorkflow<{
439
+ context: ModernServerContext;
440
+ }, unknown>;
441
+ reset: import("@modern-js/plugin").ParallelWorkflow<void, unknown>;
442
+ }, {
443
+ useAppContext: () => ISAppContext;
444
+ useConfigContext: () => UserConfig;
445
+ }>;
251
446
  export {};
package/jest.config.js CHANGED
@@ -2,7 +2,6 @@ const sharedConfig = require('@scripts/jest-config');
2
2
 
3
3
  /** @type {import('@jest/types').Config.InitialOptions} */
4
4
  module.exports = {
5
- // eslint-disable-next-line node/no-unsupported-features/es-syntax
6
5
  ...sharedConfig,
7
6
  rootDir: __dirname,
8
7
  };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.2",
14
+ "version": "1.2.5",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -28,11 +28,12 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@modern-js/plugin": "^1.2.1"
31
+ "@modern-js/plugin": "^1.3.2",
32
+ "@modern-js/utils": "^1.3.7"
32
33
  },
33
34
  "devDependencies": {
34
- "@modern-js/core": "^1.4.0",
35
- "@modern-js/types": "^1.3.0",
35
+ "@modern-js/core": "1.6.1",
36
+ "@modern-js/types": "^1.3.6",
36
37
  "@types/jest": "^26",
37
38
  "@types/node": "^14",
38
39
  "@types/react": "^17",
@@ -51,8 +52,7 @@
51
52
  },
52
53
  "publishConfig": {
53
54
  "registry": "https://registry.npmjs.org/",
54
- "access": "public",
55
- "types": "./dist/types/index.d.ts"
55
+ "access": "public"
56
56
  },
57
57
  "scripts": {
58
58
  "new": "modern new",
@@ -0,0 +1,3 @@
1
+ module.exports = () => ({
2
+ name: 'test-a',
3
+ });
@@ -0,0 +1,3 @@
1
+ {
2
+ "name": "test-a"
3
+ }
@@ -0,0 +1,33 @@
1
+ import path from 'path';
2
+ import { loadPlugins } from '../src/loadPlugins';
3
+
4
+ const modulePath = path.join(__dirname, './fixtures/load-plugins');
5
+ describe('test load plugin', () => {
6
+ it('should load string plugin correctly', () => {
7
+ const loaded = loadPlugins(['test-a'], modulePath);
8
+ expect(loaded[0].pluginPath).toBe(
9
+ path.join(__dirname, './fixtures/load-plugins/test-a/index.js'),
10
+ );
11
+ });
12
+
13
+ it('should load plugin instance correctly', () => {
14
+ const loaded = loadPlugins(
15
+ [
16
+ {
17
+ name: 'modern',
18
+ },
19
+ ],
20
+ modulePath,
21
+ );
22
+
23
+ expect(loaded[0].name).toBe('modern');
24
+ });
25
+
26
+ it('should throw error if plugin not found', () => {
27
+ try {
28
+ loadPlugins(['test-b'], modulePath);
29
+ } catch (e: any) {
30
+ expect(e.message).toMatch('Can not find plugin test-b.');
31
+ }
32
+ });
33
+ });