@modern-js/core 1.5.0 → 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 CHANGED
@@ -2,9 +2,7 @@ module.exports = {
2
2
  root: true,
3
3
  extends: ['@modern-js'],
4
4
  parserOptions: {
5
- // the base path used to search for `tsconfig.json`
6
5
  tsconfigRootDir: __dirname,
7
- // the relative path to sub-package's `tsconfig.json`
8
6
  project: ['./tsconfig.json'],
9
7
  },
10
8
  };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
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
+
3
20
  ## 1.5.0
4
21
 
5
22
  ### Minor 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
  };
@@ -1,9 +1,3 @@
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';
8
2
  import { createPlugin } from "./manager";
9
3
  const debug = createDebugger('load-plugins');
@@ -33,7 +27,7 @@ const tryResolve = (name, appDirectory) => {
33
27
  return filePath;
34
28
  };
35
29
 
36
- export function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
30
+ export function getAppPlugins(appDirectory, oldPluginConfig, internalPlugins) {
37
31
  const allPlugins = internalPlugins || INTERNAL_PLUGINS;
38
32
  const appPlugins = [...Object.keys(allPlugins).filter(name => {
39
33
  const config = allPlugins[name];
@@ -44,9 +38,31 @@ export function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
44
38
  }
45
39
 
46
40
  return isDepExists(appDirectory, name);
47
- }).map(name => allPlugins[name]), ...pluginConfig];
41
+ }).map(name => allPlugins[name]), ...oldPluginConfig];
48
42
  return appPlugins;
49
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
+ });
50
66
  /**
51
67
  * Load internal plugins which in @modern-js scope and user's custom plugins.
52
68
  * @param appDirectory - Application root directory.
@@ -55,31 +71,11 @@ export function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
55
71
  * @returns Plugin Objects has been required.
56
72
  */
57
73
 
58
- export const loadPlugins = (appDirectory, userConfig, options = {}) => {
59
- const {
60
- internalPlugins
61
- } = options;
62
-
63
- const resolvePlugin = p => {
64
- const pkg = typeof p === 'string' ? p : p[0];
65
- const path = tryResolve(pkg, appDirectory);
66
- let module = compatRequire(path);
67
- const pluginOptions = Array.isArray(p) ? p[1] : undefined;
68
-
69
- if (typeof module === 'function') {
70
- const plugin = module(pluginOptions);
71
- module = createPlugin(plugin.setup, plugin);
72
- }
73
-
74
- return {
75
- pkg,
76
- path,
77
- module
78
- };
79
- };
80
74
 
81
- const plugins = getAppPlugins(appDirectory, userConfig.plugins || [], internalPlugins);
82
- 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 => {
83
79
  const _plugin = typeof plugin === 'string' || Array.isArray(plugin) ? {
84
80
  cli: plugin
85
81
  } : plugin;
@@ -91,15 +87,7 @@ export const loadPlugins = (appDirectory, userConfig, options = {}) => {
91
87
  const loadedPlugin = {};
92
88
 
93
89
  if (cli) {
94
- const {
95
- pkg,
96
- path,
97
- module
98
- } = resolvePlugin(cli);
99
- loadedPlugin.cli = _objectSpread(_objectSpread({}, module), {}, {
100
- pluginPath: path
101
- });
102
- loadedPlugin.cliPkg = pkg;
90
+ loadedPlugin.cli = resolveCliPlugin(cli, appDirectory);
103
91
  } // server plugins don't support to accept params
104
92
 
105
93
 
@@ -114,4 +102,16 @@ export const loadPlugins = (appDirectory, userConfig, options = {}) => {
114
102
  });
115
103
  return loadedPlugin;
116
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;
117
117
  };
@@ -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;
@@ -10,12 +10,6 @@ var _utils = require("@modern-js/utils");
10
10
 
11
11
  var _manager = require("./manager");
12
12
 
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; }
14
-
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; }
16
-
17
- 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; }
18
-
19
13
  const debug = (0, _utils.createDebugger)('load-plugins');
20
14
 
21
15
  /**
@@ -43,7 +37,7 @@ const tryResolve = (name, appDirectory) => {
43
37
  return filePath;
44
38
  };
45
39
 
46
- function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
40
+ function getAppPlugins(appDirectory, oldPluginConfig, internalPlugins) {
47
41
  const allPlugins = internalPlugins || _utils.INTERNAL_PLUGINS;
48
42
  const appPlugins = [...Object.keys(allPlugins).filter(name => {
49
43
  const config = allPlugins[name];
@@ -54,9 +48,31 @@ function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
54
48
  }
55
49
 
56
50
  return (0, _utils.isDepExists)(appDirectory, name);
57
- }).map(name => allPlugins[name]), ...pluginConfig];
51
+ }).map(name => allPlugins[name]), ...oldPluginConfig];
58
52
  return appPlugins;
59
53
  }
54
+
55
+ const resolveCliPlugin = (p, appDirectory) => {
56
+ const pkg = typeof p === 'string' ? p : p[0];
57
+ const path = tryResolve(pkg, appDirectory);
58
+ const module = (0, _utils.compatRequire)(path);
59
+
60
+ if (typeof module === 'function') {
61
+ const pluginOptions = Array.isArray(p) ? p[1] : undefined;
62
+ const result = module(pluginOptions);
63
+ return (0, _manager.createPlugin)(result.setup, result);
64
+ }
65
+
66
+ return module;
67
+ };
68
+
69
+ const isOldPluginConfig = config => Array.isArray(config) && config.some(item => {
70
+ if (typeof item === 'string' || Array.isArray(item)) {
71
+ return true;
72
+ }
73
+
74
+ return 'cli' in item || 'server' in item;
75
+ });
60
76
  /**
61
77
  * Load internal plugins which in @modern-js scope and user's custom plugins.
62
78
  * @param appDirectory - Application root directory.
@@ -67,30 +83,9 @@ function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
67
83
 
68
84
 
69
85
  const loadPlugins = (appDirectory, userConfig, options = {}) => {
70
- const {
71
- internalPlugins
72
- } = options;
73
-
74
- const resolvePlugin = p => {
75
- const pkg = typeof p === 'string' ? p : p[0];
76
- const path = tryResolve(pkg, appDirectory);
77
- let module = (0, _utils.compatRequire)(path);
78
- const pluginOptions = Array.isArray(p) ? p[1] : undefined;
79
-
80
- if (typeof module === 'function') {
81
- const plugin = module(pluginOptions);
82
- module = (0, _manager.createPlugin)(plugin.setup, plugin);
83
- }
84
-
85
- return {
86
- pkg,
87
- path,
88
- module
89
- };
90
- };
91
-
92
- const plugins = getAppPlugins(appDirectory, userConfig.plugins || [], internalPlugins);
93
- return plugins.map(plugin => {
86
+ const pluginConfig = userConfig.plugins;
87
+ const plugins = getAppPlugins(appDirectory, isOldPluginConfig(pluginConfig) ? pluginConfig : [], options.internalPlugins);
88
+ const loadedPlugins = plugins.map(plugin => {
94
89
  const _plugin = typeof plugin === 'string' || Array.isArray(plugin) ? {
95
90
  cli: plugin
96
91
  } : plugin;
@@ -102,15 +97,7 @@ const loadPlugins = (appDirectory, userConfig, options = {}) => {
102
97
  const loadedPlugin = {};
103
98
 
104
99
  if (cli) {
105
- const {
106
- pkg,
107
- path,
108
- module
109
- } = resolvePlugin(cli);
110
- loadedPlugin.cli = _objectSpread(_objectSpread({}, module), {}, {
111
- pluginPath: path
112
- });
113
- loadedPlugin.cliPkg = pkg;
100
+ loadedPlugin.cli = resolveCliPlugin(cli, appDirectory);
114
101
  } // server plugins don't support to accept params
115
102
 
116
103
 
@@ -125,6 +112,18 @@ const loadPlugins = (appDirectory, userConfig, options = {}) => {
125
112
  });
126
113
  return loadedPlugin;
127
114
  });
115
+
116
+ if (!isOldPluginConfig(pluginConfig)) {
117
+ const cliPlugins = Array.isArray(pluginConfig) ? pluginConfig : pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.cli;
118
+
119
+ if (cliPlugins !== null && cliPlugins !== void 0 && cliPlugins.length) {
120
+ loadedPlugins.push(...cliPlugins.map(item => ({
121
+ cli: (0, _manager.createPlugin)(item.setup, item)
122
+ })));
123
+ }
124
+ }
125
+
126
+ return loadedPlugins;
128
127
  };
129
128
 
130
129
  exports.loadPlugins = loadPlugins;
@@ -22,4 +22,7 @@ export declare const defaults: {
22
22
  terser: undefined;
23
23
  minifyCss: undefined;
24
24
  };
25
+ plugins: never[];
26
+ runtime: {};
27
+ runtimeByEntries: {};
25
28
  };
@@ -1,5 +1,8 @@
1
+ /// <reference types="node" />
2
+ import http from 'http';
1
3
  import { ErrorObject } from 'ajv';
2
4
  import { MetaOptions } from '@modern-js/utils';
5
+ import type { NextFunction, ProxyOptions } from '@modern-js/types';
3
6
  import { PluginConfig } from '../loadPlugins';
4
7
  import { defaults } from './defaults';
5
8
  import { mergeConfig, NormalizedConfig } from './mergeConfig';
@@ -95,6 +98,14 @@ interface DeployConfig {
95
98
  domainByEntries?: Record<string, string | Array<string>>;
96
99
  }
97
100
  declare type ConfigFunction = Record<string, unknown> | ((config: Record<string, unknown>) => Record<string, unknown> | void);
101
+ declare type RequestHandler = (req: http.IncomingMessage, res: http.ServerResponse, next: NextFunction) => void;
102
+ declare type DevServerConfig = {
103
+ proxy?: ProxyOptions;
104
+ headers?: Record<string, string>;
105
+ before?: RequestHandler[];
106
+ after?: RequestHandler[];
107
+ [propsName: string]: any;
108
+ };
98
109
  interface ToolsConfig {
99
110
  webpack?: ConfigFunction;
100
111
  babel?: ConfigFunction;
@@ -102,7 +113,7 @@ interface ToolsConfig {
102
113
  postcss?: ConfigFunction;
103
114
  styledComponents?: ConfigFunction;
104
115
  lodash?: ConfigFunction;
105
- devServer?: Record<string, unknown>;
116
+ devServer?: DevServerConfig;
106
117
  tsLoader?: ConfigFunction;
107
118
  terser?: ConfigFunction;
108
119
  minifyCss?: ConfigFunction;
@@ -27,7 +27,7 @@ export declare const useConfigContext: () => UserConfig;
27
27
  */
28
28
 
29
29
  export declare const useResolvedConfigContext: () => NormalizedConfig;
30
- export declare const initAppContext: (appDirectory: string, plugins: Array<LoadedPlugin>, configFile: string | false, options?: {
30
+ export declare const initAppContext: (appDirectory: string, plugins: LoadedPlugin[], configFile: string | false, options?: {
31
31
  metaName?: string | undefined;
32
32
  srcDir?: string | undefined;
33
33
  distDir?: string | undefined;
@@ -1,18 +1,32 @@
1
1
  import { INTERNAL_PLUGINS } from '@modern-js/utils';
2
2
  import type { UserConfig } from './config';
3
- declare type Plugin = string | [string, any];
3
+ import { CliPlugin } from './manager';
4
+ declare type PluginItem = string | [string, any];
4
5
  export declare type LoadedPlugin = {
5
- cli?: any;
6
- cliPkg?: string;
7
- server?: any;
6
+ cli?: CliPlugin;
7
+ server?: string;
8
8
  serverPkg?: string;
9
9
  };
10
- export declare type PluginConfigItem = {
11
- cli?: Plugin;
12
- server?: Plugin;
13
- } | Plugin;
14
- export declare type PluginConfig = Array<PluginConfigItem>;
15
- export declare function getAppPlugins(appDirectory: string, pluginConfig: PluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): PluginConfigItem[];
10
+ /**
11
+ * @deprecated
12
+ * Using NewPluginConfig insteand.
13
+ */
14
+
15
+ declare type OldPluginConfig = Array<PluginItem | {
16
+ cli?: PluginItem;
17
+ server?: PluginItem;
18
+ }>;
19
+ declare type NewPluginConfig = CliPlugin[] | {
20
+ cli?: CliPlugin[];
21
+ /** Custom server plugin is not supported yet. */
22
+
23
+ server?: never;
24
+ };
25
+ export declare type PluginConfig = OldPluginConfig | NewPluginConfig;
26
+ export declare function getAppPlugins(appDirectory: string, oldPluginConfig: OldPluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): (PluginItem | {
27
+ cli?: PluginItem | undefined;
28
+ server?: PluginItem | undefined;
29
+ })[];
16
30
  /**
17
31
  * Load internal plugins which in @modern-js scope and user's custom plugins.
18
32
  * @param appDirectory - Application root directory.
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.5.0",
14
+ "version": "1.6.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -43,8 +43,8 @@
43
43
  "@babel/code-frame": "^7.14.5",
44
44
  "@babel/runtime": "^7",
45
45
  "@modern-js/load-config": "^1.2.2",
46
- "@modern-js/plugin": "^1.3.0",
47
- "@modern-js/utils": "^1.3.5",
46
+ "@modern-js/plugin": "^1.3.2",
47
+ "@modern-js/utils": "^1.3.6",
48
48
  "address": "^1.1.2",
49
49
  "ajv": "^8.6.2",
50
50
  "ajv-keywords": "^5.0.0",
@@ -8,7 +8,7 @@ const mockAppDirectory = path.join(__dirname, './fixtures/index-test');
8
8
  const mockConfigDir = './config';
9
9
  const mockSrcDirectory = path.join(mockAppDirectory, './src');
10
10
 
11
- describe('initWatcher', () => {
11
+ describe.skip('initWatcher', () => {
12
12
  afterAll(() => {
13
13
  const file = path.join(mockSrcDirectory, './index.ts');
14
14
  if (fs.existsSync(file)) {
@@ -1,4 +1,5 @@
1
1
  import path from 'path';
2
+ import { CliPlugin } from '../src';
2
3
  import { loadPlugins, getAppPlugins } from '../src/loadPlugins';
3
4
 
4
5
  describe('load plugins', () => {
@@ -33,9 +34,7 @@ describe('load plugins', () => {
33
34
  {
34
35
  cli: {
35
36
  name: 'a',
36
- pluginPath: path.join(fixture, './test-plugin-a.js'),
37
37
  },
38
- cliPkg: path.join(fixture, './test-plugin-a.js'),
39
38
  },
40
39
  {
41
40
  server: './test-plugin-b',
@@ -54,8 +53,8 @@ describe('load plugins', () => {
54
53
  plugins: [{ cli: ['./test-plugin-c', 'c'] }, ['./test-plugin-c', 'c2']],
55
54
  });
56
55
 
57
- expect(plugins[0].cli.name).toEqual('c');
58
- expect(plugins[1].cli.name).toEqual('c2');
56
+ expect(plugins[0].cli!.name).toEqual('c');
57
+ expect(plugins[1].cli!.name).toEqual('c2');
59
58
  });
60
59
 
61
60
  test('should load user string plugin successfully', () => {
@@ -72,9 +71,7 @@ describe('load plugins', () => {
72
71
  {
73
72
  cli: {
74
73
  name: 'a',
75
- pluginPath: path.join(fixture, './test-plugin-a.js'),
76
74
  },
77
- cliPkg: path.join(fixture, './test-plugin-a.js'),
78
75
  },
79
76
  ]);
80
77
  });
@@ -88,4 +85,32 @@ describe('load plugins', () => {
88
85
  });
89
86
  }).toThrowError(/^Can not find plugin /);
90
87
  });
88
+
89
+ test(`should load new plugin array correctly`, () => {
90
+ const appDirectory = path.resolve(
91
+ __dirname,
92
+ './fixtures/load-plugin/user-plugins',
93
+ );
94
+ const plugin = (): CliPlugin => ({
95
+ name: 'foo',
96
+ });
97
+ const userConfig = { plugins: [plugin()] };
98
+ const loadedPlugins = loadPlugins(appDirectory, userConfig);
99
+
100
+ expect(loadedPlugins[0].cli?.name).toEqual('foo');
101
+ });
102
+
103
+ test(`should load new plugin object correctly`, () => {
104
+ const appDirectory = path.resolve(
105
+ __dirname,
106
+ './fixtures/load-plugin/user-plugins',
107
+ );
108
+ const plugin = (): CliPlugin => ({
109
+ name: 'foo',
110
+ });
111
+ const userConfig = { plugins: { cli: [plugin()] } };
112
+ const loadedPlugins = loadPlugins(appDirectory, userConfig);
113
+
114
+ expect(loadedPlugins[0].cli?.name).toEqual('foo');
115
+ });
91
116
  });