@modern-js/core 2.0.0-beta.1 → 2.0.0-beta.2

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.
Files changed (76) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/dist/bin.js +1 -4
  3. package/dist/config/createLoadedConfig.d.ts +6 -0
  4. package/dist/config/createLoadedConfig.js +35 -0
  5. package/dist/config/createResolvedConfig.d.ts +3 -0
  6. package/dist/config/createResolvedConfig.js +92 -0
  7. package/dist/config/index.d.ts +2 -17
  8. package/dist/config/index.js +2 -136
  9. package/dist/context.d.ts +5 -9
  10. package/dist/index.d.ts +6 -14
  11. package/dist/index.js +23 -62
  12. package/dist/loadPlugins.d.ts +2 -12
  13. package/dist/loadPlugins.js +12 -5
  14. package/dist/manager.d.ts +80 -64
  15. package/dist/manager.js +11 -5
  16. package/dist/schema/patchSchema.d.ts +19 -0
  17. package/dist/{config/schema/index.js → schema/patchSchema.js} +8 -45
  18. package/dist/schema/source.d.ts +9 -0
  19. package/dist/schema/source.js +10 -0
  20. package/dist/schema/testing.d.ts +13 -0
  21. package/dist/schema/testing.js +11 -0
  22. package/dist/schema/traverseSchema.d.ts +2 -0
  23. package/dist/schema/traverseSchema.js +20 -0
  24. package/dist/types/config/index.d.ts +41 -0
  25. package/dist/{config/types → types/config}/index.js +0 -0
  26. package/dist/types/config/testing.d.ts +15 -0
  27. package/dist/{config/types/electron.js → types/config/testing.js} +0 -0
  28. package/dist/types/context.d.ts +26 -0
  29. package/dist/{config/types/less.js → types/context.js} +0 -0
  30. package/dist/types/hooks.d.ts +26 -0
  31. package/dist/{config/types/postcss.js → types/hooks.js} +0 -0
  32. package/dist/types/index.d.ts +10 -1
  33. package/dist/types/index.js +5 -1
  34. package/dist/types/plugin.d.ts +26 -0
  35. package/dist/{config/types/sass.js → types/plugin.js} +0 -0
  36. package/dist/types/pluginAPI.d.ts +18 -0
  37. package/dist/{config/types/ssg.js → types/pluginAPI.js} +0 -0
  38. package/dist/utils/mergeConfig.d.ts +2 -0
  39. package/dist/{config → utils}/mergeConfig.js +1 -8
  40. package/dist/utils/repeatKeyWarning.d.ts +2 -2
  41. package/dist/utils/repeatKeyWarning.js +2 -2
  42. package/package.json +16 -9
  43. package/compiled/v8-compile-cache/index.d.ts +0 -1
  44. package/compiled/v8-compile-cache/index.js +0 -1
  45. package/compiled/v8-compile-cache/license +0 -21
  46. package/compiled/v8-compile-cache/package.json +0 -1
  47. package/dist/config/defaults.d.ts +0 -29
  48. package/dist/config/defaults.js +0 -110
  49. package/dist/config/mergeConfig.d.ts +0 -32
  50. package/dist/config/schema/deploy.d.ts +0 -16
  51. package/dist/config/schema/deploy.js +0 -16
  52. package/dist/config/schema/index.d.ts +0 -499
  53. package/dist/config/schema/output.d.ts +0 -146
  54. package/dist/config/schema/output.js +0 -68
  55. package/dist/config/schema/server.d.ts +0 -194
  56. package/dist/config/schema/server.js +0 -111
  57. package/dist/config/schema/source.d.ts +0 -64
  58. package/dist/config/schema/source.js +0 -38
  59. package/dist/config/schema/tools.d.ts +0 -51
  60. package/dist/config/schema/tools.js +0 -24
  61. package/dist/config/types/electron.d.ts +0 -13
  62. package/dist/config/types/index.d.ts +0 -260
  63. package/dist/config/types/less.d.ts +0 -12
  64. package/dist/config/types/postcss.d.ts +0 -28
  65. package/dist/config/types/sass.d.ts +0 -10
  66. package/dist/config/types/ssg.d.ts +0 -15
  67. package/dist/config/types/test.d.ts +0 -14
  68. package/dist/config/types/test.js +0 -2
  69. package/dist/config/types/ts-loader.d.ts +0 -23
  70. package/dist/config/types/ts-loader.js +0 -2
  71. package/dist/initWatcher.d.ts +0 -3
  72. package/dist/initWatcher.js +0 -66
  73. package/dist/pluginAPI.d.ts +0 -13
  74. package/dist/pluginAPI.js +0 -16
  75. package/dist/types/cli.d.ts +0 -59
  76. package/dist/types/cli.js +0 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,69 @@
1
1
  # @modern-js/core
2
2
 
3
+ ## 2.0.0-beta.2
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9c3e: chore: v2
8
+
9
+ ### Minor Changes
10
+
11
+ - c9e800d39a: feat: support React18 streaming SSR
12
+ feat: 支持 React18 流式 SSR
13
+ - 92f0ead: feat:
14
+
15
+ 1. core: 增加 test 函数
16
+ 2. module plugins: 增加 `babel`, `mainField`, `target` 插件
17
+ 3. storybook: 修改部分逻辑并且增加 tspath webpack 插件
18
+ 4. 增加 designSystem 配置
19
+
20
+ feat:
21
+
22
+ 1. core: add test method
23
+ 2. module plugins: add `babel`, `mainField`, `target` plugin
24
+ 3. storybook: change some logic and add tspath webpack plugin
25
+ 4. add `designSystem` config
26
+
27
+ - edd1cfb1af: feat: modernjs Access builder compiler
28
+ feat: modernjs 接入 builder 构建
29
+ - d5a31df781: refactor: remove unbundle configs and types
30
+
31
+ refactor: 移除 unbundle 相关的配置项和类型定义
32
+
33
+ ### Patch Changes
34
+
35
+ - 85edee888c: feat(app-tools): support tools.htmlPlugin config
36
+
37
+ feat(app-tools): 支持 tools.htmlPlugin 配置项
38
+
39
+ - b8bbe036c7: feat: change type logic
40
+ feat: 修改类型相关的逻辑
41
+ - 8b8e1bb571: feat: support nested routes
42
+ feat: 支持嵌套路由
43
+ - 3bbea92b2a: feat: support Hook、Middleware new API
44
+ feat: 支持 Hook、Middleware 的新 API
45
+ - 14b712da84: fix: use consistent alias type and default value across packages
46
+
47
+ fix: 在各个包中使用一致的 alias 类型定义和默认值
48
+
49
+ - Updated dependencies [a2509bfbdb]
50
+ - Updated dependencies [92f0ead]
51
+ - Updated dependencies [edd1cfb1af]
52
+ - Updated dependencies [cc971eabfc]
53
+ - Updated dependencies [5b9049f2e9]
54
+ - Updated dependencies [92004d1]
55
+ - Updated dependencies [b8bbe036c7]
56
+ - Updated dependencies [d5a31df781]
57
+ - Updated dependencies [dda38c9c3e]
58
+ - Updated dependencies [3bbea92b2a]
59
+ - Updated dependencies [f179749]
60
+ - Updated dependencies [abf3421a75]
61
+ - Updated dependencies [543be9558e]
62
+ - Updated dependencies [14b712da84]
63
+ - @modern-js/node-bundle-require@2.0.0-beta.2
64
+ - @modern-js/utils@2.0.0-beta.2
65
+ - @modern-js/plugin@2.0.0-beta.2
66
+
3
67
  ## 2.0.0-beta.1
4
68
 
5
69
  ### Major Changes
package/dist/bin.js CHANGED
@@ -1,9 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
- /* eslint-disable-next-line eslint-comments/disable-enable-pair */
4
- /* eslint-disable import/first */
5
3
  Object.defineProperty(exports, "__esModule", { value: true });
6
- require('../compiled/v8-compile-cache');
7
4
  const utils_1 = require("@modern-js/utils");
8
5
  const _1 = require(".");
9
6
  const command = process.argv[2];
@@ -38,4 +35,4 @@ const customConfigFile = cliParams.config || cliParams.c;
38
35
  if (SUPPORT_CONFIG_PARAM_COMMANDS.includes(command) && customConfigFile) {
39
36
  runOptions.configFile = customConfigFile;
40
37
  }
41
- _1.cli.run(process.argv.slice(2), runOptions);
38
+ _1.cli.run(runOptions);
@@ -0,0 +1,6 @@
1
+ import { LoadedConfig, UserConfig, ConfigParams } from '../types';
2
+ /**
3
+ * Assign the pkg config into the user config.
4
+ */
5
+ export declare const assignPkgConfig: (userConfig?: UserConfig, pkgConfig?: ConfigParams) => UserConfig;
6
+ export declare function createLoadedConfig(appDirectory: string, filePath?: string, packageJsonConfig?: string): Promise<LoadedConfig<{}>>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createLoadedConfig = exports.assignPkgConfig = void 0;
4
+ const lodash_1 = require("@modern-js/utils/lodash");
5
+ const utils_1 = require("@modern-js/utils");
6
+ const load_configs_1 = require("../load-configs");
7
+ /**
8
+ * Assign the pkg config into the user config.
9
+ */
10
+ const assignPkgConfig = (userConfig = {}, pkgConfig = {}) => (0, lodash_1.mergeWith)({}, userConfig, pkgConfig, (objValue, srcValue) => {
11
+ // mergeWith can not merge object with symbol, but plugins object contains symbol,
12
+ // so we need to handle it manually.
13
+ if (objValue === undefined && (0, utils_1.isPlainObject)(srcValue)) {
14
+ return { ...srcValue };
15
+ }
16
+ // return undefined to use the default behavior of mergeWith
17
+ return undefined;
18
+ });
19
+ exports.assignPkgConfig = assignPkgConfig;
20
+ async function createLoadedConfig(appDirectory, filePath, packageJsonConfig) {
21
+ const loaded = await (0, load_configs_1.loadConfig)(appDirectory, filePath, packageJsonConfig);
22
+ const config = !loaded
23
+ ? {}
24
+ : await (typeof loaded.config === 'function'
25
+ ? loaded.config(0)
26
+ : loaded.config);
27
+ return {
28
+ config: (0, exports.assignPkgConfig)(config, loaded === null || loaded === void 0 ? void 0 : loaded.pkgConfig),
29
+ filePath: loaded.path,
30
+ dependencies: loaded.dependencies || [],
31
+ pkgConfig: loaded.pkgConfig || {},
32
+ jsConfig: config || {},
33
+ };
34
+ }
35
+ exports.createLoadedConfig = createLoadedConfig;
@@ -0,0 +1,3 @@
1
+ import type { ErrorObject } from '@modern-js/utils/ajv';
2
+ import type { UserConfig, NormalizedConfig, LoadedConfig, PluginValidateSchema } from '../types';
3
+ export declare const createResolveConfig: (loaded: LoadedConfig<{}>, configs: UserConfig[], schemas: PluginValidateSchema[], onSchemaError?: (error: ErrorObject) => void | Promise<void>) => Promise<NormalizedConfig>;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.createResolveConfig = void 0;
27
+ const utils_1 = require("@modern-js/utils");
28
+ const constants_1 = require("@modern-js/utils/constants");
29
+ const patchSchema_1 = require("../schema/patchSchema");
30
+ const repeatKeyWarning_1 = require("../utils/repeatKeyWarning");
31
+ const mergeConfig_1 = require("../utils/mergeConfig");
32
+ const debug = (0, utils_1.createDebugger)('resolve-config');
33
+ const showAdditionalPropertiesError = async (error) => {
34
+ if (error.keyword === 'additionalProperties' &&
35
+ error.params.additionalProperty) {
36
+ const target = [
37
+ error.instancePath.slice(1),
38
+ error.params.additionalProperty,
39
+ ]
40
+ .filter(Boolean)
41
+ .join('.');
42
+ const name = Object.keys(constants_1.PLUGIN_SCHEMAS).find(key => constants_1.PLUGIN_SCHEMAS[key].some((schemaItem) => schemaItem.target === target));
43
+ if (name) {
44
+ const packageManager = await (0, utils_1.getPackageManager)();
45
+ utils_1.logger.warn(`The configuration of ${utils_1.chalk.bold(target)} is provided by plugin ${utils_1.chalk.bold(name)}. Please use ${utils_1.chalk.bold(`${packageManager} run new`)} to enable the corresponding capability.\n`);
46
+ }
47
+ }
48
+ };
49
+ const createResolveConfig = async (
50
+ // eslint-disable-next-line @typescript-eslint/ban-types
51
+ loaded, configs, schemas, onSchemaError = showAdditionalPropertiesError) => {
52
+ var _a;
53
+ const { default: Ajv } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/ajv')));
54
+ const { default: ajvKeywords } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/ajv-keywords')));
55
+ const { default: betterAjvErrors } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/better-ajv-errors')));
56
+ const { config: userConfig, jsConfig, pkgConfig } = loaded;
57
+ const ajv = new Ajv({ $data: true, strict: false });
58
+ ajvKeywords(ajv);
59
+ const validateSchema = (0, patchSchema_1.patchSchema)(schemas);
60
+ const validate = ajv.compile(validateSchema);
61
+ (0, repeatKeyWarning_1.repeatKeyWarning)(validateSchema, jsConfig, pkgConfig);
62
+ // validate user config.
63
+ const valid = validate(userConfig);
64
+ const formatValidateError = (config) => {
65
+ var _a;
66
+ return betterAjvErrors(validateSchema, config, (_a = validate.errors) === null || _a === void 0 ? void 0 : _a.map(e => ({
67
+ ...e,
68
+ dataPath: e.instancePath,
69
+ })), {
70
+ indent: 2,
71
+ });
72
+ };
73
+ // FIXME: pass temporarily
74
+ if (!valid && ((_a = validate.errors) === null || _a === void 0 ? void 0 : _a.length)) {
75
+ await onSchemaError(validate === null || validate === void 0 ? void 0 : validate.errors[0]);
76
+ const errors = formatValidateError(userConfig);
77
+ utils_1.logger.log(errors);
78
+ throw new Error(`Validate configuration error`);
79
+ }
80
+ // validate config from plugins.
81
+ for (const config of configs) {
82
+ if (!validate(config)) {
83
+ const errors = formatValidateError(config);
84
+ utils_1.logger.error(errors);
85
+ throw new Error(`Validate configuration error.`);
86
+ }
87
+ }
88
+ const resolved = (0, mergeConfig_1.mergeConfig)([...configs, userConfig]);
89
+ debug('resolved %o', resolved);
90
+ return resolved;
91
+ };
92
+ exports.createResolveConfig = createResolveConfig;
@@ -1,17 +1,2 @@
1
- import type { ErrorObject } from '@modern-js/utils/ajv';
2
- import { getDefaultConfig } from './defaults';
3
- import { NormalizedConfig } from './mergeConfig';
4
- import { PluginValidateSchema } from './schema';
5
- import type { UserConfig, ConfigParam, LoadedConfig } from './types';
6
- export { getDefaultConfig };
7
- export * from './mergeConfig';
8
- export * from './types';
9
- export * from './schema';
10
- export declare const addServerConfigToDeps: (dependencies: string[], appDirectory: string, serverConfigFile: string) => Promise<void>;
11
- export declare const defineConfig: (config: ConfigParam) => ConfigParam;
12
- /**
13
- * Assign the pkg config into the user config.
14
- */
15
- export declare const assignPkgConfig: (userConfig?: UserConfig, pkgConfig?: ConfigParam) => UserConfig & ConfigParam;
16
- export declare const loadUserConfig: (appDirectory: string, filePath?: string, packageJsonConfig?: string) => Promise<LoadedConfig>;
17
- export declare const resolveConfig: (loaded: LoadedConfig, configs: UserConfig[], schemas: PluginValidateSchema[], restartWithExistingPort: number, argv: string[], onSchemaError?: (error: ErrorObject) => void | Promise<void>) => Promise<NormalizedConfig>;
1
+ export * from './createLoadedConfig';
2
+ export * from './createResolvedConfig';
@@ -10,143 +10,9 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
15
  };
21
- var __importStar = (this && this.__importStar) || function (mod) {
22
- if (mod && mod.__esModule) return mod;
23
- var result = {};
24
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
- __setModuleDefault(result, mod);
26
- return result;
27
- };
28
16
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.resolveConfig = exports.loadUserConfig = exports.assignPkgConfig = exports.defineConfig = exports.addServerConfigToDeps = exports.getDefaultConfig = void 0;
30
- const utils_1 = require("@modern-js/utils");
31
- const lodash_1 = require("@modern-js/utils/lodash");
32
- const context_1 = require("../context");
33
- const load_configs_1 = require("../load-configs");
34
- const repeatKeyWarning_1 = require("../utils/repeatKeyWarning");
35
- const defaults_1 = require("./defaults");
36
- Object.defineProperty(exports, "getDefaultConfig", { enumerable: true, get: function () { return defaults_1.getDefaultConfig; } });
37
- const mergeConfig_1 = require("./mergeConfig");
38
- const schema_1 = require("./schema");
39
- const debug = (0, utils_1.createDebugger)('resolve-config');
40
- __exportStar(require("./mergeConfig"), exports);
41
- __exportStar(require("./types"), exports);
42
- __exportStar(require("./schema"), exports);
43
- const addServerConfigToDeps = async (dependencies, appDirectory, serverConfigFile) => {
44
- const serverConfig = await (0, utils_1.getServerConfig)(appDirectory, serverConfigFile);
45
- if (serverConfig) {
46
- dependencies.push(serverConfig);
47
- }
48
- };
49
- exports.addServerConfigToDeps = addServerConfigToDeps;
50
- const defineConfig = (config) => config;
51
- exports.defineConfig = defineConfig;
52
- /**
53
- * Assign the pkg config into the user config.
54
- */
55
- const assignPkgConfig = (userConfig = {}, pkgConfig = {}) => (0, lodash_1.mergeWith)({}, userConfig, pkgConfig, (objValue, srcValue) => {
56
- // mergeWith can not merge object with symbol, but plugins object contains symbol,
57
- // so we need to handle it manually.
58
- if (objValue === undefined && (0, utils_1.isPlainObject)(srcValue)) {
59
- return { ...srcValue };
60
- }
61
- // return undefined to use the default behavior of mergeWith
62
- return undefined;
63
- });
64
- exports.assignPkgConfig = assignPkgConfig;
65
- const loadUserConfig = async (appDirectory, filePath, packageJsonConfig) => {
66
- const loaded = await (0, load_configs_1.loadConfig)(appDirectory, filePath, packageJsonConfig);
67
- const config = !loaded
68
- ? {}
69
- : await (typeof loaded.config === 'function'
70
- ? loaded.config(0)
71
- : loaded.config);
72
- return {
73
- config: (0, exports.assignPkgConfig)(config, loaded === null || loaded === void 0 ? void 0 : loaded.pkgConfig),
74
- jsConfig: config || {},
75
- pkgConfig: ((loaded === null || loaded === void 0 ? void 0 : loaded.pkgConfig) || {}),
76
- filePath: loaded === null || loaded === void 0 ? void 0 : loaded.path,
77
- dependencies: (loaded === null || loaded === void 0 ? void 0 : loaded.dependencies) || [],
78
- };
79
- };
80
- exports.loadUserConfig = loadUserConfig;
81
- const showAdditionalPropertiesError = async (error) => {
82
- if (error.keyword === 'additionalProperties' &&
83
- error.params.additionalProperty) {
84
- const target = [
85
- error.instancePath.slice(1),
86
- error.params.additionalProperty,
87
- ]
88
- .filter(Boolean)
89
- .join('.');
90
- const name = Object.keys(utils_1.PLUGIN_SCHEMAS).find(key => utils_1.PLUGIN_SCHEMAS[key].some((schemaItem) => schemaItem.target === target));
91
- if (name) {
92
- const packageManager = await (0, utils_1.getPackageManager)();
93
- utils_1.logger.warn(`The configuration of ${utils_1.chalk.bold(target)} is provided by plugin ${utils_1.chalk.bold(name)}. Please use ${utils_1.chalk.bold(`${packageManager} run new`)} to enable the corresponding capability.\n`);
94
- }
95
- }
96
- };
97
- const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort, argv, onSchemaError = showAdditionalPropertiesError) => {
98
- var _a;
99
- const { default: Ajv } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/ajv')));
100
- const { default: ajvKeywords } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/ajv-keywords')));
101
- const { default: betterAjvErrors } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/better-ajv-errors')));
102
- const { config: userConfig, jsConfig, pkgConfig } = loaded;
103
- const ajv = new Ajv({ $data: true, strict: false });
104
- ajvKeywords(ajv);
105
- const validateSchema = (0, schema_1.patchSchema)(schemas);
106
- const validate = ajv.compile(validateSchema);
107
- (0, repeatKeyWarning_1.repeatKeyWarning)(validateSchema, jsConfig, pkgConfig);
108
- // validate user config.
109
- const valid = validate(userConfig);
110
- const formatValidateError = (config) => {
111
- var _a;
112
- return betterAjvErrors(validateSchema, config, (_a = validate.errors) === null || _a === void 0 ? void 0 : _a.map(e => ({
113
- ...e,
114
- dataPath: e.instancePath,
115
- })), {
116
- indent: 2,
117
- });
118
- };
119
- if (!valid && ((_a = validate.errors) === null || _a === void 0 ? void 0 : _a.length)) {
120
- await onSchemaError(validate === null || validate === void 0 ? void 0 : validate.errors[0]);
121
- const errors = formatValidateError(userConfig);
122
- utils_1.logger.log(errors);
123
- throw new Error(`Validate configuration error`);
124
- }
125
- // validate config from plugins.
126
- for (const config of configs) {
127
- if (!validate(config)) {
128
- const errors = formatValidateError(config);
129
- utils_1.logger.error(errors);
130
- throw new Error(`Validate configuration error.`);
131
- }
132
- }
133
- const resolved = (0, mergeConfig_1.mergeConfig)([
134
- (0, defaults_1.getDefaultConfig)(context_1.AppContext.use().value),
135
- ...configs,
136
- userConfig,
137
- ]);
138
- resolved._raw = loaded.config;
139
- if ((0, utils_1.isDev)() && argv[0] === 'dev') {
140
- if (restartWithExistingPort > 0) {
141
- // dev server is restarted, should use existing port number
142
- resolved.server.port = restartWithExistingPort;
143
- }
144
- else {
145
- // get port for new dev server
146
- resolved.server.port = await (0, utils_1.getPort)(resolved.server.port);
147
- }
148
- }
149
- debug('resolved %o', resolved);
150
- return resolved;
151
- };
152
- exports.resolveConfig = resolveConfig;
17
+ __exportStar(require("./createLoadedConfig"), exports);
18
+ __exportStar(require("./createResolvedConfig"), exports);
package/dist/context.d.ts CHANGED
@@ -1,11 +1,7 @@
1
- import type { IAppContext, InternalPlugins } from '@modern-js/types';
2
- import { UserConfig } from './config';
3
- import { NormalizedConfig } from './config/mergeConfig';
4
- import { CliPlugin } from './manager';
5
- export type { IAppContext };
1
+ import type { CliPlugin, UserConfig, IAppContext, InternalPlugins, NormalizedConfig } from './types';
6
2
  export declare const AppContext: import("@modern-js/plugin").Context<IAppContext>;
7
- export declare const ConfigContext: import("@modern-js/plugin").Context<UserConfig>;
8
- export declare const ResolvedConfigContext: import("@modern-js/plugin").Context<NormalizedConfig>;
3
+ export declare const ConfigContext: import("@modern-js/plugin").Context<UserConfig<any, {}, {}, {}>>;
4
+ export declare const ResolvedConfigContext: import("@modern-js/plugin").Context<NormalizedConfig<any, {}, {}, {}>>;
9
5
  /**
10
6
  * Set app context.
11
7
  * @param value new app context. It will override previous app context.
@@ -18,11 +14,11 @@ export declare const useAppContext: () => IAppContext;
18
14
  /**
19
15
  * Get original content of user config.
20
16
  */
21
- export declare const useConfigContext: () => UserConfig;
17
+ export declare const useConfigContext: <Extends extends Record<string, any>>() => UserConfig<Extends>;
22
18
  /**
23
19
  * Get normalized content of user config.
24
20
  */
25
- export declare const useResolvedConfigContext: () => NormalizedConfig;
21
+ export declare const useResolvedConfigContext: <Extends extends Record<string, any>>() => NormalizedConfig<Extends>;
26
22
  export declare const initAppContext: ({ appDirectory, plugins, configFile, options, serverConfigFile, serverInternalPlugins, }: {
27
23
  appDirectory: string;
28
24
  plugins: CliPlugin[];
package/dist/index.d.ts CHANGED
@@ -1,15 +1,11 @@
1
1
  import type { ErrorObject } from '@modern-js/utils/ajv';
2
2
  import { InternalPlugins } from '@modern-js/types';
3
3
  import { TransformPlugin } from './loadPlugins';
4
- import { IAppContext, initAppContext } from './context';
5
- export * from './config';
6
- export type { Hooks, ImportSpecifier, ImportStatement, RuntimePlugin, } from './types';
4
+ import { initAppContext } from './context';
5
+ export * from './types';
7
6
  export * from '@modern-js/plugin';
8
7
  export { manager, mountHook, createPlugin, registerHook } from './manager';
9
- export type { CliHooks, CliPlugin, CliHookCallbacks } from './manager';
10
- export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext, } from './pluginAPI';
11
- export type { PluginAPI } from './pluginAPI';
12
- export type { IAppContext };
8
+ export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext, } from './context';
13
9
  declare const initAppDir: (cwd?: string) => Promise<string>;
14
10
  export interface CoreOptions {
15
11
  cwd?: string;
@@ -50,17 +46,13 @@ export declare const mergeOptions: (options?: CoreOptions) => {
50
46
  } | undefined;
51
47
  };
52
48
  export declare const cli: {
53
- init: (argv?: string[], options?: CoreOptions) => Promise<{
54
- loadedConfig: import("./config").LoadedConfig;
55
- appContext: IAppContext;
56
- resolved: import("./config").NormalizedConfig;
57
- }>;
58
- run: (argv: string[], options?: CoreOptions) => Promise<void>;
59
- restart: () => Promise<void>;
49
+ init: (options?: CoreOptions) => Promise<void>;
50
+ run: (options?: CoreOptions) => Promise<void>;
60
51
  test: (argv: string[], options?: {
61
52
  coreOptions?: CoreOptions;
62
53
  disableWatcher?: boolean;
63
54
  }) => Promise<void>;
55
+ initOptions: CoreOptions | undefined;
64
56
  };
65
57
  export { initAppDir, initAppContext };
66
58
  declare module '@modern-js/utils/compiled/commander' {
package/dist/index.js CHANGED
@@ -21,14 +21,13 @@ exports.initAppContext = exports.initAppDir = exports.cli = exports.mergeOptions
21
21
  const path_1 = __importDefault(require("path"));
22
22
  const utils_1 = require("@modern-js/utils");
23
23
  const commander_1 = require("./utils/commander");
24
- const config_1 = require("./config");
25
24
  const loadPlugins_1 = require("./loadPlugins");
26
25
  const context_1 = require("./context");
27
26
  Object.defineProperty(exports, "initAppContext", { enumerable: true, get: function () { return context_1.initAppContext; } });
28
- const initWatcher_1 = require("./initWatcher");
29
27
  const loadEnv_1 = require("./loadEnv");
30
28
  const manager_1 = require("./manager");
31
- __exportStar(require("./config"), exports);
29
+ const config_1 = require("./config");
30
+ __exportStar(require("./types"), exports);
32
31
  __exportStar(require("@modern-js/plugin"), exports);
33
32
  // TODO: remove export after refactor all plugins
34
33
  var manager_2 = require("./manager");
@@ -36,14 +35,13 @@ Object.defineProperty(exports, "manager", { enumerable: true, get: function () {
36
35
  Object.defineProperty(exports, "mountHook", { enumerable: true, get: function () { return manager_2.mountHook; } });
37
36
  Object.defineProperty(exports, "createPlugin", { enumerable: true, get: function () { return manager_2.createPlugin; } });
38
37
  Object.defineProperty(exports, "registerHook", { enumerable: true, get: function () { return manager_2.registerHook; } });
39
- // TODO: remove export after refactor all plugins
40
- var pluginAPI_1 = require("./pluginAPI");
41
- Object.defineProperty(exports, "AppContext", { enumerable: true, get: function () { return pluginAPI_1.AppContext; } });
42
- Object.defineProperty(exports, "ConfigContext", { enumerable: true, get: function () { return pluginAPI_1.ConfigContext; } });
43
- Object.defineProperty(exports, "ResolvedConfigContext", { enumerable: true, get: function () { return pluginAPI_1.ResolvedConfigContext; } });
44
- Object.defineProperty(exports, "useAppContext", { enumerable: true, get: function () { return pluginAPI_1.useAppContext; } });
45
- Object.defineProperty(exports, "useConfigContext", { enumerable: true, get: function () { return pluginAPI_1.useConfigContext; } });
46
- Object.defineProperty(exports, "useResolvedConfigContext", { enumerable: true, get: function () { return pluginAPI_1.useResolvedConfigContext; } });
38
+ var context_2 = require("./context");
39
+ Object.defineProperty(exports, "AppContext", { enumerable: true, get: function () { return context_2.AppContext; } });
40
+ Object.defineProperty(exports, "ConfigContext", { enumerable: true, get: function () { return context_2.ConfigContext; } });
41
+ Object.defineProperty(exports, "ResolvedConfigContext", { enumerable: true, get: function () { return context_2.ResolvedConfigContext; } });
42
+ Object.defineProperty(exports, "useAppContext", { enumerable: true, get: function () { return context_2.useAppContext; } });
43
+ Object.defineProperty(exports, "useConfigContext", { enumerable: true, get: function () { return context_2.useConfigContext; } });
44
+ Object.defineProperty(exports, "useResolvedConfigContext", { enumerable: true, get: function () { return context_2.useResolvedConfigContext; } });
47
45
  const initAppDir = async (cwd) => {
48
46
  if (!cwd) {
49
47
  // eslint-disable-next-line no-param-reassign
@@ -71,21 +69,19 @@ const setProgramVersion = (version = 'unknown') => {
71
69
  };
72
70
  const createCli = () => {
73
71
  let hooksRunner;
74
- let isRestart = false;
75
- let restartWithExistingPort = 0;
76
- let restartOptions;
77
- const init = async (argv = [], options) => {
72
+ let initOptions;
73
+ const init = async (options) => {
78
74
  var _a, _b, _c, _d;
79
75
  manager_1.manager.clear();
80
76
  const mergedOptions = (0, exports.mergeOptions)(options);
81
- restartOptions = mergedOptions;
77
+ initOptions = mergedOptions;
82
78
  const appDirectory = await initAppDir(options === null || options === void 0 ? void 0 : options.cwd);
83
79
  (0, commander_1.initCommandsMap)();
84
80
  setProgramVersion(options === null || options === void 0 ? void 0 : options.version);
85
81
  const metaName = (_b = (_a = mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.options) === null || _a === void 0 ? void 0 : _a.metaName) !== null && _b !== void 0 ? _b : 'MODERN';
86
82
  (0, loadEnv_1.loadEnv)(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
87
- const loaded = await (0, config_1.loadUserConfig)(appDirectory, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.configFile, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.packageJsonConfig);
88
- const plugins = (0, loadPlugins_1.loadPlugins)(appDirectory, loaded.config, {
83
+ const loaded = await (0, config_1.createLoadedConfig)(appDirectory, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.configFile, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.packageJsonConfig);
84
+ const plugins = await (0, loadPlugins_1.loadPlugins)(appDirectory, loaded.config, {
89
85
  internalPlugins: (_c = mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.internalPlugins) === null || _c === void 0 ? void 0 : _c.cli,
90
86
  transformPlugin: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.transformPlugin,
91
87
  });
@@ -98,8 +94,6 @@ const createCli = () => {
98
94
  serverConfigFile: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.serverConfigFile,
99
95
  serverInternalPlugins: ((_d = mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.internalPlugins) === null || _d === void 0 ? void 0 : _d.server) || utils_1.INTERNAL_SERVER_PLUGINS,
100
96
  });
101
- // 将 server.config 加入到 loaded.dependencies,以便对文件监听做热更新
102
- (0, config_1.addServerConfigToDeps)(loaded.dependencies, appDirectory, mergedOptions.serverConfigFile);
103
97
  context_1.ConfigContext.set(loaded.config);
104
98
  context_1.AppContext.set(appContext);
105
99
  hooksRunner = await manager_1.manager.init();
@@ -117,68 +111,35 @@ const createCli = () => {
117
111
  });
118
112
  const extraConfigs = await hooksRunner.config();
119
113
  const extraSchemas = await hooksRunner.validateSchema();
120
- const config = await (0, config_1.resolveConfig)(loaded, extraConfigs, extraSchemas, restartWithExistingPort, argv, options === null || options === void 0 ? void 0 : options.onSchemaError);
114
+ const normalizedConfig = await (0, config_1.createResolveConfig)(loaded, extraConfigs, extraSchemas, options === null || options === void 0 ? void 0 : options.onSchemaError);
121
115
  const { resolved } = await hooksRunner.resolvedConfig({
122
- resolved: config,
116
+ resolved: normalizedConfig,
123
117
  });
118
+ // FIXME: Why need to configContext again?
124
119
  // update context value
125
120
  context_1.ConfigContext.set(loaded.config);
126
121
  context_1.ResolvedConfigContext.set(resolved);
127
- context_1.AppContext.set({
128
- ...appContext,
129
- port: resolved.server.port,
130
- distDirectory: (0, utils_1.ensureAbsolutePath)(appDirectory, resolved.output.path),
131
- });
122
+ // TODO: confirm the `addRuntimeExports` run order
123
+ await hooksRunner.addRuntimeExports();
132
124
  await hooksRunner.prepare();
133
- return {
134
- loadedConfig: loaded,
135
- // appContext may be updated in `prepare` hook, should return latest value
136
- appContext: (0, context_1.useAppContext)(),
137
- resolved,
138
- };
139
125
  };
140
- async function run(argv, options) {
141
- const { loadedConfig, appContext, resolved } = await init(argv, options);
126
+ async function run(options) {
127
+ await init(options);
142
128
  await hooksRunner.commands({ program: utils_1.program });
143
- (0, initWatcher_1.initWatcher)(loadedConfig, appContext.appDirectory, resolved.source.configDir, hooksRunner, argv);
144
129
  utils_1.program.parse(process.argv);
145
130
  }
146
- async function restart() {
147
- var _a, _b;
148
- isRestart = true;
149
- restartWithExistingPort = isRestart ? (_b = (_a = context_1.AppContext.use().value) === null || _a === void 0 ? void 0 : _a.port) !== null && _b !== void 0 ? _b : 0 : 0;
150
- utils_1.logger.info('Restart...\n');
151
- let hasGetError = false;
152
- const runner = manager_1.manager.useRunner();
153
- await runner.beforeRestart();
154
- try {
155
- await init(process.argv.slice(2), restartOptions);
156
- }
157
- catch (err) {
158
- console.error(err);
159
- hasGetError = true;
160
- }
161
- finally {
162
- if (!hasGetError) {
163
- utils_1.program.parse(process.argv);
164
- }
165
- }
166
- }
167
131
  async function test(argv, options) {
168
132
  const newProgram = new utils_1.Command();
169
133
  const { coreOptions } = options !== null && options !== void 0 ? options : {};
170
- const { loadedConfig, appContext, resolved } = await init(argv, coreOptions);
134
+ await init(coreOptions);
171
135
  await hooksRunner.commands({ program: newProgram });
172
- if (!(options === null || options === void 0 ? void 0 : options.disableWatcher)) {
173
- (0, initWatcher_1.initWatcher)(loadedConfig, appContext.appDirectory, resolved.source.configDir, hooksRunner, argv);
174
- }
175
136
  await newProgram.parseAsync(argv);
176
137
  }
177
138
  return {
178
139
  init,
179
140
  run,
180
- restart,
181
141
  test,
142
+ initOptions,
182
143
  };
183
144
  };
184
145
  exports.cli = createCli();
@@ -1,15 +1,6 @@
1
1
  import { InternalPlugins } from '@modern-js/types';
2
- import type { UserConfig } from './config';
3
- import { CliPlugin } from './manager';
4
- declare type PluginItem = string | [string, any];
2
+ import type { CliPlugin, UserConfig, OldPluginConfig, PluginConfig } from './types';
5
3
  export declare type TransformPlugin = (plugin: PluginConfig, resolvedConfig: UserConfig, pluginOptions?: any) => PluginConfig;
6
- /**
7
- * @deprecated
8
- * Using NewPluginConfig instead.
9
- */
10
- declare type OldPluginConfig = Array<PluginItem>;
11
- export declare type NewPluginConfig<T = {}> = CliPlugin<T>[];
12
- export declare type PluginConfig = OldPluginConfig | NewPluginConfig;
13
4
  export declare const isOldPluginConfig: (config?: PluginConfig) => config is OldPluginConfig;
14
5
  /**
15
6
  * Load internal plugins which in @modern-js scope and user's custom plugins.
@@ -22,5 +13,4 @@ export declare const isOldPluginConfig: (config?: PluginConfig) => config is Old
22
13
  export declare const loadPlugins: (appDirectory: string, userConfig: UserConfig, options?: {
23
14
  internalPlugins?: InternalPlugins;
24
15
  transformPlugin?: TransformPlugin;
25
- }) => CliPlugin<{}>[];
26
- export {};
16
+ }) => Promise<CliPlugin<{}, {}, {}, {}>[]>;