@modern-js/core 1.6.1 → 1.7.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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # @modern-js/core
2
2
 
3
+ ## 1.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - d2d1d6b2: feat: support server config
8
+
9
+ ### Patch Changes
10
+
11
+ - 60855eb2: fix: ignore initial watching add event
12
+ - ec1b7367: fix: tools config types
13
+ - 07a4887e: feat: prebundle commander and signale to @modern-js/utils
14
+ - 17d0cc46: feat: prebundle lodash to @modern-js/utils/lodash
15
+ - Updated dependencies [77ff9754]
16
+ - Updated dependencies [d2d1d6b2]
17
+ - Updated dependencies [07a4887e]
18
+ - Updated dependencies [ea2ae711]
19
+ - Updated dependencies [17d0cc46]
20
+ - Updated dependencies [d2d1d6b2]
21
+ - @modern-js/utils@1.4.0
22
+ - @modern-js/load-config@1.3.0
23
+
3
24
  ## 1.6.1
4
25
 
5
26
  ### Patch Changes
@@ -7,9 +7,8 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
7
7
  import { loadConfig } from '@modern-js/load-config';
8
8
  import Ajv from 'ajv';
9
9
  import ajvKeywords from 'ajv-keywords';
10
- import logger from 'signale';
11
- import { createDebugger, getPort, isDev, PLUGIN_SCHEMAS, chalk } from '@modern-js/utils';
12
- import mergeWith from 'lodash.mergewith';
10
+ import { signale as logger, createDebugger, getPort, isDev, PLUGIN_SCHEMAS, chalk, getServerConfig } from '@modern-js/utils';
11
+ import { mergeWith } from '@modern-js/utils/lodash';
13
12
  import betterAjvErrors from 'better-ajv-errors';
14
13
  import { codeFrameColumns } from '@babel/code-frame';
15
14
  import { repeatKeyWarning } from "../utils/repeatKeyWarning";
@@ -20,6 +19,13 @@ const debug = createDebugger('resolve-config');
20
19
  export { defaults as defaultsConfig };
21
20
  export { mergeConfig };
22
21
  export * from "./types";
22
+ export const addServerConfigToDeps = async (dependencies, appDirectory, serverConfigFile) => {
23
+ const serverConfig = await getServerConfig(appDirectory, serverConfigFile);
24
+
25
+ if (serverConfig) {
26
+ dependencies.push(serverConfig);
27
+ }
28
+ };
23
29
  export const defineConfig = config => config;
24
30
  export const loadUserConfig = async (appDirectory, filePath, packageJsonConfig) => {
25
31
  const loaded = await loadConfig(appDirectory, filePath, packageJsonConfig);
@@ -43,8 +49,6 @@ const showAdditionalPropertiesError = error => {
43
49
  }
44
50
  }
45
51
  };
46
- /* eslint-disable max-statements, max-params */
47
-
48
52
 
49
53
  export const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort, argv, onSchemaError = showAdditionalPropertiesError) => {
50
54
  var _validate$errors;
@@ -108,5 +112,4 @@ export const resolveConfig = async (loaded, configs, schemas, restartWithExistin
108
112
 
109
113
  debug('resolved %o', resolved);
110
114
  return resolved;
111
- };
112
- /* eslint-enable max-statements, max-params */
115
+ };
@@ -1,4 +1,4 @@
1
- import mergeWith from 'lodash.mergewith';
1
+ import { mergeWith } from '@modern-js/utils/lodash';
2
2
  import { isFunction } from '@modern-js/utils';
3
3
 
4
4
  /**
@@ -1,5 +1,5 @@
1
1
  import { isObject, createDebugger } from '@modern-js/utils';
2
- import cloneDeep from 'lodash.clonedeep';
2
+ import { cloneDeep } from '@modern-js/utils/lodash';
3
3
  import { source } from "./source";
4
4
  import { output } from "./output";
5
5
  import { server } from "./server";
@@ -0,0 +1 @@
1
+ export {};
@@ -25,7 +25,13 @@ export const useConfigContext = () => ConfigContext.use().value;
25
25
  */
26
26
 
27
27
  export const useResolvedConfigContext = () => ResolvedConfigContext.use().value;
28
- export const initAppContext = (appDirectory, plugins, configFile, options) => {
28
+ export const initAppContext = ({
29
+ appDirectory,
30
+ plugins,
31
+ configFile,
32
+ options,
33
+ serverConfigFile
34
+ }) => {
29
35
  const {
30
36
  metaName = 'modern-js',
31
37
  srcDir = 'src',
@@ -36,6 +42,7 @@ export const initAppContext = (appDirectory, plugins, configFile, options) => {
36
42
  metaName,
37
43
  appDirectory,
38
44
  configFile,
45
+ serverConfigFile,
39
46
  ip: address.ip(),
40
47
  port: 0,
41
48
  packageName: require(path.resolve(appDirectory, './package.json')).name,
@@ -5,10 +5,10 @@ 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 { pkgUp, ensureAbsolutePath, logger } from '@modern-js/utils';
8
+ import { pkgUp, program, ensureAbsolutePath, logger, DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
9
9
  import { enable } from '@modern-js/plugin/node';
10
- import { program } from "./utils/commander";
11
- import { resolveConfig, loadUserConfig } from "./config";
10
+ import { initCommandsMap } from "./utils/commander";
11
+ import { resolveConfig, loadUserConfig, addServerConfigToDeps } from "./config";
12
12
  import { loadPlugins } from "./loadPlugins";
13
13
  import { AppContext, ConfigContext, initAppContext, ResolvedConfigContext } from "./context";
14
14
  import { initWatcher } from "./initWatcher";
@@ -40,6 +40,13 @@ const initAppDir = async cwd => {
40
40
  return path.dirname(pkg);
41
41
  };
42
42
 
43
+ export const mergeOptions = options => {
44
+ const defaultOptions = {
45
+ serverConfigFile: DEFAULT_SERVER_CONFIG
46
+ };
47
+ return _objectSpread(_objectSpread({}, defaultOptions), options);
48
+ };
49
+
43
50
  const createCli = () => {
44
51
  let hooksRunner;
45
52
  let isRestart = false;
@@ -47,20 +54,30 @@ const createCli = () => {
47
54
  let restartOptions;
48
55
 
49
56
  const init = async (argv = [], options) => {
50
- var _options$options$meta, _options$options;
57
+ var _mergedOptions$option, _mergedOptions$option2;
51
58
 
52
59
  enable();
53
60
  manager.clear();
54
- restartOptions = options;
61
+ const mergedOptions = mergeOptions(options);
62
+ restartOptions = mergedOptions;
55
63
  const appDirectory = await initAppDir();
56
- 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';
64
+ initCommandsMap();
65
+ const metaName = (_mergedOptions$option = mergedOptions === null || mergedOptions === void 0 ? void 0 : (_mergedOptions$option2 = mergedOptions.options) === null || _mergedOptions$option2 === void 0 ? void 0 : _mergedOptions$option2.metaName) !== null && _mergedOptions$option !== void 0 ? _mergedOptions$option : 'MODERN';
57
66
  loadEnv(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
58
- const loaded = await loadUserConfig(appDirectory, options === null || options === void 0 ? void 0 : options.configFile, options === null || options === void 0 ? void 0 : options.packageJsonConfig);
67
+ const loaded = await loadUserConfig(appDirectory, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.configFile, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.packageJsonConfig);
59
68
  const plugins = loadPlugins(appDirectory, loaded.config, {
60
- internalPlugins: options === null || options === void 0 ? void 0 : options.plugins
69
+ internalPlugins: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.plugins
61
70
  });
62
71
  plugins.forEach(plugin => plugin.cli && manager.usePlugin(plugin.cli));
63
- const appContext = initAppContext(appDirectory, plugins, loaded.filePath, options === null || options === void 0 ? void 0 : options.options);
72
+ const appContext = initAppContext({
73
+ appDirectory,
74
+ plugins,
75
+ configFile: loaded.filePath,
76
+ options: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.options,
77
+ serverConfigFile: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.serverConfigFile
78
+ }); // 将 server.config 加入到 loaded.dependencies,以便对文件监听做热更新
79
+
80
+ addServerConfigToDeps(loaded.dependencies, appDirectory, mergedOptions.serverConfigFile);
64
81
  manager.run(() => {
65
82
  ConfigContext.set(loaded.config);
66
83
  AppContext.set(appContext);
@@ -1,8 +1,7 @@
1
1
  import crypto from 'crypto';
2
2
  import fs from 'fs';
3
3
  import path from 'path';
4
- import { isDev, createDebugger, isTest } from '@modern-js/utils';
5
- import chokidar from 'chokidar';
4
+ import { isDev, chokidar, createDebugger, isTest } from '@modern-js/utils';
6
5
  const debug = createDebugger('watch-files');
7
6
 
8
7
  const md5 = data => crypto.createHash('md5').update(data).digest('hex');
@@ -1,19 +1,19 @@
1
- import { program, Command } from 'commander';
1
+ import { program, Command } from '@modern-js/utils';
2
+ export function initCommandsMap() {
3
+ if (!program.hasOwnProperty('commandsMap')) {
4
+ Object.defineProperty(program, 'commandsMap', {
5
+ get() {
6
+ const map = new Map();
2
7
 
3
- if (!program.hasOwnProperty('commandsMap')) {
4
- Object.defineProperty(program, 'commandsMap', {
5
- get() {
6
- const map = new Map();
8
+ for (const command of program.commands) {
9
+ map.set(command._name, command);
10
+ }
7
11
 
8
- for (const command of program.commands) {
9
- map.set(command._name, command);
10
- }
12
+ return map;
13
+ },
11
14
 
12
- return map;
13
- },
14
-
15
- configurable: false
16
- });
15
+ configurable: false
16
+ });
17
+ }
17
18
  }
18
-
19
- export { program, Command };
19
+ export { program };
@@ -4,12 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  var _exportNames = {
7
+ addServerConfigToDeps: true,
7
8
  defineConfig: true,
8
9
  loadUserConfig: true,
9
10
  resolveConfig: true,
10
11
  defaultsConfig: true,
11
12
  mergeConfig: true
12
13
  };
14
+ exports.addServerConfigToDeps = void 0;
13
15
  Object.defineProperty(exports, "defaultsConfig", {
14
16
  enumerable: true,
15
17
  get: function () {
@@ -31,11 +33,9 @@ var _ajv = _interopRequireDefault(require("ajv"));
31
33
 
32
34
  var _ajvKeywords = _interopRequireDefault(require("ajv-keywords"));
33
35
 
34
- var _signale = _interopRequireDefault(require("signale"));
35
-
36
36
  var _utils = require("@modern-js/utils");
37
37
 
38
- var _lodash = _interopRequireDefault(require("lodash.mergewith"));
38
+ var _lodash = require("@modern-js/utils/lodash");
39
39
 
40
40
  var _betterAjvErrors = _interopRequireDefault(require("better-ajv-errors"));
41
41
 
@@ -73,6 +73,16 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
73
73
 
74
74
  const debug = (0, _utils.createDebugger)('resolve-config');
75
75
 
76
+ const addServerConfigToDeps = async (dependencies, appDirectory, serverConfigFile) => {
77
+ const serverConfig = await (0, _utils.getServerConfig)(appDirectory, serverConfigFile);
78
+
79
+ if (serverConfig) {
80
+ dependencies.push(serverConfig);
81
+ }
82
+ };
83
+
84
+ exports.addServerConfigToDeps = addServerConfigToDeps;
85
+
76
86
  const defineConfig = config => config;
77
87
 
78
88
  exports.defineConfig = defineConfig;
@@ -81,7 +91,7 @@ const loadUserConfig = async (appDirectory, filePath, packageJsonConfig) => {
81
91
  const loaded = await (0, _loadConfig.loadConfig)(appDirectory, filePath, packageJsonConfig);
82
92
  const config = !loaded ? {} : await (typeof loaded.config === 'function' ? loaded.config(0) : loaded.config);
83
93
  return {
84
- config: (0, _lodash.default)({}, config || {}, (loaded === null || loaded === void 0 ? void 0 : loaded.pkgConfig) || {}),
94
+ config: (0, _lodash.mergeWith)({}, config || {}, (loaded === null || loaded === void 0 ? void 0 : loaded.pkgConfig) || {}),
85
95
  jsConfig: config || {},
86
96
  pkgConfig: (loaded === null || loaded === void 0 ? void 0 : loaded.pkgConfig) || {},
87
97
  filePath: loaded === null || loaded === void 0 ? void 0 : loaded.path,
@@ -97,12 +107,10 @@ const showAdditionalPropertiesError = error => {
97
107
  const name = Object.keys(_utils.PLUGIN_SCHEMAS).find(key => _utils.PLUGIN_SCHEMAS[key].some(schemaItem => schemaItem.target === target));
98
108
 
99
109
  if (name) {
100
- _signale.default.warn(`The configuration of ${_utils.chalk.bold(target)} is provided by plugin ${_utils.chalk.bold(name)}. Please use ${_utils.chalk.bold('yarn new')} to enable the corresponding capability.\n`);
110
+ _utils.signale.warn(`The configuration of ${_utils.chalk.bold(target)} is provided by plugin ${_utils.chalk.bold(name)}. Please use ${_utils.chalk.bold('yarn new')} to enable the corresponding capability.\n`);
101
111
  }
102
112
  }
103
113
  };
104
- /* eslint-disable max-statements, max-params */
105
-
106
114
 
107
115
  const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort, argv, onSchemaError = showAdditionalPropertiesError) => {
108
116
  var _validate$errors;
@@ -134,7 +142,7 @@ const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort,
134
142
  indent: 2
135
143
  });
136
144
 
137
- _signale.default.log((0, _codeFrame.codeFrameColumns)(JSON.stringify(userConfig, null, 2), {
145
+ _utils.signale.log((0, _codeFrame.codeFrameColumns)(JSON.stringify(userConfig, null, 2), {
138
146
  start: errors === null || errors === void 0 ? void 0 : errors[0].start,
139
147
  end: errors === null || errors === void 0 ? void 0 : errors[0].end
140
148
  }, {
@@ -148,7 +156,7 @@ const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort,
148
156
 
149
157
  for (const config of configs) {
150
158
  if (!validate(config)) {
151
- _signale.default.error(validate.errors);
159
+ _utils.signale.error(validate.errors);
152
160
 
153
161
  throw new Error(`Validate configuration error.`);
154
162
  }
@@ -170,7 +178,5 @@ const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort,
170
178
  debug('resolved %o', resolved);
171
179
  return resolved;
172
180
  };
173
- /* eslint-enable max-statements, max-params */
174
-
175
181
 
176
182
  exports.resolveConfig = resolveConfig;
@@ -5,19 +5,17 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.mergeConfig = void 0;
7
7
 
8
- var _lodash = _interopRequireDefault(require("lodash.mergewith"));
8
+ var _lodash = require("@modern-js/utils/lodash");
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
12
  /**
15
13
  * merge configuration from modern.config.js and plugins.
16
14
  *
17
15
  * @param configs - Configuration from modern.config.ts or plugin's config hook.
18
16
  * @returns - normalized user config.
19
17
  */
20
- const mergeConfig = configs => (0, _lodash.default)({}, ...configs, (target, source) => {
18
+ const mergeConfig = configs => (0, _lodash.mergeWith)({}, ...configs, (target, source) => {
21
19
  if (Array.isArray(target)) {
22
20
  if (Array.isArray(source)) {
23
21
  return [...target, ...source];
@@ -7,7 +7,7 @@ exports.traverseSchema = exports.patchSchema = void 0;
7
7
 
8
8
  var _utils = require("@modern-js/utils");
9
9
 
10
- var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
10
+ var _lodash = require("@modern-js/utils/lodash");
11
11
 
12
12
  var _source = require("./source");
13
13
 
@@ -19,8 +19,6 @@ var _deploy = require("./deploy");
19
19
 
20
20
  var _tools = require("./tools");
21
21
 
22
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
22
  const debug = (0, _utils.createDebugger)('validate-schema');
25
23
  const plugins = {
26
24
  type: 'array',
@@ -40,7 +38,7 @@ const dev = {
40
38
  };
41
39
 
42
40
  const patchSchema = pluginSchemas => {
43
- const finalSchema = (0, _lodash.default)({
41
+ const finalSchema = (0, _lodash.cloneDeep)({
44
42
  type: 'object',
45
43
  additionalProperties: false,
46
44
  properties: {
@@ -96,7 +94,7 @@ const patchSchema = pluginSchemas => {
96
94
  throw new Error(`${target} already exists in current validate schema`);
97
95
  }
98
96
 
99
- targetNode[mountProperty] = (0, _lodash.default)(schema);
97
+ targetNode[mountProperty] = (0, _lodash.cloneDeep)(schema);
100
98
  }
101
99
 
102
100
  debug(`final validate schema: %o`, finalSchema);
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -53,7 +53,13 @@ const useResolvedConfigContext = () => ResolvedConfigContext.use().value;
53
53
 
54
54
  exports.useResolvedConfigContext = useResolvedConfigContext;
55
55
 
56
- const initAppContext = (appDirectory, plugins, configFile, options) => {
56
+ const initAppContext = ({
57
+ appDirectory,
58
+ plugins,
59
+ configFile,
60
+ options,
61
+ serverConfigFile
62
+ }) => {
57
63
  const {
58
64
  metaName = 'modern-js',
59
65
  srcDir = 'src',
@@ -64,6 +70,7 @@ const initAppContext = (appDirectory, plugins, configFile, options) => {
64
70
  metaName,
65
71
  appDirectory,
66
72
  configFile,
73
+ serverConfigFile,
67
74
  ip: _address.default.ip(),
68
75
  port: 0,
69
76
  packageName: require(_path.default.resolve(appDirectory, './package.json')).name,
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  var _exportNames = {
7
+ mergeOptions: true,
7
8
  cli: true,
8
9
  initAppDir: true,
9
10
  initAppContext: true,
@@ -57,6 +58,7 @@ Object.defineProperty(exports, "manager", {
57
58
  return _manager.manager;
58
59
  }
59
60
  });
61
+ exports.mergeOptions = void 0;
60
62
  Object.defineProperty(exports, "mountHook", {
61
63
  enumerable: true,
62
64
  get: function () {
@@ -162,7 +164,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
162
164
 
163
165
  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; }
164
166
 
165
- _commander.program.name('modern').usage('<command> [options]').version(process.env.MODERN_JS_VERSION || '0.1.0');
167
+ _utils.program.name('modern').usage('<command> [options]').version(process.env.MODERN_JS_VERSION || '0.1.0');
166
168
 
167
169
  const initAppDir = async cwd => {
168
170
  if (!cwd) {
@@ -183,6 +185,15 @@ const initAppDir = async cwd => {
183
185
 
184
186
  exports.initAppDir = initAppDir;
185
187
 
188
+ const mergeOptions = options => {
189
+ const defaultOptions = {
190
+ serverConfigFile: _utils.DEFAULT_SERVER_CONFIG
191
+ };
192
+ return _objectSpread(_objectSpread({}, defaultOptions), options);
193
+ };
194
+
195
+ exports.mergeOptions = mergeOptions;
196
+
186
197
  const createCli = () => {
187
198
  let hooksRunner;
188
199
  let isRestart = false;
@@ -190,22 +201,32 @@ const createCli = () => {
190
201
  let restartOptions;
191
202
 
192
203
  const init = async (argv = [], options) => {
193
- var _options$options$meta, _options$options;
204
+ var _mergedOptions$option, _mergedOptions$option2;
194
205
 
195
206
  (0, _node.enable)();
196
207
 
197
208
  _manager.manager.clear();
198
209
 
199
- restartOptions = options;
210
+ const mergedOptions = mergeOptions(options);
211
+ restartOptions = mergedOptions;
200
212
  const appDirectory = await initAppDir();
201
- 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';
213
+ (0, _commander.initCommandsMap)();
214
+ const metaName = (_mergedOptions$option = mergedOptions === null || mergedOptions === void 0 ? void 0 : (_mergedOptions$option2 = mergedOptions.options) === null || _mergedOptions$option2 === void 0 ? void 0 : _mergedOptions$option2.metaName) !== null && _mergedOptions$option !== void 0 ? _mergedOptions$option : 'MODERN';
202
215
  (0, _loadEnv.loadEnv)(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
203
- 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);
216
+ const loaded = await (0, _config.loadUserConfig)(appDirectory, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.configFile, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.packageJsonConfig);
204
217
  const plugins = (0, _loadPlugins.loadPlugins)(appDirectory, loaded.config, {
205
- internalPlugins: options === null || options === void 0 ? void 0 : options.plugins
218
+ internalPlugins: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.plugins
206
219
  });
207
220
  plugins.forEach(plugin => plugin.cli && _manager.manager.usePlugin(plugin.cli));
208
- const appContext = (0, _context.initAppContext)(appDirectory, plugins, loaded.filePath, options === null || options === void 0 ? void 0 : options.options);
221
+ const appContext = (0, _context.initAppContext)({
222
+ appDirectory,
223
+ plugins,
224
+ configFile: loaded.filePath,
225
+ options: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.options,
226
+ serverConfigFile: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.serverConfigFile
227
+ }); // 将 server.config 加入到 loaded.dependencies,以便对文件监听做热更新
228
+
229
+ (0, _config.addServerConfigToDeps)(loaded.dependencies, appDirectory, mergedOptions.serverConfigFile);
209
230
 
210
231
  _manager.manager.run(() => {
211
232
  _context.ConfigContext.set(loaded.config);
@@ -263,11 +284,11 @@ const createCli = () => {
263
284
  resolved
264
285
  } = await init(argv, options);
265
286
  await hooksRunner.commands({
266
- program: _commander.program
287
+ program: _utils.program
267
288
  });
268
289
  (0, _initWatcher.initWatcher)(loadedConfig, appContext.appDirectory, resolved.source.configDir, hooksRunner, argv);
269
290
 
270
- _manager.manager.run(() => _commander.program.parse(process.argv));
291
+ _manager.manager.run(() => _utils.program.parse(process.argv));
271
292
  }
272
293
 
273
294
  async function restart() {
@@ -291,7 +312,7 @@ const createCli = () => {
291
312
  hasGetError = true;
292
313
  } finally {
293
314
  if (!hasGetError) {
294
- _manager.manager.run(() => _commander.program.parse(process.argv));
315
+ _manager.manager.run(() => _utils.program.parse(process.argv));
295
316
  }
296
317
  }
297
318
  }
@@ -13,8 +13,6 @@ var _path = _interopRequireDefault(require("path"));
13
13
 
14
14
  var _utils = require("@modern-js/utils");
15
15
 
16
- var _chokidar = _interopRequireDefault(require("chokidar"));
17
-
18
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
17
 
20
18
  const debug = (0, _utils.createDebugger)('watch-files');
@@ -33,7 +31,7 @@ const initWatcher = async (loaded, appDirectory, configDir, hooksRunner, argv) =
33
31
  const watched = [`${configPath}/html`, ...extraFiles, loaded === null || loaded === void 0 ? void 0 : loaded.filePath, ...loaded.dependencies].filter(Boolean);
34
32
  debug(`watched: %o`, watched);
35
33
 
36
- const watcher = _chokidar.default.watch(watched, {
34
+ const watcher = _utils.chokidar.watch(watched, {
37
35
  cwd: appDirectory,
38
36
  ignoreInitial: true,
39
37
  ignorePermissionErrors: true,
@@ -3,33 +3,30 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- Object.defineProperty(exports, "Command", {
7
- enumerable: true,
8
- get: function () {
9
- return _commander.Command;
10
- }
11
- });
6
+ exports.initCommandsMap = initCommandsMap;
12
7
  Object.defineProperty(exports, "program", {
13
8
  enumerable: true,
14
9
  get: function () {
15
- return _commander.program;
10
+ return _utils.program;
16
11
  }
17
12
  });
18
13
 
19
- var _commander = require("commander");
14
+ var _utils = require("@modern-js/utils");
20
15
 
21
- if (!_commander.program.hasOwnProperty('commandsMap')) {
22
- Object.defineProperty(_commander.program, 'commandsMap', {
23
- get() {
24
- const map = new Map();
16
+ function initCommandsMap() {
17
+ if (!_utils.program.hasOwnProperty('commandsMap')) {
18
+ Object.defineProperty(_utils.program, 'commandsMap', {
19
+ get() {
20
+ const map = new Map();
25
21
 
26
- for (const command of _commander.program.commands) {
27
- map.set(command._name, command);
28
- }
22
+ for (const command of _utils.program.commands) {
23
+ map.set(command._name, command);
24
+ }
29
25
 
30
- return map;
31
- },
26
+ return map;
27
+ },
32
28
 
33
- configurable: false
34
- });
29
+ configurable: false
30
+ });
31
+ }
35
32
  }
@@ -6,6 +6,7 @@ import type { UserConfig, ConfigParam, LoadedConfig } from './types';
6
6
  export { defaults as defaultsConfig };
7
7
  export { mergeConfig };
8
8
  export * from './types';
9
+ export declare const addServerConfigToDeps: (dependencies: string[], appDirectory: string, serverConfigFile: string) => Promise<void>;
9
10
  export declare const defineConfig: (config: ConfigParam) => ConfigParam;
10
11
  export declare const loadUserConfig: (appDirectory: string, filePath?: string | undefined, packageJsonConfig?: string | undefined) => Promise<LoadedConfig>;
11
12
  export declare const resolveConfig: (loaded: LoadedConfig, configs: UserConfig[], schemas: PluginValidateSchema[], restartWithExistingPort: number, argv: string[], onSchemaError?: (error: ErrorObject) => void) => Promise<NormalizedConfig>;
@@ -3,13 +3,18 @@ import type { IncomingMessage, ServerResponse } from 'http';
3
3
  import type { NextFunction, BffProxyOptions } from '@modern-js/types';
4
4
  import type { MetaOptions } from '@modern-js/utils';
5
5
  import type { TransformOptions } from '@babel/core';
6
+ import type { Configuration as WebpackConfiguration } from 'webpack';
7
+ import autoprefixer from 'autoprefixer';
8
+ import type { BasePluginOptions, TerserOptions as RawTerserOptions } from 'terser-webpack-plugin';
6
9
  import type { PluginConfig } from '../../loadPlugins';
7
10
  import type { TestConfig, JestConfig } from './test';
8
11
  import type { SassConfig, SassLoaderOptions } from './sass';
9
12
  import type { LessConfig, LessLoaderOptions } from './less';
10
13
  import type { UnbundleConfig } from './unbundle';
11
14
  import type { SSGConfig, SSGRouteOptions, SSGMultiEntryOptions, SSGSingleEntryOptions } from './ssg';
12
- export type { TestConfig, JestConfig, UnbundleConfig, SassConfig, SassLoaderOptions, LessConfig, LessLoaderOptions, SSGConfig, SSGRouteOptions, SSGMultiEntryOptions, SSGSingleEntryOptions, TransformOptions };
15
+ declare type AutoprefixerOptions = autoprefixer.Options;
16
+ declare type TerserOptions = BasePluginOptions & RawTerserOptions;
17
+ export type { TestConfig, JestConfig, UnbundleConfig, SassConfig, SassLoaderOptions, LessConfig, LessLoaderOptions, SSGConfig, SSGRouteOptions, SSGMultiEntryOptions, SSGSingleEntryOptions, TransformOptions, AutoprefixerOptions, TerserOptions };
13
18
  export interface SourceConfig {
14
19
  entries?: Record<string, string | {
15
20
  entry: string;
@@ -135,7 +140,7 @@ export interface DeployConfig {
135
140
  domain?: string | Array<string>;
136
141
  domainByEntries?: Record<string, string | Array<string>>;
137
142
  }
138
- declare type ConfigFunction = Record<string, unknown> | ((config: Record<string, unknown>) => Record<string, unknown> | void);
143
+ declare type ConfigFunction = Record<string, unknown> | ((config: Record<string, unknown>, utils?: any) => Record<string, unknown> | void);
139
144
  export declare type RequestHandler = (req: IncomingMessage, res: ServerResponse, next: NextFunction) => void;
140
145
  export declare type DevServerConfig = {
141
146
  proxy?: BffProxyOptions;
@@ -144,17 +149,20 @@ export declare type DevServerConfig = {
144
149
  after?: RequestHandler[];
145
150
  [propsName: string]: any;
146
151
  };
147
- export declare type BabelConfig = TransformOptions | ((config: TransformOptions) => TransformOptions | void);
152
+ export declare type WebpackConfig = WebpackConfiguration | ((config: WebpackConfiguration, utils?: any) => WebpackConfiguration | void);
153
+ export declare type BabelConfig = TransformOptions | ((config: TransformOptions, utils?: any) => TransformOptions | void);
154
+ export declare type AutoprefixerConfig = AutoprefixerOptions | ((config: AutoprefixerOptions) => AutoprefixerOptions | void);
155
+ export declare type TerserConfig = TerserOptions | ((config: TerserOptions) => TerserOptions | void);
148
156
  export interface ToolsConfig {
149
- webpack?: ConfigFunction;
157
+ webpack?: WebpackConfig;
150
158
  babel?: BabelConfig;
151
- autoprefixer?: ConfigFunction;
159
+ autoprefixer?: AutoprefixerConfig;
152
160
  postcss?: ConfigFunction;
153
161
  styledComponents?: ConfigFunction;
154
162
  lodash?: ConfigFunction;
155
163
  devServer?: DevServerConfig;
156
164
  tsLoader?: ConfigFunction;
157
- terser?: ConfigFunction;
165
+ terser?: TerserConfig;
158
166
  minifyCss?: ConfigFunction;
159
167
  esbuild?: Record<string, unknown>;
160
168
  /**
@@ -190,12 +198,12 @@ export declare type RuntimeConfig = Record<string, any>;
190
198
  export interface RuntimeByEntriesConfig {
191
199
  [name: string]: RuntimeConfig;
192
200
  }
193
- export declare type BffConfig = {
194
- prefix?: string;
195
- requestCreator?: string;
196
- fetcher?: string;
197
- proxy?: Record<string, any>;
198
- };
201
+ export declare type BffConfig = Partial<{
202
+ prefix: string;
203
+ requestCreator: string;
204
+ fetcher: string;
205
+ proxy: Record<string, any>;
206
+ }>;
199
207
  export interface UserConfig {
200
208
  source?: SourceConfig;
201
209
  output?: OutputConfig;
@@ -7,4 +7,4 @@ export declare type LessLoaderOptions = {
7
7
  webpackImporter?: boolean;
8
8
  implementation?: boolean;
9
9
  };
10
- export declare type LessConfig = LessLoaderOptions | ((options: LessLoaderOptions) => LessLoaderOptions | void);
10
+ export declare type LessConfig = LessLoaderOptions | ((options: LessLoaderOptions, utils?: any) => LessLoaderOptions | void);
@@ -5,4 +5,4 @@ export interface SassLoaderOptions {
5
5
  implementation?: string;
6
6
  additionalData?: string | ((content: string, filename: string) => string);
7
7
  }
8
- export declare type SassConfig = SassLoaderOptions | ((options: SassLoaderOptions) => SassLoaderOptions | void);
8
+ export declare type SassConfig = SassLoaderOptions | ((options: SassLoaderOptions, utils?: any) => SassLoaderOptions | void);
@@ -27,9 +27,21 @@ export declare const useConfigContext: () => UserConfig;
27
27
  */
28
28
 
29
29
  export declare const useResolvedConfigContext: () => NormalizedConfig;
30
- export declare const initAppContext: (appDirectory: string, plugins: LoadedPlugin[], configFile: string | false, options?: {
31
- metaName?: string | undefined;
32
- srcDir?: string | undefined;
33
- distDir?: string | undefined;
34
- sharedDir?: string | undefined;
35
- } | undefined) => IAppContext;
30
+ export declare const initAppContext: ({
31
+ appDirectory,
32
+ plugins,
33
+ configFile,
34
+ options,
35
+ serverConfigFile
36
+ }: {
37
+ appDirectory: string;
38
+ plugins: LoadedPlugin[];
39
+ configFile: string | false;
40
+ options?: {
41
+ metaName?: string | undefined;
42
+ srcDir?: string | undefined;
43
+ distDir?: string | undefined;
44
+ sharedDir?: string | undefined;
45
+ } | undefined;
46
+ serverConfigFile: string;
47
+ }) => IAppContext;
@@ -15,6 +15,7 @@ export type { NormalizedConfig, IAppContext };
15
15
  declare const initAppDir: (cwd?: string | undefined) => Promise<string>;
16
16
  export interface CoreOptions {
17
17
  configFile?: string;
18
+ serverConfigFile?: string;
18
19
  packageJsonConfig?: string;
19
20
  plugins?: typeof INTERNAL_PLUGINS;
20
21
  onSchemaError?: (error: ErrorObject) => void;
@@ -25,6 +26,24 @@ export interface CoreOptions {
25
26
  sharedDir?: string;
26
27
  };
27
28
  }
29
+ export declare const mergeOptions: (options?: CoreOptions | undefined) => {
30
+ configFile?: string | undefined;
31
+ serverConfigFile: string;
32
+ packageJsonConfig?: string | undefined;
33
+ plugins?: {
34
+ [name: string]: {
35
+ cli?: string | undefined;
36
+ server?: string | undefined;
37
+ };
38
+ } | undefined;
39
+ onSchemaError?: ((error: ErrorObject) => void) | undefined;
40
+ options?: {
41
+ metaName?: string | undefined;
42
+ srcDir?: string | undefined;
43
+ distDir?: string | undefined;
44
+ sharedDir?: string | undefined;
45
+ } | undefined;
46
+ };
28
47
  export declare const cli: {
29
48
  init: (argv?: string[], options?: CoreOptions | undefined) => Promise<{
30
49
  loadedConfig: import("./config").LoadedConfig;
@@ -1,4 +1,3 @@
1
- import chokidar from 'chokidar';
2
1
  import { LoadedConfig } from './config';
3
2
  import { HooksRunner } from './manager';
4
- export declare const initWatcher: (loaded: LoadedConfig, appDirectory: string, configDir: string | undefined, hooksRunner: HooksRunner, argv: string[]) => Promise<chokidar.FSWatcher | undefined>;
3
+ export declare const initWatcher: (loaded: LoadedConfig, appDirectory: string, configDir: string | undefined, hooksRunner: HooksRunner, argv: string[]) => Promise<import("@modern-js/utils").FSWatcher | undefined>;
@@ -1,6 +1,6 @@
1
1
  import { ToThreads, ToRunners, AsyncSetup, PluginOptions, AsyncWorkflow, AsyncWaterfall, ParallelWorkflow } from '@modern-js/plugin';
2
+ import { Command } from '@modern-js/utils';
2
3
  import type { Hooks } from '@modern-js/types';
3
- import type { Command } from './utils/commander';
4
4
  import type { NormalizedConfig } from './config/mergeConfig';
5
5
  import { pluginAPI } from './pluginAPI';
6
6
  export declare type HooksRunner = ToRunners<{
@@ -1,7 +1,8 @@
1
- import { program, Command } from 'commander';
2
- declare module 'commander' {
1
+ import { program } from '@modern-js/utils';
2
+ declare module '@modern-js/utils/compiled/commander' {
3
3
  interface Command {
4
4
  commandsMap: Map<string, Command>;
5
5
  }
6
6
  }
7
- export { program, Command };
7
+ export declare function initCommandsMap(): void;
8
+ export { program };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.6.1",
14
+ "version": "1.7.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -42,42 +42,37 @@
42
42
  "dependencies": {
43
43
  "@babel/code-frame": "^7.14.5",
44
44
  "@babel/runtime": "^7",
45
- "@modern-js/load-config": "^1.2.2",
45
+ "@modern-js/load-config": "^1.3.0",
46
46
  "@modern-js/plugin": "^1.3.2",
47
- "@modern-js/utils": "^1.3.7",
47
+ "@modern-js/utils": "^1.4.0",
48
48
  "address": "^1.1.2",
49
49
  "ajv": "^8.6.2",
50
50
  "ajv-keywords": "^5.0.0",
51
51
  "better-ajv-errors": "^0.7.0",
52
- "chokidar": "^3.5.2",
53
- "commander": "^8.1.0",
54
52
  "dotenv": "^10.0.0",
55
53
  "dotenv-expand": "^5.1.0",
56
- "lodash.clonedeep": "^4.5.0",
57
- "lodash.mergewith": "^4.6.2",
58
- "signale": "^1.4.0",
59
54
  "v8-compile-cache": "^2.3.0"
60
55
  },
61
56
  "devDependencies": {
62
57
  "@jest/types": "^27.0.6",
63
- "@modern-js/types": "^1.3.6",
58
+ "@modern-js/types": "^1.4.0",
64
59
  "@scripts/build": "0.0.0",
65
60
  "@scripts/jest-config": "0.0.0",
66
61
  "@types/babel__code-frame": "^7.0.3",
67
62
  "@types/babel__core": "^7.1.16",
68
63
  "@types/jest": "^26",
69
64
  "@types/less": "^3.0.3",
70
- "@types/lodash.clonedeep": "^4.5.6",
71
- "@types/lodash.mergewith": "^4.6.6",
72
65
  "@types/node": "^14",
73
66
  "@types/react": "^17",
74
67
  "@types/react-dom": "^17",
75
- "@types/signale": "^1.4.2",
68
+ "autoprefixer": "^10.3.1",
76
69
  "btsm": "2.2.2",
77
70
  "jest": "^27",
78
71
  "sass": "^1.45.0",
72
+ "terser-webpack-plugin": "^5.1.4",
79
73
  "typescript": "^4",
80
- "webpack": "^5.71.0"
74
+ "webpack": "^5.71.0",
75
+ "webpack-chain": "^6.5.1"
81
76
  },
82
77
  "sideEffects": false,
83
78
  "modernConfig": {
@@ -1,7 +1,7 @@
1
1
  import path from 'path';
2
2
  // import os from 'os';
3
- import { isDev, getPort } from '@modern-js/utils';
4
- import { resolveConfig } from '../src/config';
3
+ import { isDev, getPort, DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
4
+ import { resolveConfig, addServerConfigToDeps } from '../src/config';
5
5
  import {
6
6
  cli,
7
7
  loadUserConfig,
@@ -133,5 +133,14 @@ describe('config', () => {
133
133
  });
134
134
  });
135
135
 
136
- // type TEST = Parameters<typeof resolveConfig>;
137
- // type TypeC = TEST[1];
136
+ describe('addServerConfigToDeps', () => {
137
+ it('should add server config to deps', async () => {
138
+ const appDirectory = path.join(__dirname, './fixtures/index-test');
139
+ const deps: string[] = [];
140
+ await addServerConfigToDeps(deps, appDirectory, DEFAULT_SERVER_CONFIG);
141
+ expect(deps.length).toBe(1);
142
+ expect(deps[0]).toBe(
143
+ path.join(appDirectory, `${DEFAULT_SERVER_CONFIG}.js`),
144
+ );
145
+ });
146
+ });
@@ -1,4 +1,5 @@
1
1
  import path from 'path';
2
+ import { DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
2
3
  import { initAppContext } from '../src/context';
3
4
 
4
5
  describe('context', () => {
@@ -7,10 +8,17 @@ describe('context', () => {
7
8
  __dirname,
8
9
  './fixtures/load-plugin/user-plugins',
9
10
  );
10
- const appContext = initAppContext(appDirectory, [], false);
11
+ const appContext = initAppContext({
12
+ appDirectory,
13
+ plugins: [],
14
+ configFile: false,
15
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
16
+ });
17
+
11
18
  expect(appContext).toEqual({
12
19
  appDirectory,
13
20
  configFile: false,
21
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
14
22
  ip: expect.any(String),
15
23
  port: 0,
16
24
  packageName: expect.any(String),
@@ -44,10 +52,17 @@ describe('context', () => {
44
52
  metaName: 'jupiter',
45
53
  };
46
54
 
47
- const appContext = initAppContext(appDirectory, [], false, customOptions);
55
+ const appContext = initAppContext({
56
+ appDirectory,
57
+ plugins: [],
58
+ configFile: false,
59
+ options: customOptions,
60
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
61
+ });
48
62
  expect(appContext).toEqual({
49
63
  appDirectory,
50
64
  configFile: false,
65
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
51
66
  ip: expect.any(String),
52
67
  port: 0,
53
68
  packageName: 'user-plugins',
@@ -1,5 +1,5 @@
1
1
  import path from 'path';
2
- import { cli } from '../src';
2
+ import { cli, mergeOptions } from '../src';
3
3
  import { resolveConfig, loadUserConfig } from '../src/config';
4
4
  import { loadEnv } from '../src/loadEnv';
5
5
 
@@ -67,3 +67,17 @@ describe('@modern-js/core test', () => {
67
67
  // TODO: add more test cases
68
68
  });
69
69
  });
70
+
71
+ describe('test mergeOptions', () => {
72
+ it('serverConfigFile must exist', () => {
73
+ const options = mergeOptions({});
74
+ expect(options).toHaveProperty('serverConfigFile');
75
+ });
76
+
77
+ it('serverConfigFile can be overwritten', () => {
78
+ const options = mergeOptions({
79
+ serverConfigFile: 'test',
80
+ });
81
+ expect(options.serverConfigFile).toBe('test');
82
+ });
83
+ });
@@ -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.skip('initWatcher', () => {
11
+ describe('initWatcher', () => {
12
12
  afterAll(() => {
13
13
  const file = path.join(mockSrcDirectory, './index.ts');
14
14
  if (fs.existsSync(file)) {
@@ -16,7 +16,7 @@ describe.skip('initWatcher', () => {
16
16
  }
17
17
  });
18
18
 
19
- test('will trigger add event', async () => {
19
+ xtest('will trigger add event', async () => {
20
20
  let triggeredType = '';
21
21
  let triggeredFile = '';
22
22
  const loaded = {
@@ -1,8 +1,7 @@
1
- import { program, Command } from '../src/utils/commander';
1
+ import { program } from '../src/utils/commander';
2
2
 
3
3
  describe('utils', () => {
4
4
  it('default', () => {
5
5
  expect(program).toBeDefined();
6
- expect(Command).toBeDefined();
7
6
  });
8
7
  });