@modern-js/core 2.25.2 → 2.27.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/bin.d.ts +1 -1
  3. package/dist/bin.js +5 -3
  4. package/dist/config/createDefaultConfig.d.ts +1 -1
  5. package/dist/config/createDefaultConfig.js +12 -6
  6. package/dist/config/createLoadedConfig.d.ts +1 -1
  7. package/dist/config/createLoadedConfig.js +80 -73
  8. package/dist/config/createResolvedConfig.d.ts +1 -1
  9. package/dist/config/createResolvedConfig.js +59 -72
  10. package/dist/config/index.d.ts +1 -1
  11. package/dist/config/index.js +7 -18
  12. package/dist/config/loadConfig.d.ts +5 -5
  13. package/dist/config/loadConfig.js +115 -112
  14. package/dist/context.d.ts +21 -14
  15. package/dist/context.js +74 -60
  16. package/dist/createCli.d.ts +13 -13
  17. package/dist/createCli.js +134 -113
  18. package/dist/index.d.ts +16 -16
  19. package/dist/index.js +59 -35
  20. package/dist/loadEnv.d.ts +1 -1
  21. package/dist/loadEnv.js +23 -17
  22. package/dist/loadPlugins.d.ts +4 -4
  23. package/dist/loadPlugins.js +54 -54
  24. package/dist/manager.d.ts +62 -62
  25. package/dist/manager.js +40 -23
  26. package/dist/nodeApi.d.ts +1 -1
  27. package/dist/nodeApi.js +25 -9
  28. package/dist/runBin.d.ts +2 -2
  29. package/dist/runBin.js +56 -53
  30. package/dist/schema/patchSchema.d.ts +15 -15
  31. package/dist/schema/patchSchema.js +56 -53
  32. package/dist/schema/testing.d.ts +11 -11
  33. package/dist/schema/testing.js +26 -8
  34. package/dist/schema/traverseSchema.d.ts +1 -1
  35. package/dist/schema/traverseSchema.js +25 -17
  36. package/dist/types/config/index.d.ts +37 -37
  37. package/dist/types/config/index.js +3 -1
  38. package/dist/types/config/testing.d.ts +11 -11
  39. package/dist/types/config/testing.js +3 -1
  40. package/dist/types/context.d.ts +27 -27
  41. package/dist/types/context.js +3 -1
  42. package/dist/types/coreOptions.d.ts +22 -22
  43. package/dist/types/coreOptions.js +3 -1
  44. package/dist/types/hooks.d.ts +41 -41
  45. package/dist/types/hooks.js +3 -1
  46. package/dist/types/index.d.ts +3 -3
  47. package/dist/types/index.js +10 -21
  48. package/dist/types/plugin.d.ts +10 -10
  49. package/dist/types/plugin.js +3 -1
  50. package/dist/types/pluginAPI.d.ts +10 -10
  51. package/dist/types/pluginAPI.js +3 -1
  52. package/dist/utils/checkIsDuplicationPlugin.d.ts +1 -1
  53. package/dist/utils/checkIsDuplicationPlugin.js +24 -21
  54. package/dist/utils/commander.d.ts +1 -1
  55. package/dist/utils/commander.js +31 -17
  56. package/dist/utils/createFileWatcher.d.ts +1 -1
  57. package/dist/utils/createFileWatcher.js +85 -106
  58. package/dist/utils/index.d.ts +1 -1
  59. package/dist/utils/index.js +10 -21
  60. package/dist/utils/initAppDir.d.ts +1 -1
  61. package/dist/utils/initAppDir.js +22 -17
  62. package/dist/utils/mergeConfig.d.ts +1 -1
  63. package/dist/utils/mergeConfig.js +37 -27
  64. package/dist/utils/repeatKeyWarning.d.ts +1 -1
  65. package/dist/utils/repeatKeyWarning.js +29 -16
  66. package/package.json +11 -10
@@ -1,130 +1,133 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.loadConfig = exports.getConfigFilePath = exports.clearFilesOverTime = exports.getDependencies = exports.getPackageConfig = exports.PACKAGE_JSON_CONFIG_NAME = exports.LOCAL_CONFIG_FILE_NAME = exports.CONFIG_FILE_NAME = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const utils_1 = require("@modern-js/utils");
9
- const node_bundle_require_1 = require("@modern-js/node-bundle-require");
10
- const debug = (0, utils_1.createDebugger)('load-config');
11
- exports.CONFIG_FILE_NAME = 'modern.config';
12
- exports.LOCAL_CONFIG_FILE_NAME = 'modern.config.local';
13
- exports.PACKAGE_JSON_CONFIG_NAME = 'modernConfig';
14
- /**
15
- * Get user config from package.json.
16
- * @param appDirectory - App root directory.
17
- * @returns modernConfig or undefined
18
- */
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for (var name in all)
7
+ Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ CONFIG_FILE_NAME: function() {
14
+ return CONFIG_FILE_NAME;
15
+ },
16
+ LOCAL_CONFIG_FILE_NAME: function() {
17
+ return LOCAL_CONFIG_FILE_NAME;
18
+ },
19
+ PACKAGE_JSON_CONFIG_NAME: function() {
20
+ return PACKAGE_JSON_CONFIG_NAME;
21
+ },
22
+ getPackageConfig: function() {
23
+ return getPackageConfig;
24
+ },
25
+ getDependencies: function() {
26
+ return getDependencies;
27
+ },
28
+ clearFilesOverTime: function() {
29
+ return clearFilesOverTime;
30
+ },
31
+ getConfigFilePath: function() {
32
+ return getConfigFilePath;
33
+ },
34
+ loadConfig: function() {
35
+ return loadConfig;
36
+ }
37
+ });
38
+ const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
39
+ const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
40
+ const _utils = require("@modern-js/utils");
41
+ const _nodebundlerequire = require("@modern-js/node-bundle-require");
42
+ const debug = (0, _utils.createDebugger)("load-config");
43
+ const CONFIG_FILE_NAME = "modern.config";
44
+ const LOCAL_CONFIG_FILE_NAME = "modern.config.local";
45
+ const PACKAGE_JSON_CONFIG_NAME = "modernConfig";
19
46
  const getPackageConfig = (appDirectory, packageJsonConfig) => {
20
- const json = JSON.parse(utils_1.fs.readFileSync(path_1.default.resolve(appDirectory, './package.json'), 'utf8'));
21
- return json[packageJsonConfig !== null && packageJsonConfig !== void 0 ? packageJsonConfig : exports.PACKAGE_JSON_CONFIG_NAME];
47
+ const json = JSON.parse(_utils.fs.readFileSync(_path.default.resolve(appDirectory, "./package.json"), "utf8"));
48
+ return json[packageJsonConfig !== null && packageJsonConfig !== void 0 ? packageJsonConfig : PACKAGE_JSON_CONFIG_NAME];
22
49
  };
23
- exports.getPackageConfig = getPackageConfig;
24
- /**
25
- * Get the file dependencies by module.children, ignore file path in node_modules and this monorepo packages default.
26
- * @param filePath - Absolute file path.
27
- * @returns File dependencies array.
28
- */
29
50
  const getDependencies = (filePath) => {
30
- const mod = require.cache[filePath];
31
- if (!mod) {
32
- debug(`${filePath} has not been required yet`);
33
- return [];
34
- }
35
- const deps = [];
36
- if (!/\/node_modules\/|\/modern-js\/packages\//.test(mod.id)) {
37
- deps.push(mod.id);
38
- for (const child of mod.children) {
39
- deps.push(...(0, exports.getDependencies)(child.id));
40
- }
51
+ const mod = require.cache[filePath];
52
+ if (!mod) {
53
+ debug(`${filePath} has not been required yet`);
54
+ return [];
55
+ }
56
+ const deps = [];
57
+ if (!/\/node_modules\/|\/modern-js\/packages\//.test(mod.id)) {
58
+ deps.push(mod.id);
59
+ for (const child of mod.children) {
60
+ deps.push(...getDependencies(child.id));
41
61
  }
42
- return deps;
62
+ }
63
+ return deps;
43
64
  };
44
- exports.getDependencies = getDependencies;
45
- /**
46
- *
47
- * @param targetDir target dir
48
- * @param overtime Unit of second
49
- */
50
65
  const clearFilesOverTime = async (targetDir, overtime) => {
51
- try {
52
- // when stats is true, globby return Stats[]
53
- const files = (await (0, utils_1.globby)(`${targetDir}/**/*`, {
54
- stats: true,
55
- absolute: true,
56
- }));
57
- const currentTime = Date.now();
58
- if (files.length > 0) {
59
- for (const file of files) {
60
- if (currentTime - file.stats.birthtimeMs >= overtime * 1000) {
61
- utils_1.fs.unlinkSync(file.path);
62
- }
63
- }
66
+ try {
67
+ const files = await (0, _utils.globby)(`${targetDir}/**/*`, {
68
+ stats: true,
69
+ absolute: true
70
+ });
71
+ const currentTime = Date.now();
72
+ if (files.length > 0) {
73
+ for (const file of files) {
74
+ if (currentTime - file.stats.birthtimeMs >= overtime * 1e3) {
75
+ _utils.fs.unlinkSync(file.path);
64
76
  }
77
+ }
65
78
  }
66
- catch (err) {
67
- // ignore error when clear files
68
- }
79
+ } catch (err) {
80
+ }
69
81
  };
70
- exports.clearFilesOverTime = clearFilesOverTime;
71
82
  const bundleRequireWithCatch = async (configFile, { appDirectory }) => {
72
- try {
73
- const mod = await (0, node_bundle_require_1.bundleRequire)(configFile, {
74
- autoClear: false,
75
- getOutputFile: async (filePath) => {
76
- const defaultOutputFileName = path_1.default.basename(await (0, node_bundle_require_1.defaultGetOutputFile)(filePath));
77
- const outputPath = path_1.default.join(appDirectory, utils_1.CONFIG_CACHE_DIR);
78
- // 10 min
79
- const timeLimit = 10 * 60;
80
- await (0, exports.clearFilesOverTime)(outputPath, timeLimit);
81
- return path_1.default.join(outputPath, defaultOutputFileName);
82
- },
83
- });
84
- return mod;
85
- }
86
- catch (e) {
87
- if (e instanceof Error) {
88
- e.message = `Get Error while loading config file: ${configFile}, please check it and retry.\n${e.message || ''}`;
89
- }
90
- throw e;
83
+ try {
84
+ const mod = await (0, _nodebundlerequire.bundleRequire)(configFile, {
85
+ autoClear: false,
86
+ getOutputFile: async (filePath) => {
87
+ const defaultOutputFileName = _path.default.basename(await (0, _nodebundlerequire.defaultGetOutputFile)(filePath));
88
+ const outputPath = _path.default.join(appDirectory, _utils.CONFIG_CACHE_DIR);
89
+ const timeLimit = 10 * 60;
90
+ await clearFilesOverTime(outputPath, timeLimit);
91
+ return _path.default.join(outputPath, defaultOutputFileName);
92
+ }
93
+ });
94
+ return mod;
95
+ } catch (e) {
96
+ if (e instanceof Error) {
97
+ e.message = `Get Error while loading config file: ${configFile}, please check it and retry.
98
+ ${e.message || ""}`;
91
99
  }
100
+ throw e;
101
+ }
92
102
  };
93
103
  const getConfigFilePath = (appDirectory, filePath) => {
94
- if (filePath) {
95
- if (path_1.default.isAbsolute(filePath)) {
96
- return filePath;
97
- }
98
- return path_1.default.resolve(appDirectory, filePath);
104
+ if (filePath) {
105
+ if (_path.default.isAbsolute(filePath)) {
106
+ return filePath;
99
107
  }
100
- return (0, utils_1.findExists)(utils_1.CONFIG_FILE_EXTENSIONS.map(extension => path_1.default.resolve(appDirectory, `${exports.CONFIG_FILE_NAME}${extension}`)));
108
+ return _path.default.resolve(appDirectory, filePath);
109
+ }
110
+ return (0, _utils.findExists)(_utils.CONFIG_FILE_EXTENSIONS.map((extension) => _path.default.resolve(appDirectory, `${CONFIG_FILE_NAME}${extension}`)));
101
111
  };
102
- exports.getConfigFilePath = getConfigFilePath;
103
- /**
104
- * Parse and load user config file, support extensions like .ts, mjs, js, ejs.
105
- * @param appDirectory - App root directory, from which start search user config file.
106
- * @param configFile - Specific absolute config file path.
107
- * @returns Object contain config file path, user config object and dependency files used by config file.
108
- */
109
112
  const loadConfig = async (appDirectory, configFile, packageJsonConfig, loadedConfig) => {
110
- const pkgConfig = (0, exports.getPackageConfig)(appDirectory, packageJsonConfig);
111
- let config;
112
- const dependencies = pkgConfig
113
- ? [path_1.default.resolve(appDirectory, './package.json')]
114
- : [];
115
- if (loadedConfig) {
116
- config = loadedConfig;
117
- }
118
- else if (configFile) {
119
- delete require.cache[configFile];
120
- const mod = await bundleRequireWithCatch(configFile, { appDirectory });
121
- config = mod.default || mod;
122
- }
123
- return {
124
- path: configFile,
125
- config,
126
- pkgConfig,
127
- dependencies,
128
- };
113
+ const pkgConfig = getPackageConfig(appDirectory, packageJsonConfig);
114
+ let config;
115
+ const dependencies = pkgConfig ? [
116
+ _path.default.resolve(appDirectory, "./package.json")
117
+ ] : [];
118
+ if (loadedConfig) {
119
+ config = loadedConfig;
120
+ } else if (configFile) {
121
+ delete require.cache[configFile];
122
+ const mod = await bundleRequireWithCatch(configFile, {
123
+ appDirectory
124
+ });
125
+ config = mod.default || mod;
126
+ }
127
+ return {
128
+ path: configFile,
129
+ config,
130
+ pkgConfig,
131
+ dependencies
132
+ };
129
133
  };
130
- exports.loadConfig = loadConfig;
package/dist/context.d.ts CHANGED
@@ -19,17 +19,24 @@ export declare const useConfigContext: <Extends extends Record<string, any>>() =
19
19
  * Get normalized content of user config.
20
20
  */
21
21
  export declare const useResolvedConfigContext: <Extends extends Record<string, any>>() => NormalizedConfig<Extends>;
22
- export declare const initAppContext: ({ appDirectory, plugins, configFile, options, serverConfigFile, serverInternalPlugins, }: {
23
- appDirectory: string;
24
- plugins: CliPlugin[];
25
- configFile: string | false;
26
- options?: {
27
- metaName?: string | undefined;
28
- srcDir?: string | undefined;
29
- apiDir?: string | undefined;
30
- distDir?: string | undefined;
31
- sharedDir?: string | undefined;
32
- } | undefined;
33
- serverConfigFile: string;
34
- serverInternalPlugins: InternalPlugins;
35
- }) => IAppContext;
22
+ export declare const initAppContext: ({
23
+ appDirectory,
24
+ plugins,
25
+ configFile,
26
+ options,
27
+ serverConfigFile,
28
+ serverInternalPlugins
29
+ }: {
30
+ appDirectory: string;
31
+ plugins: CliPlugin[];
32
+ configFile: string | false;
33
+ options?: {
34
+ metaName?: string | undefined;
35
+ srcDir?: string | undefined;
36
+ apiDir?: string | undefined;
37
+ distDir?: string | undefined;
38
+ sharedDir?: string | undefined;
39
+ } | undefined;
40
+ serverConfigFile: string;
41
+ serverInternalPlugins: InternalPlugins;
42
+ }) => IAppContext;
package/dist/context.js CHANGED
@@ -1,62 +1,76 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for (var name in all)
7
+ Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ AppContext: function() {
14
+ return AppContext;
15
+ },
16
+ ConfigContext: function() {
17
+ return ConfigContext;
18
+ },
19
+ ResolvedConfigContext: function() {
20
+ return ResolvedConfigContext;
21
+ },
22
+ setAppContext: function() {
23
+ return setAppContext;
24
+ },
25
+ useAppContext: function() {
26
+ return useAppContext;
27
+ },
28
+ useConfigContext: function() {
29
+ return useConfigContext;
30
+ },
31
+ useResolvedConfigContext: function() {
32
+ return useResolvedConfigContext;
33
+ },
34
+ initAppContext: function() {
35
+ return initAppContext;
36
+ }
37
+ });
38
+ const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
39
+ const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
40
+ const _utils = require("@modern-js/utils");
41
+ const _plugin = require("@modern-js/plugin");
42
+ const AppContext = (0, _plugin.createContext)({});
43
+ const ConfigContext = (0, _plugin.createContext)({});
44
+ const ResolvedConfigContext = (0, _plugin.createContext)({});
45
+ const setAppContext = (value) => AppContext.set(value);
46
+ const useAppContext = () => AppContext.use().value;
47
+ const useConfigContext = () => ConfigContext.use().value;
48
+ const useResolvedConfigContext = () => ResolvedConfigContext.use().value;
49
+ const initAppContext = ({ appDirectory, plugins, configFile, options, serverConfigFile, serverInternalPlugins }) => {
50
+ const { metaName = "modern-js", srcDir = "src", distDir = "", apiDir = "api", sharedDir = "shared" } = options || {};
51
+ return {
52
+ metaName,
53
+ appDirectory,
54
+ configFile,
55
+ serverConfigFile,
56
+ serverInternalPlugins,
57
+ ip: _utils.address.ip(),
58
+ port: 0,
59
+ packageName: require(_path.default.resolve(appDirectory, "./package.json")).name,
60
+ srcDirectory: _path.default.resolve(appDirectory, srcDir),
61
+ apiDirectory: _path.default.resolve(appDirectory, apiDir),
62
+ lambdaDirectory: _path.default.resolve(appDirectory, apiDir, "lambda"),
63
+ distDirectory: distDir,
64
+ sharedDirectory: _path.default.resolve(appDirectory, sharedDir),
65
+ nodeModulesDirectory: _path.default.resolve(appDirectory, "./node_modules"),
66
+ internalDirectory: _path.default.resolve(appDirectory, `./node_modules/.${metaName}`),
67
+ plugins,
68
+ htmlTemplates: {},
69
+ serverRoutes: [],
70
+ entrypoints: [],
71
+ checkedEntries: [],
72
+ apiOnly: false,
73
+ internalDirAlias: `@_${metaName.replace(/-/g, "_")}_internal`,
74
+ internalSrcAlias: `@_${metaName.replace(/-/g, "_")}_src`
75
+ };
4
76
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.initAppContext = exports.useResolvedConfigContext = exports.useConfigContext = exports.useAppContext = exports.setAppContext = exports.ResolvedConfigContext = exports.ConfigContext = exports.AppContext = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const utils_1 = require("@modern-js/utils");
9
- const plugin_1 = require("@modern-js/plugin");
10
- exports.AppContext = (0, plugin_1.createContext)({});
11
- exports.ConfigContext = (0, plugin_1.createContext)({});
12
- exports.ResolvedConfigContext = (0, plugin_1.createContext)({});
13
- /**
14
- * Set app context.
15
- * @param value new app context. It will override previous app context.
16
- */
17
- const setAppContext = (value) => exports.AppContext.set(value);
18
- exports.setAppContext = setAppContext;
19
- /**
20
- * Get app context, including directories, plugins and some static infos.
21
- */
22
- const useAppContext = () => exports.AppContext.use().value;
23
- exports.useAppContext = useAppContext;
24
- /**
25
- * Get original content of user config.
26
- */
27
- const useConfigContext = () => exports.ConfigContext.use().value;
28
- exports.useConfigContext = useConfigContext;
29
- /**
30
- * Get normalized content of user config.
31
- */
32
- const useResolvedConfigContext = () => exports.ResolvedConfigContext.use().value;
33
- exports.useResolvedConfigContext = useResolvedConfigContext;
34
- const initAppContext = ({ appDirectory, plugins, configFile, options, serverConfigFile, serverInternalPlugins, }) => {
35
- const { metaName = 'modern-js', srcDir = 'src', distDir = '', apiDir = 'api', sharedDir = 'shared', } = options || {};
36
- return {
37
- metaName,
38
- appDirectory,
39
- configFile,
40
- serverConfigFile,
41
- serverInternalPlugins,
42
- ip: utils_1.address.ip(),
43
- port: 0,
44
- packageName: require(path_1.default.resolve(appDirectory, './package.json')).name,
45
- srcDirectory: path_1.default.resolve(appDirectory, srcDir),
46
- apiDirectory: path_1.default.resolve(appDirectory, apiDir),
47
- lambdaDirectory: path_1.default.resolve(appDirectory, apiDir, 'lambda'),
48
- distDirectory: distDir,
49
- sharedDirectory: path_1.default.resolve(appDirectory, sharedDir),
50
- nodeModulesDirectory: path_1.default.resolve(appDirectory, './node_modules'),
51
- internalDirectory: path_1.default.resolve(appDirectory, `./node_modules/.${metaName}`),
52
- plugins,
53
- htmlTemplates: {},
54
- serverRoutes: [],
55
- entrypoints: [],
56
- checkedEntries: [],
57
- apiOnly: false,
58
- internalDirAlias: `@_${metaName.replace(/-/g, '_')}_internal`,
59
- internalSrcAlias: `@_${metaName.replace(/-/g, '_')}_src`,
60
- };
61
- };
62
- exports.initAppContext = initAppContext;
@@ -1,17 +1,17 @@
1
1
  import type { CoreOptions } from './types';
2
2
  export declare const mergeOptions: (options?: CoreOptions) => CoreOptions & {
3
- serverConfigFile: string;
3
+ serverConfigFile: string;
4
4
  };
5
5
  export declare const createCli: () => {
6
- init: (options?: CoreOptions) => Promise<{
7
- resolved: import("./types").NormalizedConfig<{}>;
8
- appContext: import("./types").IAppContext;
9
- }>;
10
- run: (options?: CoreOptions) => Promise<void>;
11
- test: (argv: string[], options?: {
12
- coreOptions?: CoreOptions;
13
- disableWatcher?: boolean;
14
- }) => Promise<void>;
15
- runCommand: (command: string, commandOptions?: string[], options?: CoreOptions) => Promise<void>;
16
- getPrevInitOptions: () => CoreOptions | undefined;
17
- };
6
+ init: (options?: CoreOptions) => Promise<{
7
+ resolved: import("./types").NormalizedConfig<{}>;
8
+ appContext: import("./types").IAppContext;
9
+ }>;
10
+ run: (options?: CoreOptions) => Promise<void>;
11
+ test: (argv: string[], options?: {
12
+ coreOptions?: CoreOptions;
13
+ disableWatcher?: boolean;
14
+ }) => Promise<void>;
15
+ runCommand: (command: string, commandOptions?: string[], options?: CoreOptions) => Promise<void>;
16
+ getPrevInitOptions: () => CoreOptions | undefined;
17
+ };