@modern-js/core 1.4.3 → 1.4.6

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/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # @modern-js/core
2
2
 
3
+ ## 1.4.6
4
+
5
+ ### Patch Changes
6
+
7
+ - cc5e8001: fix: load plugins
8
+ - 2520ea86: fix: garfish schema
9
+ - e81fd9b7: fix: update "server.metrics" type
10
+ - 1c411e71: fix: mergeConfig util function
11
+ - Updated dependencies [db43dce6]
12
+ - @modern-js/utils@1.3.4
13
+
14
+ ## 1.4.4
15
+
16
+ ### Patch Changes
17
+
18
+ - 969f172f: support tools.styledComponents for module-tools,support close tsc process with disbaleTsChecker
19
+ - 4b5d4bf4: fix: output.copy type
20
+ - 62f5b8c8: fix: types
21
+ - 55e18278: chore: remove unused dependencies and devDependencies
22
+ - 4499a674: feat: support to pass options to plugins
23
+ - 403f5169: fix source.moduleScopes type
24
+ - Updated dependencies [4c792f68]
25
+ - Updated dependencies [55e18278]
26
+ - Updated dependencies [a7f42f48]
27
+ - @modern-js/utils@1.3.3
28
+ - @modern-js/load-config@1.2.2
29
+
3
30
  ## 1.4.3
4
31
 
5
32
  ### Patch Changes
@@ -8,12 +8,14 @@ import { isFunction } from '@modern-js/utils';
8
8
  * @returns - normalized user config.
9
9
  */
10
10
  export const mergeConfig = configs => mergeWith({}, ...configs, (target, source) => {
11
- if (Array.isArray(target) && Array.isArray(source)) {
12
- return [...target, ...source];
13
- }
14
-
15
- if (isFunction(source)) {
16
- return Array.isArray(target) ? [...target, source] : [target, source].filter(Boolean);
11
+ if (Array.isArray(target)) {
12
+ if (Array.isArray(source)) {
13
+ return [...target, ...source];
14
+ } else {
15
+ return typeof source !== 'undefined' ? [...target, source] : target;
16
+ }
17
+ } else if (isFunction(source)) {
18
+ return typeof target !== 'undefined' ? [target, source] : [source];
17
19
  }
18
20
 
19
21
  return undefined;
@@ -2,9 +2,6 @@ import { ENTRY_NAME_PATTERN } from '@modern-js/utils';
2
2
  export const deploy = {
3
3
  type: 'object',
4
4
  properties: {
5
- microFrontend: {
6
- type: ['boolean', 'object']
7
- },
8
5
  domain: {
9
6
  type: ['array', 'string']
10
7
  },
@@ -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'
@@ -62,7 +62,7 @@ const createCli = () => {
62
62
  let hooksRunner;
63
63
  let isRestart = false;
64
64
  let restartWithExistingPort = 0;
65
- let restartOptions; // eslint-disable-next-line max-statements
65
+ let restartOptions;
66
66
 
67
67
  const init = async (argv = [], options) => {
68
68
  var _options$options$meta, _options$options;
@@ -74,12 +74,10 @@ const createCli = () => {
74
74
  const metaName = (_options$options$meta = options === null || options === void 0 ? void 0 : (_options$options = options.options) === null || _options$options === void 0 ? void 0 : _options$options.metaName) !== null && _options$options$meta !== void 0 ? _options$options$meta : 'MODERN';
75
75
  loadEnv(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
76
76
  const loaded = await loadUserConfig(appDirectory, options === null || options === void 0 ? void 0 : options.configFile, options === null || options === void 0 ? void 0 : options.packageJsonConfig);
77
- let plugins = loadPlugins(appDirectory, loaded.config.plugins || [], options === null || options === void 0 ? void 0 : options.plugins);
78
-
79
- if (options !== null && options !== void 0 && options.beforeUsePlugins) {
80
- plugins = options.beforeUsePlugins(plugins, loaded.config);
81
- }
82
-
77
+ 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
80
+ });
83
81
  plugins.forEach(plugin => plugin.cli && manager.usePlugin(plugin.cli));
84
82
  const appContext = initAppContext(appDirectory, plugins, loaded.filePath, options === null || options === void 0 ? void 0 : options.options);
85
83
  manager.run(() => {
@@ -9,41 +9,27 @@ const debug = createDebugger('load-plugins');
9
9
 
10
10
  /**
11
11
  * Try to resolve plugin entry file path.
12
+ * @param name - Plugin name.
12
13
  * @param appDirectory - Application root directory.
13
- * @param plugin - Plugin name or plugin name with options.
14
14
  * @returns Resolved file path.
15
15
  */
16
- const resolvePlugin = (appDirectory, plugin) => {
17
- const tryResolve = name => {
18
- let filePath = '';
16
+ const tryResolve = (name, appDirectory) => {
17
+ let filePath = '';
19
18
 
20
- try {
21
- filePath = require.resolve(name, {
22
- paths: [appDirectory]
23
- });
24
- delete require.cache[filePath];
25
- } catch (err) {
26
- if (err.code === 'MODULE_NOT_FOUND') {
27
- throw new Error(`Can not find plugin ${name}.`);
28
- }
29
-
30
- throw err;
19
+ try {
20
+ filePath = require.resolve(name, {
21
+ paths: [appDirectory]
22
+ });
23
+ delete require.cache[filePath];
24
+ } catch (err) {
25
+ if (err.code === 'MODULE_NOT_FOUND') {
26
+ throw new Error(`Can not find plugin ${name}.`);
31
27
  }
32
28
 
33
- return filePath;
34
- };
35
-
36
- const resolved = {};
37
-
38
- if (typeof plugin === 'string' || plugin.cli) {
39
- resolved.cli = typeof plugin === 'string' ? tryResolve(plugin) : tryResolve(plugin.cli);
29
+ throw err;
40
30
  }
41
31
 
42
- if (plugin.server) {
43
- resolved.server = tryResolve(plugin.server);
44
- }
45
-
46
- return resolved;
32
+ return filePath;
47
33
  };
48
34
 
49
35
  export function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
@@ -63,36 +49,74 @@ export function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
63
49
  /**
64
50
  * Load internal plugins which in @modern-js scope and user's custom plugins.
65
51
  * @param appDirectory - Application root directory.
66
- * @param pluginsConfig - Plugins declared in the user configuration.
52
+ * @param userConfig - Resolved user config.
53
+ * @param options.internalPlugins - Internal plugins.
54
+ * @param options.transformPlugin - transform plugin before using it.
67
55
  * @returns Plugin Objects has been required.
68
56
  */
69
57
 
70
- export const loadPlugins = (appDirectory, pluginConfig, internalPlugins) => {
71
- const plugins = getAppPlugins(appDirectory, pluginConfig, internalPlugins);
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
+
83
+ const plugins = getAppPlugins(appDirectory, userConfig.plugins || [], internalPlugins);
72
84
  return plugins.map(plugin => {
85
+ const _plugin = typeof plugin === 'string' || Array.isArray(plugin) ? {
86
+ cli: plugin
87
+ } : plugin;
88
+
73
89
  const {
74
90
  cli,
75
91
  server
76
- } = resolvePlugin(appDirectory, plugin);
77
- debug(`resolve plugin %s: %s`, plugin, {
78
- cli,
79
- server
80
- });
92
+ } = _plugin;
93
+ const loadedPlugin = {};
94
+
95
+ 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;
105
+ } // server plugins don't support to accept params
81
106
 
82
- const cliPlugin = cli && _objectSpread(_objectSpread({}, compatRequire(cli)), {}, {
83
- pluginPath: cli
84
- }); // server plugin should be required by server
85
107
 
108
+ if (server && typeof server === 'string') {
109
+ const path = tryResolve(server, appDirectory);
110
+ loadedPlugin.server = {
111
+ pluginPath: path
112
+ };
113
+ loadedPlugin.serverPkg = server;
114
+ }
86
115
 
87
- const serverPlugin = server && {
88
- // ...compatRequire(server),
89
- pluginPath: server
90
- };
91
- return {
92
- cli: cliPlugin,
93
- cliPath: typeof plugin === 'string' ? plugin : plugin.cli,
94
- server: serverPlugin,
95
- serverPath: typeof plugin === 'string' ? undefined : plugin.server
96
- };
116
+ debug(`resolve plugin %s: %s`, plugin, {
117
+ cli: loadedPlugin.cli,
118
+ server: loadedPlugin.server
119
+ });
120
+ return loadedPlugin;
97
121
  });
98
122
  };
@@ -18,12 +18,14 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
18
18
  * @returns - normalized user config.
19
19
  */
20
20
  const mergeConfig = configs => (0, _lodash.default)({}, ...configs, (target, source) => {
21
- if (Array.isArray(target) && Array.isArray(source)) {
22
- return [...target, ...source];
23
- }
24
-
25
- if ((0, _utils.isFunction)(source)) {
26
- return Array.isArray(target) ? [...target, source] : [target, source].filter(Boolean);
21
+ if (Array.isArray(target)) {
22
+ if (Array.isArray(source)) {
23
+ return [...target, ...source];
24
+ } else {
25
+ return typeof source !== 'undefined' ? [...target, source] : target;
26
+ }
27
+ } else if ((0, _utils.isFunction)(source)) {
28
+ return typeof target !== 'undefined' ? [target, source] : [source];
27
29
  }
28
30
 
29
31
  return undefined;
@@ -10,9 +10,6 @@ var _utils = require("@modern-js/utils");
10
10
  const deploy = {
11
11
  type: 'object',
12
12
  properties: {
13
- microFrontend: {
14
- type: ['boolean', 'object']
15
- },
16
13
  domain: {
17
14
  type: ['array', 'string']
18
15
  },
@@ -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'
@@ -183,7 +183,7 @@ const createCli = () => {
183
183
  let hooksRunner;
184
184
  let isRestart = false;
185
185
  let restartWithExistingPort = 0;
186
- let restartOptions; // eslint-disable-next-line max-statements
186
+ let restartOptions;
187
187
 
188
188
  const init = async (argv = [], options) => {
189
189
  var _options$options$meta, _options$options;
@@ -195,12 +195,10 @@ const createCli = () => {
195
195
  const metaName = (_options$options$meta = options === null || options === void 0 ? void 0 : (_options$options = options.options) === null || _options$options === void 0 ? void 0 : _options$options.metaName) !== null && _options$options$meta !== void 0 ? _options$options$meta : 'MODERN';
196
196
  (0, _loadEnv.loadEnv)(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
197
197
  const loaded = await (0, _config.loadUserConfig)(appDirectory, options === null || options === void 0 ? void 0 : options.configFile, options === null || options === void 0 ? void 0 : options.packageJsonConfig);
198
- let plugins = (0, _loadPlugins.loadPlugins)(appDirectory, loaded.config.plugins || [], options === null || options === void 0 ? void 0 : options.plugins);
199
-
200
- if (options !== null && options !== void 0 && options.beforeUsePlugins) {
201
- plugins = options.beforeUsePlugins(plugins, loaded.config);
202
- }
203
-
198
+ const plugins = (0, _loadPlugins.loadPlugins)(appDirectory, loaded.config, {
199
+ internalPlugins: options === null || options === void 0 ? void 0 : options.plugins,
200
+ transformPlugin: options === null || options === void 0 ? void 0 : options.transformPlugin
201
+ });
204
202
  plugins.forEach(plugin => plugin.cli && manager.usePlugin(plugin.cli));
205
203
  const appContext = (0, _context.initAppContext)(appDirectory, plugins, loaded.filePath, options === null || options === void 0 ? void 0 : options.options);
206
204
  manager.run(() => {
@@ -18,41 +18,27 @@ const debug = (0, _utils.createDebugger)('load-plugins');
18
18
 
19
19
  /**
20
20
  * Try to resolve plugin entry file path.
21
+ * @param name - Plugin name.
21
22
  * @param appDirectory - Application root directory.
22
- * @param plugin - Plugin name or plugin name with options.
23
23
  * @returns Resolved file path.
24
24
  */
25
- const resolvePlugin = (appDirectory, plugin) => {
26
- const tryResolve = name => {
27
- let filePath = '';
25
+ const tryResolve = (name, appDirectory) => {
26
+ let filePath = '';
28
27
 
29
- try {
30
- filePath = require.resolve(name, {
31
- paths: [appDirectory]
32
- });
33
- delete require.cache[filePath];
34
- } catch (err) {
35
- if (err.code === 'MODULE_NOT_FOUND') {
36
- throw new Error(`Can not find plugin ${name}.`);
37
- }
38
-
39
- throw err;
28
+ try {
29
+ filePath = require.resolve(name, {
30
+ paths: [appDirectory]
31
+ });
32
+ delete require.cache[filePath];
33
+ } catch (err) {
34
+ if (err.code === 'MODULE_NOT_FOUND') {
35
+ throw new Error(`Can not find plugin ${name}.`);
40
36
  }
41
37
 
42
- return filePath;
43
- };
44
-
45
- const resolved = {};
46
-
47
- if (typeof plugin === 'string' || plugin.cli) {
48
- resolved.cli = typeof plugin === 'string' ? tryResolve(plugin) : tryResolve(plugin.cli);
49
- }
50
-
51
- if (plugin.server) {
52
- resolved.server = tryResolve(plugin.server);
38
+ throw err;
53
39
  }
54
40
 
55
- return resolved;
41
+ return filePath;
56
42
  };
57
43
 
58
44
  function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
@@ -72,38 +58,76 @@ function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
72
58
  /**
73
59
  * Load internal plugins which in @modern-js scope and user's custom plugins.
74
60
  * @param appDirectory - Application root directory.
75
- * @param pluginsConfig - Plugins declared in the user configuration.
61
+ * @param userConfig - Resolved user config.
62
+ * @param options.internalPlugins - Internal plugins.
63
+ * @param options.transformPlugin - transform plugin before using it.
76
64
  * @returns Plugin Objects has been required.
77
65
  */
78
66
 
79
67
 
80
- const loadPlugins = (appDirectory, pluginConfig, internalPlugins) => {
81
- const plugins = getAppPlugins(appDirectory, pluginConfig, internalPlugins);
68
+ const loadPlugins = (appDirectory, userConfig, options = {}) => {
69
+ const {
70
+ internalPlugins,
71
+ transformPlugin
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 (transformPlugin) {
81
+ module = transformPlugin(module, userConfig, pluginOptions);
82
+ } else {
83
+ module = typeof module === 'function' ? module(pluginOptions) : module;
84
+ }
85
+
86
+ return {
87
+ pkg,
88
+ path,
89
+ module
90
+ };
91
+ };
92
+
93
+ const plugins = getAppPlugins(appDirectory, userConfig.plugins || [], internalPlugins);
82
94
  return plugins.map(plugin => {
95
+ const _plugin = typeof plugin === 'string' || Array.isArray(plugin) ? {
96
+ cli: plugin
97
+ } : plugin;
98
+
83
99
  const {
84
100
  cli,
85
101
  server
86
- } = resolvePlugin(appDirectory, plugin);
87
- debug(`resolve plugin %s: %s`, plugin, {
88
- cli,
89
- server
90
- });
102
+ } = _plugin;
103
+ const loadedPlugin = {};
104
+
105
+ if (cli) {
106
+ const {
107
+ pkg,
108
+ path,
109
+ module
110
+ } = resolvePlugin(cli);
111
+ loadedPlugin.cli = _objectSpread(_objectSpread({}, module), {}, {
112
+ pluginPath: path
113
+ });
114
+ loadedPlugin.cliPkg = pkg;
115
+ } // server plugins don't support to accept params
91
116
 
92
- const cliPlugin = cli && _objectSpread(_objectSpread({}, (0, _utils.compatRequire)(cli)), {}, {
93
- pluginPath: cli
94
- }); // server plugin should be required by server
95
117
 
118
+ if (server && typeof server === 'string') {
119
+ const path = tryResolve(server, appDirectory);
120
+ loadedPlugin.server = {
121
+ pluginPath: path
122
+ };
123
+ loadedPlugin.serverPkg = server;
124
+ }
96
125
 
97
- const serverPlugin = server && {
98
- // ...compatRequire(server),
99
- pluginPath: server
100
- };
101
- return {
102
- cli: cliPlugin,
103
- cliPath: typeof plugin === 'string' ? plugin : plugin.cli,
104
- server: serverPlugin,
105
- serverPath: typeof plugin === 'string' ? undefined : plugin.server
106
- };
126
+ debug(`resolve plugin %s: %s`, plugin, {
127
+ cli: loadedPlugin.cli,
128
+ server: loadedPlugin.server
129
+ });
130
+ return loadedPlugin;
107
131
  });
108
132
  };
109
133
 
@@ -19,7 +19,7 @@ interface SourceConfig {
19
19
  envVars?: Array<string>;
20
20
  globalVars?: Record<string, string>;
21
21
  alias?: Record<string, string> | ((aliases: Record<string, string>) => Record<string, unknown>);
22
- moduleScopes?: Array<string | RegExp> | ((scopes: Array<string | RegExp>) => Array<string | RegExp>);
22
+ moduleScopes?: Array<string | RegExp> | ((scopes: Array<string | RegExp>) => void) | ((scopes: Array<string | RegExp>) => Array<string | RegExp>);
23
23
  include?: Array<string | RegExp>;
24
24
  }
25
25
  interface OutputConfig {
@@ -38,7 +38,9 @@ interface OutputConfig {
38
38
  mountId?: string;
39
39
  favicon?: string;
40
40
  faviconByEntries?: Record<string, string | undefined>;
41
- copy?: Record<string, unknown>;
41
+ copy?: Array<Record<string, unknown> & {
42
+ from: string;
43
+ }>;
42
44
  scriptExt?: Record<string, unknown>;
43
45
  disableTsChecker?: boolean;
44
46
  disableHtmlFolder?: boolean;
@@ -98,6 +100,7 @@ interface ToolsConfig {
98
100
  babel?: ConfigFunction;
99
101
  autoprefixer?: ConfigFunction;
100
102
  postcss?: ConfigFunction;
103
+ styledComponents?: ConfigFunction;
101
104
  lodash?: ConfigFunction;
102
105
  devServer?: Record<string, unknown>;
103
106
  tsLoader?: ConfigFunction;
@@ -3,10 +3,11 @@ export interface NormalizedSourceConfig extends Omit<SourceConfig, 'alias' | 'mo
3
3
  alias: SourceConfig['alias'] | Array<SourceConfig['alias']>;
4
4
  moduleScopes: SourceConfig['moduleScopes'] | Array<SourceConfig['moduleScopes']>;
5
5
  }
6
- export interface NormalizedToolsConfig extends Omit<ToolsConfig, 'webpack' | 'babel' | 'postcss' | 'autoprefixer' | 'lodash' | 'tsLoader' | 'terser' | 'minifyCss' | 'esbuild'> {
6
+ export interface NormalizedToolsConfig extends Omit<ToolsConfig, 'webpack' | 'babel' | 'postcss' | 'autoprefixer' | 'lodash' | 'tsLoader' | 'terser' | 'minifyCss' | 'esbuild' | 'styledComponents'> {
7
7
  webpack: ToolsConfig['webpack'] | Array<NonNullable<ToolsConfig['webpack']>>;
8
8
  babel: ToolsConfig['babel'] | Array<NonNullable<ToolsConfig['babel']>>;
9
9
  postcss: ToolsConfig['postcss'] | Array<NonNullable<ToolsConfig['postcss']>>;
10
+ styledComponents: ToolsConfig['styledComponents'] | Array<NonNullable<ToolsConfig['styledComponents']>>;
10
11
  autoprefixer: ToolsConfig['autoprefixer'] | Array<NonNullable<ToolsConfig['autoprefixer']>>;
11
12
  lodash: ToolsConfig['lodash'] | Array<ToolsConfig['lodash']>;
12
13
  tsLoader: ToolsConfig['tsLoader'] | Array<NonNullable<ToolsConfig['tsLoader']>>;
@@ -1,9 +1,6 @@
1
1
  export declare const deploy: {
2
2
  type: string;
3
3
  properties: {
4
- microFrontend: {
5
- type: string[];
6
- };
7
4
  domain: {
8
5
  type: string[];
9
6
  };
@@ -380,10 +380,10 @@ export declare const patchSchema: (pluginSchemas: Array<PluginValidateSchema | P
380
380
  instanceof: string;
381
381
  };
382
382
  logger: {
383
- type: string;
383
+ type: string[];
384
384
  };
385
385
  metrics: {
386
- type: string;
386
+ type: string[];
387
387
  };
388
388
  proxy: {
389
389
  type: string;
@@ -396,9 +396,6 @@ export declare const patchSchema: (pluginSchemas: Array<PluginValidateSchema | P
396
396
  deploy: {
397
397
  type: string;
398
398
  properties: {
399
- microFrontend: {
400
- type: string[];
401
- };
402
399
  domain: {
403
400
  type: string[];
404
401
  };
@@ -167,10 +167,10 @@ export declare const server: {
167
167
  instanceof: string;
168
168
  };
169
169
  logger: {
170
- type: string;
170
+ type: string[];
171
171
  };
172
172
  metrics: {
173
- type: string;
173
+ type: string[];
174
174
  };
175
175
  proxy: {
176
176
  type: string;
@@ -1,6 +1,7 @@
1
1
  import type { IAppContext } from '@modern-js/types';
2
2
  import { UserConfig } from './config';
3
3
  import { NormalizedConfig } from './config/mergeConfig';
4
+ import type { LoadedPlugin } from './loadPlugins';
4
5
  export type { IAppContext };
5
6
  export declare const AppContext: import("@modern-js/plugin").Context<IAppContext>;
6
7
  export declare const ConfigContext: import("@modern-js/plugin").Context<UserConfig>;
@@ -8,10 +9,7 @@ export declare const ResolvedConfigContext: import("@modern-js/plugin").Context<
8
9
  export declare const useAppContext: () => IAppContext;
9
10
  export declare const useConfigContext: () => UserConfig;
10
11
  export declare const useResolvedConfigContext: () => NormalizedConfig;
11
- export declare const initAppContext: (appDirectory: string, plugins: Array<{
12
- cli: any;
13
- server: any;
14
- }>, configFile: string | false, options?: {
12
+ export declare const initAppContext: (appDirectory: string, plugins: Array<LoadedPlugin>, configFile: string | false, options?: {
15
13
  metaName?: string | undefined;
16
14
  srcDir?: string | undefined;
17
15
  distDir?: string | undefined;
@@ -3,6 +3,7 @@ import { ParallelWorkflow, AsyncWorkflow, Progresses2Runners, AsyncWaterfall } f
3
3
  import type { Hooks } from '@modern-js/types';
4
4
  import { ErrorObject } from 'ajv';
5
5
  import { Command } from './utils/commander';
6
+ import { TransformPlugin } from './loadPlugins';
6
7
  import { AppContext, ConfigContext, IAppContext, initAppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from './context';
7
8
  import { NormalizedConfig } from './config/mergeConfig';
8
9
  export type { Hooks };
@@ -104,12 +105,7 @@ export interface CoreOptions {
104
105
  configFile?: string;
105
106
  packageJsonConfig?: string;
106
107
  plugins?: typeof INTERNAL_PLUGINS;
107
- beforeUsePlugins?: (plugins: any, config: any) => {
108
- cli: any;
109
- cliPath: any;
110
- server: any;
111
- serverPath: any;
112
- }[];
108
+ transformPlugin?: TransformPlugin;
113
109
  onSchemaError?: (error: ErrorObject) => void;
114
110
  options?: {
115
111
  metaName?: string;
@@ -1,30 +1,30 @@
1
1
  import { INTERNAL_PLUGINS } from '@modern-js/utils';
2
- export interface PluginConfigItem {
3
- cli?: string;
4
- server?: string;
5
- }
2
+ import type { UserConfig } from './config';
3
+ declare type Plugin = string | [string, any];
4
+ export declare type LoadedPlugin = {
5
+ cli?: any;
6
+ cliPkg?: string;
7
+ server?: any;
8
+ serverPkg?: string;
9
+ };
10
+ export declare type PluginConfigItem = {
11
+ cli?: Plugin;
12
+ server?: Plugin;
13
+ } | Plugin;
6
14
  export declare type PluginConfig = Array<PluginConfigItem>;
7
- export declare function getAppPlugins(appDirectory: string, pluginConfig: PluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): {
8
- cli?: string | undefined;
9
- server?: string | undefined;
10
- }[];
15
+ export declare type TransformPlugin = (plugin: PluginConfig, resolvedConfig: UserConfig, pluginOptions?: any) => PluginConfig;
16
+ export declare function getAppPlugins(appDirectory: string, pluginConfig: PluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): PluginConfigItem[];
11
17
  /**
12
18
  * Load internal plugins which in @modern-js scope and user's custom plugins.
13
19
  * @param appDirectory - Application root directory.
14
- * @param pluginsConfig - Plugins declared in the user configuration.
20
+ * @param userConfig - Resolved user config.
21
+ * @param options.internalPlugins - Internal plugins.
22
+ * @param options.transformPlugin - transform plugin before using it.
15
23
  * @returns Plugin Objects has been required.
16
24
  */
17
25
 
18
- export declare const loadPlugins: (appDirectory: string, pluginConfig: PluginConfig, internalPlugins?: {
19
- [name: string]: {
20
- cli?: string | undefined;
21
- server?: string | undefined;
22
- };
23
- } | undefined) => {
24
- cli: any;
25
- cliPath: string | undefined;
26
- server: "" | {
27
- pluginPath: string;
28
- } | undefined;
29
- serverPath: string | undefined;
30
- }[];
26
+ export declare const loadPlugins: (appDirectory: string, userConfig: UserConfig, options?: {
27
+ internalPlugins?: typeof INTERNAL_PLUGINS;
28
+ transformPlugin?: TransformPlugin;
29
+ }) => LoadedPlugin[];
30
+ export {};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.3",
14
+ "version": "1.4.6",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -42,9 +42,9 @@
42
42
  "dependencies": {
43
43
  "@babel/code-frame": "^7.14.5",
44
44
  "@babel/runtime": "^7",
45
- "@modern-js/load-config": "^1.2.1",
45
+ "@modern-js/load-config": "^1.2.2",
46
46
  "@modern-js/plugin": "^1.2.1",
47
- "@modern-js/utils": "^1.3.2",
47
+ "@modern-js/utils": "^1.3.4",
48
48
  "address": "^1.1.2",
49
49
  "ajv": "^8.6.2",
50
50
  "ajv-keywords": "^5.0.0",
@@ -55,14 +55,13 @@
55
55
  "dotenv-expand": "^5.1.0",
56
56
  "lodash.clonedeep": "^4.5.0",
57
57
  "lodash.mergewith": "^4.6.2",
58
- "minimist": "^1.2.5",
59
58
  "signale": "^1.4.0",
60
59
  "v8-compile-cache": "^2.3.0"
61
60
  },
62
61
  "devDependencies": {
63
62
  "btsm": "2.2.2",
64
63
  "@types/babel__code-frame": "^7.0.3",
65
- "@modern-js/types": "^1.3.3",
64
+ "@modern-js/types": "^1.3.4",
66
65
  "@types/jest": "^26",
67
66
  "@types/lodash.clonedeep": "^4.5.6",
68
67
  "@types/lodash.mergewith": "^4.6.6",
@@ -0,0 +1,3 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+
3
+ exports.default = name => ({ name });
@@ -62,13 +62,8 @@ describe('@modern-js/core test', () => {
62
62
 
63
63
  it('test cli init dev', async () => {
64
64
  cwdSpy.mockReturnValue(path.join(cwd, 'nested-folder'));
65
- const options = {
66
- beforeUsePlugins: jest.fn(),
67
- };
68
- options.beforeUsePlugins.mockImplementation((plugins, _) => plugins);
69
- await cli.init(['dev'], options);
65
+ await cli.init(['dev']);
70
66
  expect(loadEnv).toHaveBeenCalledWith(cwd, undefined);
71
- expect(options.beforeUsePlugins).toHaveBeenCalledWith([], {});
72
67
  // TODO: add more test cases
73
68
  });
74
69
  });
@@ -22,10 +22,12 @@ describe('load plugins', () => {
22
22
  './fixtures/load-plugin/user-plugins',
23
23
  );
24
24
 
25
- const plugins = loadPlugins(fixture, [
26
- { cli: path.join(fixture, './test-plugin-a.js') },
27
- { server: './test-plugin-b' },
28
- ]);
25
+ const plugins = loadPlugins(fixture, {
26
+ plugins: [
27
+ { cli: path.join(fixture, './test-plugin-a.js') },
28
+ { server: './test-plugin-b' },
29
+ ],
30
+ });
29
31
 
30
32
  expect(plugins).toEqual([
31
33
  {
@@ -33,26 +35,54 @@ describe('load plugins', () => {
33
35
  name: 'a',
34
36
  pluginPath: path.join(fixture, './test-plugin-a.js'),
35
37
  },
36
- cliPath: path.join(fixture, './test-plugin-a.js'),
38
+ cliPkg: path.join(fixture, './test-plugin-a.js'),
37
39
  },
38
40
  {
39
41
  server: {
40
42
  pluginPath: path.join(fixture, './test-plugin-b.js'),
41
43
  },
42
- serverPath: './test-plugin-b',
44
+ serverPkg: './test-plugin-b',
43
45
  },
44
46
  ]);
45
47
  });
46
48
 
47
- test('should load user string plugin successfully', () => {
49
+ test('should pass options to Plugin', () => {
48
50
  const fixture = path.resolve(
49
51
  __dirname,
50
52
  './fixtures/load-plugin/user-plugins',
51
53
  );
52
54
 
53
- const plugins = loadPlugins(fixture, [
54
- path.join(fixture, './test-plugin-a.js') as any,
55
+ const plugins = loadPlugins(fixture, {
56
+ plugins: [{ cli: ['./test-plugin-c', 'c'] }, ['./test-plugin-c', 'c2']],
57
+ });
58
+
59
+ expect(plugins).toEqual([
60
+ {
61
+ cli: {
62
+ name: 'c',
63
+ pluginPath: path.join(fixture, './test-plugin-c.js'),
64
+ },
65
+ cliPkg: './test-plugin-c',
66
+ },
67
+ {
68
+ cli: {
69
+ name: 'c2',
70
+ pluginPath: path.join(fixture, './test-plugin-c.js'),
71
+ },
72
+ cliPkg: './test-plugin-c',
73
+ },
55
74
  ]);
75
+ });
76
+
77
+ test('should load user string plugin successfully', () => {
78
+ const fixture = path.resolve(
79
+ __dirname,
80
+ './fixtures/load-plugin/user-plugins',
81
+ );
82
+
83
+ const plugins = loadPlugins(fixture, {
84
+ plugins: [path.join(fixture, './test-plugin-a.js') as any],
85
+ });
56
86
 
57
87
  expect(plugins).toEqual([
58
88
  {
@@ -60,16 +90,38 @@ describe('load plugins', () => {
60
90
  name: 'a',
61
91
  pluginPath: path.join(fixture, './test-plugin-a.js'),
62
92
  },
63
- cliPath: path.join(fixture, './test-plugin-a.js'),
93
+ cliPkg: path.join(fixture, './test-plugin-a.js'),
64
94
  },
65
95
  ]);
66
96
  });
67
97
 
98
+ test('should call transformPlugin', () => {
99
+ const fixture = path.resolve(
100
+ __dirname,
101
+ './fixtures/load-plugin/user-plugins',
102
+ );
103
+
104
+ const options = {
105
+ transformPlugin: jest.fn(),
106
+ };
107
+ options.transformPlugin.mockImplementation((plugins, _) => plugins);
108
+
109
+ loadPlugins(
110
+ fixture,
111
+ { plugins: [{ cli: path.join(fixture, './test-plugin-a.js') }] },
112
+ options,
113
+ );
114
+
115
+ expect(options.transformPlugin).toHaveBeenCalled();
116
+ });
117
+
68
118
  test(`should throw error when plugin not found `, () => {
69
119
  const fixture = path.resolve(__dirname, './fixtures/load-plugin/not-found');
70
120
 
71
121
  expect(() => {
72
- loadPlugins(fixture, [{ cli: './test-plugin-a' }, { cli: './plugin-b' }]);
122
+ loadPlugins(fixture, {
123
+ plugins: [{ cli: './test-plugin-a' }, { cli: './plugin-b' }],
124
+ });
73
125
  }).toThrowError(/^Can not find plugin /);
74
126
  });
75
127
  });
@@ -19,7 +19,7 @@ describe('load plugins', () => {
19
19
  });
20
20
  });
21
21
 
22
- test(`should set value when property value is undefined `, () => {
22
+ test(`should set value when property value is not undefined `, () => {
23
23
  expect(
24
24
  mergeConfig([
25
25
  { source: { entries: { app: './App.tsx' } } },
@@ -37,6 +37,22 @@ describe('load plugins', () => {
37
37
  });
38
38
  });
39
39
 
40
+ test(`should ignore undefined property`, () => {
41
+ const config = mergeConfig([
42
+ { source: { entries: { app: './App.tsx' } } },
43
+ { source: { entries: undefined } },
44
+ { tools: { webpack: () => ({}) } },
45
+ { tools: { webpack: undefined } },
46
+ ]);
47
+ expect(config.source).toEqual({
48
+ entries: {
49
+ app: './App.tsx',
50
+ },
51
+ });
52
+ expect(Array.isArray(config.tools.webpack)).toBe(true);
53
+ expect(config.tools.webpack?.length).toBe(1);
54
+ });
55
+
40
56
  test(`should merge array value`, () => {
41
57
  expect(
42
58
  mergeConfig([
@@ -67,12 +83,15 @@ describe('load plugins', () => {
67
83
  { source: { alias: { a: 'b' } } },
68
84
  { source: { alias: () => ({ c: 'd' }) } },
69
85
  { tools: { webpack: () => ({}) } },
86
+ { tools: { webpack: { name: 'test' } } },
87
+ { tools: { webpack: () => ({}) } },
70
88
  ]);
71
89
  expect(Array.isArray(config.source.alias)).toBe(true);
72
90
  expect(config?.source?.alias?.length).toBe(2);
73
91
  expect(typeof (config.source.alias as Array<any>)[1]).toBe('function');
74
-
75
92
  expect(Array.isArray(config.tools.webpack)).toBe(true);
76
- expect(config.tools.webpack?.length).toBe(1);
93
+ expect(config.tools.webpack?.length).toBe(3);
94
+ expect(typeof (config.tools.webpack as Array<any>)[0]).toBe('function');
95
+ expect(typeof (config.tools.webpack as Array<any>)[2]).toBe('function');
77
96
  });
78
97
  });
@@ -8,16 +8,14 @@ describe('patch schemas', () => {
8
8
  schema: { type: 'string' },
9
9
  },
10
10
  {
11
- target: 'deploy.microFrontend.foo',
11
+ target: 'deploy.foo',
12
12
  schema: { type: 'number' },
13
13
  },
14
14
  ]);
15
15
 
16
16
  expect(schema.properties).toHaveProperty('foo');
17
17
 
18
- expect(
19
- schema.properties.deploy.properties.microFrontend.properties,
20
- ).toHaveProperty('foo');
18
+ expect(schema.properties.deploy.properties).toHaveProperty('foo');
21
19
  });
22
20
 
23
21
  test('should throw error when node is undefined', () => {