@modern-js/core 1.4.5 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.js ADDED
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ['@modern-js'],
4
+ parserOptions: {
5
+ tsconfigRootDir: __dirname,
6
+ project: ['./tsconfig.json'],
7
+ },
8
+ };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,53 @@
1
1
  # @modern-js/core
2
2
 
3
+ ## 1.6.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 4e2026e4: feat: support new plugin config
8
+
9
+ ### Patch Changes
10
+
11
+ - 05ce88a0: fix: set default value for type NormalizedConfig to ensure all config keys are required
12
+ - a8df060e: support setup dev middleware first step
13
+ - 6a7acb81: modify devServer type and name
14
+ - Updated dependencies [c2046f37]
15
+ - Updated dependencies [dc88abf9]
16
+ - Updated dependencies [0462ff77]
17
+ - @modern-js/utils@1.3.6
18
+ - @modern-js/plugin@1.3.2
19
+
20
+ ## 1.5.0
21
+
22
+ ### Minor Changes
23
+
24
+ - 80d8ddfe: feat: add `CliPlugin` type to define new plugin
25
+
26
+ ### Patch Changes
27
+
28
+ - 80d3cfb7: fix: server.metrics type
29
+ - 42c6b136: feat: support api.setAppContext
30
+ - 4e7dcbd5: fix: server.logger type
31
+ - 9e8bc4ab: fix: server.routes type
32
+ - 0c556e59: fix: tools.less type
33
+ - 2008fdbd: convert two packages server part, support server load plugin itself
34
+ - Updated dependencies [5bf5868d]
35
+ - Updated dependencies [80d8ddfe]
36
+ - Updated dependencies [491145e3]
37
+ - @modern-js/utils@1.3.5
38
+ - @modern-js/plugin@1.3.0
39
+
40
+ ## 1.4.6
41
+
42
+ ### Patch Changes
43
+
44
+ - cc5e8001: fix: load plugins
45
+ - 2520ea86: fix: garfish schema
46
+ - e81fd9b7: fix: update "server.metrics" type
47
+ - 1c411e71: fix: mergeConfig util function
48
+ - Updated dependencies [db43dce6]
49
+ - @modern-js/utils@1.3.4
50
+
3
51
  ## 1.4.4
4
52
 
5
53
  ### Patch Changes
@@ -96,5 +96,8 @@ export const defaults = {
96
96
  server: serverDefaults,
97
97
  dev: devDefaults,
98
98
  deploy: deployDefaults,
99
- tools: toolsDefaults
99
+ tools: toolsDefaults,
100
+ plugins: [],
101
+ runtime: {},
102
+ runtimeByEntries: {}
100
103
  };
@@ -155,10 +155,10 @@ export const server = {
155
155
  instanceof: 'Function'
156
156
  },
157
157
  logger: {
158
- type: 'object'
158
+ type: ['object', 'boolean']
159
159
  },
160
160
  metrics: {
161
- type: 'object'
161
+ type: ['object', 'boolean']
162
162
  },
163
163
  proxy: {
164
164
  type: 'object'
@@ -4,8 +4,26 @@ import address from 'address';
4
4
  export const AppContext = createContext({});
5
5
  export const ConfigContext = createContext({});
6
6
  export const ResolvedConfigContext = createContext({});
7
+ /**
8
+ * Set app context.
9
+ * @param value new app context. It will override previous app context.
10
+ */
11
+
12
+ export const setAppContext = value => AppContext.set(value);
13
+ /**
14
+ * Get app context, including directories, plugins and some static infos.
15
+ */
16
+
7
17
  export const useAppContext = () => AppContext.use().value;
18
+ /**
19
+ * Get original content of user config.
20
+ */
21
+
8
22
  export const useConfigContext = () => ConfigContext.use().value;
23
+ /**
24
+ * Get normalized content of user config.
25
+ */
26
+
9
27
  export const useResolvedConfigContext = () => ResolvedConfigContext.use().value;
10
28
  export const initAppContext = (appDirectory, plugins, configFile, options) => {
11
29
  const {
@@ -5,41 +5,23 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
5
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
6
 
7
7
  import path from 'path';
8
- import { compatRequire, pkgUp, ensureAbsolutePath, logger } from '@modern-js/utils';
9
- import { createAsyncManager, createAsyncWorkflow, createParallelWorkflow, createAsyncWaterfall } from '@modern-js/plugin';
8
+ import { pkgUp, ensureAbsolutePath, logger } from '@modern-js/utils';
10
9
  import { enable } from '@modern-js/plugin/node';
11
10
  import { program } from "./utils/commander";
12
11
  import { resolveConfig, loadUserConfig } from "./config";
13
12
  import { loadPlugins } from "./loadPlugins";
14
- import { AppContext, ConfigContext, initAppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from "./context";
13
+ import { AppContext, ConfigContext, initAppContext, ResolvedConfigContext } from "./context";
15
14
  import { initWatcher } from "./initWatcher";
16
15
  import { loadEnv } from "./loadEnv";
16
+ import { manager } from "./manager";
17
17
  export * from "./config";
18
18
  export * from '@modern-js/plugin';
19
- export * from '@modern-js/plugin/node';
19
+ export * from '@modern-js/plugin/node'; // TODO: remove export after refactor all plugins
20
+
21
+ export { manager, mountHook, usePlugins, createPlugin, registerHook } from "./manager";
22
+ // TODO: remove export after refactor all plugins
23
+ export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from "./pluginAPI";
20
24
  program.name('modern').usage('<command> [options]').version(process.env.MODERN_JS_VERSION || '0.1.0');
21
- const hooksMap = {
22
- config: createParallelWorkflow(),
23
- resolvedConfig: createAsyncWaterfall(),
24
- validateSchema: createParallelWorkflow(),
25
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
26
- prepare: createAsyncWorkflow(),
27
- commands: createAsyncWorkflow(),
28
- watchFiles: createParallelWorkflow(),
29
- fileChange: createAsyncWorkflow(),
30
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
31
- beforeExit: createAsyncWorkflow(),
32
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
33
- beforeRestart: createAsyncWorkflow()
34
- };
35
- export const manager = createAsyncManager(hooksMap);
36
- export const {
37
- createPlugin,
38
- registe: registerHook,
39
- useRunner: mountHook
40
- } = manager;
41
- export const usePlugins = plugins => plugins.forEach(plugin => manager.usePlugin(compatRequire(require.resolve(plugin))));
42
- export { AppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext, ConfigContext };
43
25
 
44
26
  const initAppDir = async cwd => {
45
27
  if (!cwd) {
@@ -75,8 +57,7 @@ const createCli = () => {
75
57
  loadEnv(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
76
58
  const loaded = await loadUserConfig(appDirectory, options === null || options === void 0 ? void 0 : options.configFile, options === null || options === void 0 ? void 0 : options.packageJsonConfig);
77
59
  const plugins = loadPlugins(appDirectory, loaded.config, {
78
- internalPlugins: options === null || options === void 0 ? void 0 : options.plugins,
79
- transformPlugin: options === null || options === void 0 ? void 0 : options.transformPlugin
60
+ internalPlugins: options === null || options === void 0 ? void 0 : options.plugins
80
61
  });
81
62
  plugins.forEach(plugin => plugin.cli && manager.usePlugin(plugin.cli));
82
63
  const appContext = initAppContext(appDirectory, plugins, loaded.filePath, options === null || options === void 0 ? void 0 : options.options);
@@ -1,10 +1,5 @@
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
1
  import { isDepExists, createDebugger, compatRequire, INTERNAL_PLUGINS } from '@modern-js/utils';
2
+ import { createPlugin } from "./manager";
8
3
  const debug = createDebugger('load-plugins');
9
4
 
10
5
  /**
@@ -32,7 +27,7 @@ const tryResolve = (name, appDirectory) => {
32
27
  return filePath;
33
28
  };
34
29
 
35
- export function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
30
+ export function getAppPlugins(appDirectory, oldPluginConfig, internalPlugins) {
36
31
  const allPlugins = internalPlugins || INTERNAL_PLUGINS;
37
32
  const appPlugins = [...Object.keys(allPlugins).filter(name => {
38
33
  const config = allPlugins[name];
@@ -43,45 +38,44 @@ export function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
43
38
  }
44
39
 
45
40
  return isDepExists(appDirectory, name);
46
- }).map(name => allPlugins[name]), ...pluginConfig];
41
+ }).map(name => allPlugins[name]), ...oldPluginConfig];
47
42
  return appPlugins;
48
43
  }
44
+
45
+ const resolveCliPlugin = (p, appDirectory) => {
46
+ const pkg = typeof p === 'string' ? p : p[0];
47
+ const path = tryResolve(pkg, appDirectory);
48
+ const module = compatRequire(path);
49
+
50
+ if (typeof module === 'function') {
51
+ const pluginOptions = Array.isArray(p) ? p[1] : undefined;
52
+ const result = module(pluginOptions);
53
+ return createPlugin(result.setup, result);
54
+ }
55
+
56
+ return module;
57
+ };
58
+
59
+ const isOldPluginConfig = config => Array.isArray(config) && config.some(item => {
60
+ if (typeof item === 'string' || Array.isArray(item)) {
61
+ return true;
62
+ }
63
+
64
+ return 'cli' in item || 'server' in item;
65
+ });
49
66
  /**
50
67
  * Load internal plugins which in @modern-js scope and user's custom plugins.
51
68
  * @param appDirectory - Application root directory.
52
69
  * @param userConfig - Resolved user config.
53
70
  * @param options.internalPlugins - Internal plugins.
54
- * @param options.transformPlugin - transform plugin before using it.
55
71
  * @returns Plugin Objects has been required.
56
72
  */
57
73
 
58
- export const loadPlugins = (appDirectory, userConfig, options = {}) => {
59
- const {
60
- internalPlugins,
61
- transformPlugin
62
- } = options;
63
-
64
- const resolvePlugin = p => {
65
- const pkg = typeof p === 'string' ? p : p[0];
66
- const path = tryResolve(pkg, appDirectory);
67
- let module = compatRequire(path);
68
- const pluginOptions = Array.isArray(p) ? p[1] : undefined;
69
-
70
- if (transformPlugin) {
71
- module = transformPlugin(module, userConfig, pluginOptions);
72
- } else {
73
- module = typeof module === 'function' ? module(pluginOptions) : module;
74
- }
75
-
76
- return {
77
- pkg,
78
- path,
79
- module
80
- };
81
- };
82
74
 
83
- const plugins = getAppPlugins(appDirectory, userConfig.plugins || [], internalPlugins);
84
- return plugins.map(plugin => {
75
+ export const loadPlugins = (appDirectory, userConfig, options = {}) => {
76
+ const pluginConfig = userConfig.plugins;
77
+ const plugins = getAppPlugins(appDirectory, isOldPluginConfig(pluginConfig) ? pluginConfig : [], options.internalPlugins);
78
+ const loadedPlugins = plugins.map(plugin => {
85
79
  const _plugin = typeof plugin === 'string' || Array.isArray(plugin) ? {
86
80
  cli: plugin
87
81
  } : plugin;
@@ -93,23 +87,12 @@ export const loadPlugins = (appDirectory, userConfig, options = {}) => {
93
87
  const loadedPlugin = {};
94
88
 
95
89
  if (cli) {
96
- const {
97
- pkg,
98
- path,
99
- module
100
- } = resolvePlugin(cli);
101
- loadedPlugin.cli = _objectSpread(_objectSpread({}, module), {}, {
102
- pluginPath: path
103
- });
104
- loadedPlugin.cliPkg = pkg;
90
+ loadedPlugin.cli = resolveCliPlugin(cli, appDirectory);
105
91
  } // server plugins don't support to accept params
106
92
 
107
93
 
108
94
  if (server && typeof server === 'string') {
109
- const path = tryResolve(server, appDirectory);
110
- loadedPlugin.server = {
111
- pluginPath: path
112
- };
95
+ loadedPlugin.server = server;
113
96
  loadedPlugin.serverPkg = server;
114
97
  }
115
98
 
@@ -119,4 +102,16 @@ export const loadPlugins = (appDirectory, userConfig, options = {}) => {
119
102
  });
120
103
  return loadedPlugin;
121
104
  });
105
+
106
+ if (!isOldPluginConfig(pluginConfig)) {
107
+ const cliPlugins = Array.isArray(pluginConfig) ? pluginConfig : pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.cli;
108
+
109
+ if (cliPlugins !== null && cliPlugins !== void 0 && cliPlugins.length) {
110
+ loadedPlugins.push(...cliPlugins.map(item => ({
111
+ cli: createPlugin(item.setup, item)
112
+ })));
113
+ }
114
+ }
115
+
116
+ return loadedPlugins;
122
117
  };
@@ -0,0 +1,28 @@
1
+ import { createAsyncManager, createAsyncWorkflow, createAsyncWaterfall, createParallelWorkflow } from '@modern-js/plugin';
2
+ import { compatRequire } from '@modern-js/utils';
3
+ import { pluginAPI } from "./pluginAPI";
4
+ const baseHooks = {
5
+ config: createParallelWorkflow(),
6
+ resolvedConfig: createAsyncWaterfall(),
7
+ validateSchema: createParallelWorkflow(),
8
+ prepare: createAsyncWorkflow(),
9
+ commands: createAsyncWorkflow(),
10
+ watchFiles: createParallelWorkflow(),
11
+ fileChange: createAsyncWorkflow(),
12
+ beforeExit: createAsyncWorkflow(),
13
+ beforeRestart: createAsyncWorkflow()
14
+ };
15
+ /** All hooks of cli plugin. */
16
+
17
+ export const manager = createAsyncManager(baseHooks, pluginAPI);
18
+ /** Plugin options of a cli plugin. */
19
+
20
+ export const {
21
+ createPlugin,
22
+ registerHook,
23
+ useRunner: mountHook
24
+ } = manager;
25
+ export const usePlugins = plugins => plugins.forEach(pluginPath => {
26
+ const module = compatRequire(require.resolve(pluginPath));
27
+ manager.usePlugin(module);
28
+ });
@@ -0,0 +1,11 @@
1
+ import { AppContext, ConfigContext, setAppContext, useAppContext, useConfigContext, ResolvedConfigContext, useResolvedConfigContext } from "./context";
2
+ export const pluginAPI = {
3
+ setAppContext,
4
+ useAppContext,
5
+ useConfigContext,
6
+ useResolvedConfigContext
7
+ };
8
+ /** all apis for cli plugin */
9
+
10
+ // TODO: only export types after refactor all plugins
11
+ export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext };
@@ -102,6 +102,9 @@ const defaults = {
102
102
  server: serverDefaults,
103
103
  dev: devDefaults,
104
104
  deploy: deployDefaults,
105
- tools: toolsDefaults
105
+ tools: toolsDefaults,
106
+ plugins: [],
107
+ runtime: {},
108
+ runtimeByEntries: {}
106
109
  };
107
110
  exports.defaults = defaults;
@@ -163,10 +163,10 @@ const server = {
163
163
  instanceof: 'Function'
164
164
  },
165
165
  logger: {
166
- type: 'object'
166
+ type: ['object', 'boolean']
167
167
  },
168
168
  metrics: {
169
- type: 'object'
169
+ type: ['object', 'boolean']
170
170
  },
171
171
  proxy: {
172
172
  type: 'object'
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.useResolvedConfigContext = exports.useConfigContext = exports.useAppContext = exports.initAppContext = exports.ResolvedConfigContext = exports.ConfigContext = exports.AppContext = void 0;
6
+ exports.useResolvedConfigContext = exports.useConfigContext = exports.useAppContext = exports.setAppContext = exports.initAppContext = exports.ResolvedConfigContext = exports.ConfigContext = exports.AppContext = void 0;
7
7
 
8
8
  var _path = _interopRequireDefault(require("path"));
9
9
 
@@ -18,13 +18,34 @@ exports.AppContext = AppContext;
18
18
  const ConfigContext = (0, _plugin.createContext)({});
19
19
  exports.ConfigContext = ConfigContext;
20
20
  const ResolvedConfigContext = (0, _plugin.createContext)({});
21
+ /**
22
+ * Set app context.
23
+ * @param value new app context. It will override previous app context.
24
+ */
25
+
21
26
  exports.ResolvedConfigContext = ResolvedConfigContext;
22
27
 
28
+ const setAppContext = value => AppContext.set(value);
29
+ /**
30
+ * Get app context, including directories, plugins and some static infos.
31
+ */
32
+
33
+
34
+ exports.setAppContext = setAppContext;
35
+
23
36
  const useAppContext = () => AppContext.use().value;
37
+ /**
38
+ * Get original content of user config.
39
+ */
40
+
24
41
 
25
42
  exports.useAppContext = useAppContext;
26
43
 
27
44
  const useConfigContext = () => ConfigContext.use().value;
45
+ /**
46
+ * Get normalized content of user config.
47
+ */
48
+
28
49
 
29
50
  exports.useConfigContext = useConfigContext;
30
51