@modern-js/core 0.0.0-windows-20211056953 → 0.0.1-20221226
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 +1164 -6
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +5 -0
- package/dist/config/createDefaultConfig.d.ts +2 -0
- package/dist/config/createDefaultConfig.js +9 -0
- package/dist/config/createLoadedConfig.d.ts +6 -0
- package/dist/config/createLoadedConfig.js +35 -0
- package/dist/config/createResolvedConfig.d.ts +3 -0
- package/dist/config/createResolvedConfig.js +92 -0
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.js +19 -0
- package/dist/context.d.ts +35 -0
- package/dist/context.js +61 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +150 -0
- package/dist/load-configs/index.d.ts +34 -0
- package/dist/load-configs/index.js +124 -0
- package/dist/{types/loadEnv.d.ts → loadEnv.d.ts} +1 -1
- package/dist/loadEnv.js +21 -0
- package/dist/loadPlugins.d.ts +18 -0
- package/dist/loadPlugins.js +65 -0
- package/dist/manager.d.ts +87 -0
- package/dist/manager.js +24 -0
- package/dist/runBin.d.ts +5 -0
- package/dist/runBin.js +51 -0
- package/dist/schema/patchSchema.d.ts +19 -0
- package/dist/schema/patchSchema.js +57 -0
- package/dist/schema/source.d.ts +9 -0
- package/dist/schema/source.js +10 -0
- package/dist/schema/testing.d.ts +13 -0
- package/dist/schema/testing.js +11 -0
- package/dist/schema/traverseSchema.d.ts +2 -0
- package/dist/schema/traverseSchema.js +20 -0
- package/dist/types/config/index.d.ts +49 -125
- package/dist/types/config/index.js +2 -0
- package/dist/types/config/testing.d.ts +15 -0
- package/dist/types/config/testing.js +2 -0
- package/dist/types/context.d.ts +28 -14
- package/dist/types/context.js +2 -0
- package/dist/types/hooks.d.ts +44 -0
- package/dist/types/hooks.js +2 -0
- package/dist/types/index.d.ts +9 -87
- package/dist/types/index.js +21 -0
- package/dist/types/plugin.d.ts +26 -0
- package/dist/types/plugin.js +2 -0
- package/dist/types/pluginAPI.d.ts +18 -0
- package/dist/types/pluginAPI.js +2 -0
- package/dist/utils/commander.d.ts +4 -0
- package/dist/utils/commander.js +20 -0
- package/dist/utils/mergeConfig.d.ts +2 -0
- package/dist/utils/mergeConfig.js +32 -0
- package/dist/utils/repeatKeyWarning.d.ts +3 -0
- package/dist/utils/repeatKeyWarning.js +22 -0
- package/package.json +52 -48
- package/bin/modern-js.js +0 -18
- package/dist/js/modern/config/defaults.js +0 -100
- package/dist/js/modern/config/index.js +0 -104
- package/dist/js/modern/config/mergeConfig.js +0 -20
- package/dist/js/modern/config/schema/deploy.js +0 -20
- package/dist/js/modern/config/schema/index.js +0 -107
- package/dist/js/modern/config/schema/output.js +0 -147
- package/dist/js/modern/config/schema/server.js +0 -170
- package/dist/js/modern/config/schema/source.js +0 -59
- package/dist/js/modern/config/schema/tools.js +0 -33
- package/dist/js/modern/context.js +0 -25
- package/dist/js/modern/index.js +0 -142
- package/dist/js/modern/initWatcher.js +0 -46
- package/dist/js/modern/loadEnv.js +0 -12
- package/dist/js/modern/loadPlugins.js +0 -66
- package/dist/js/modern/utils/commander.js +0 -19
- package/dist/js/modern/utils/repeatKeyWarning.js +0 -18
- package/dist/js/node/config/defaults.js +0 -107
- package/dist/js/node/config/index.js +0 -142
- package/dist/js/node/config/mergeConfig.js +0 -32
- package/dist/js/node/config/schema/deploy.js +0 -29
- package/dist/js/node/config/schema/index.js +0 -129
- package/dist/js/node/config/schema/output.js +0 -156
- package/dist/js/node/config/schema/server.js +0 -179
- package/dist/js/node/config/schema/source.js +0 -68
- package/dist/js/node/config/schema/tools.js +0 -40
- package/dist/js/node/context.js +0 -52
- package/dist/js/node/index.js +0 -247
- package/dist/js/node/initWatcher.js +0 -67
- package/dist/js/node/loadEnv.js +0 -28
- package/dist/js/node/loadPlugins.js +0 -76
- package/dist/js/node/utils/commander.js +0 -35
- package/dist/js/node/utils/repeatKeyWarning.js +0 -31
- package/dist/types/config/defaults.d.ts +0 -25
- package/dist/types/config/mergeConfig.d.ts +0 -29
- package/dist/types/config/schema/deploy.d.ts +0 -19
- package/dist/types/config/schema/index.d.ts +0 -466
- package/dist/types/config/schema/output.d.ts +0 -146
- package/dist/types/config/schema/server.d.ts +0 -182
- package/dist/types/config/schema/source.d.ts +0 -58
- package/dist/types/config/schema/tools.d.ts +0 -33
- package/dist/types/initWatcher.d.ts +0 -3
- package/dist/types/loadPlugins.d.ts +0 -16
- package/dist/types/utils/commander.d.ts +0 -7
- package/dist/types/utils/repeatKeyWarning.d.ts +0 -3
- package/modern.config.js +0 -13
- package/src/config/defaults.ts +0 -101
- package/src/config/index.ts +0 -296
- package/src/config/mergeConfig.ts +0 -68
- package/src/config/schema/deploy.ts +0 -17
- package/src/config/schema/index.ts +0 -116
- package/src/config/schema/output.ts +0 -66
- package/src/config/schema/server.ts +0 -106
- package/src/config/schema/source.ts +0 -34
- package/src/config/schema/tools.ts +0 -15
- package/src/context.ts +0 -46
- package/src/index.ts +0 -247
- package/src/initWatcher.ts +0 -77
- package/src/loadEnv.ts +0 -21
- package/src/loadPlugins.ts +0 -81
- package/src/types.d.ts +0 -0
- package/src/utils/commander.ts +0 -22
- package/src/utils/repeatKeyWarning.ts +0 -29
- package/tests/.eslintrc.js +0 -6
- package/tests/fixtures/load-plugin/not-found/package.json +0 -3
- package/tests/fixtures/load-plugin/not-found/test-plugin-a.js +0 -1
- package/tests/fixtures/load-plugin/user-plugins/package.json +0 -3
- package/tests/fixtures/load-plugin/user-plugins/test-plugin-a.js +0 -1
- package/tests/fixtures/load-plugin/user-plugins/test-plugin-b.js +0 -3
- package/tests/loadEnv.test.ts +0 -100
- package/tests/loadPlugin.test.ts +0 -29
- package/tests/mergeConfig.test.ts +0 -78
- package/tests/repeatKeyWarning.test.ts +0 -68
- package/tests/schema.test.ts +0 -109
- package/tests/tsconfig.json +0 -13
- package/tsconfig.json +0 -14
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -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 createDefaultConfig_1 = require("./createDefaultConfig");
|
|
33
|
+
const debug = (0, utils_1.createDebugger)('resolve-config');
|
|
34
|
+
const showAdditionalPropertiesError = async (error) => {
|
|
35
|
+
if (error.keyword === 'additionalProperties' &&
|
|
36
|
+
error.params.additionalProperty) {
|
|
37
|
+
const target = [
|
|
38
|
+
error.instancePath.slice(1),
|
|
39
|
+
error.params.additionalProperty,
|
|
40
|
+
]
|
|
41
|
+
.filter(Boolean)
|
|
42
|
+
.join('.');
|
|
43
|
+
const name = Object.keys(constants_1.PLUGIN_SCHEMAS).find(key => constants_1.PLUGIN_SCHEMAS[key].some((schemaItem) => schemaItem.target === target));
|
|
44
|
+
if (name) {
|
|
45
|
+
const packageManager = await (0, utils_1.getPackageManager)();
|
|
46
|
+
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`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const createResolveConfig = async (
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
52
|
+
loaded, configs, schemas, onSchemaError = showAdditionalPropertiesError) => {
|
|
53
|
+
var _a;
|
|
54
|
+
const { default: Ajv } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/ajv')));
|
|
55
|
+
const { default: ajvKeywords } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/ajv-keywords')));
|
|
56
|
+
const { default: betterAjvErrors } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/better-ajv-errors')));
|
|
57
|
+
const { config: userConfig, jsConfig, pkgConfig } = loaded;
|
|
58
|
+
const ajv = new Ajv({ $data: true, strict: false });
|
|
59
|
+
ajvKeywords(ajv);
|
|
60
|
+
const validateSchema = (0, patchSchema_1.patchSchema)(schemas);
|
|
61
|
+
const validate = ajv.compile(validateSchema);
|
|
62
|
+
(0, repeatKeyWarning_1.repeatKeyWarning)(validateSchema, jsConfig, pkgConfig);
|
|
63
|
+
// validate user config.
|
|
64
|
+
const valid = validate(userConfig);
|
|
65
|
+
const formatValidateError = (config) => {
|
|
66
|
+
var _a;
|
|
67
|
+
return betterAjvErrors(validateSchema, config, (_a = validate.errors) === null || _a === void 0 ? void 0 : _a.map(e => ({
|
|
68
|
+
...e,
|
|
69
|
+
dataPath: e.instancePath,
|
|
70
|
+
})), {
|
|
71
|
+
indent: 2,
|
|
72
|
+
});
|
|
73
|
+
};
|
|
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)([(0, createDefaultConfig_1.createDefaultConfig)(), ...configs, userConfig]);
|
|
89
|
+
debug('resolved %o', resolved);
|
|
90
|
+
return resolved;
|
|
91
|
+
};
|
|
92
|
+
exports.createResolveConfig = createResolveConfig;
|
|
@@ -0,0 +1,19 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./createLoadedConfig"), exports);
|
|
18
|
+
__exportStar(require("./createResolvedConfig"), exports);
|
|
19
|
+
__exportStar(require("./createDefaultConfig"), exports);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { CliPlugin, UserConfig, IAppContext, InternalPlugins, NormalizedConfig, ToolsType } from './types';
|
|
2
|
+
export declare const AppContext: import("@modern-js/plugin").Context<IAppContext>;
|
|
3
|
+
export declare const ConfigContext: import("@modern-js/plugin").Context<UserConfig<any, {}, {}, {}>>;
|
|
4
|
+
export declare const ResolvedConfigContext: import("@modern-js/plugin").Context<NormalizedConfig<any, {}, {}, {}>>;
|
|
5
|
+
/**
|
|
6
|
+
* Set app context.
|
|
7
|
+
* @param value new app context. It will override previous app context.
|
|
8
|
+
*/
|
|
9
|
+
export declare const setAppContext: (value: IAppContext) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Get app context, including directories, plugins and some static infos.
|
|
12
|
+
*/
|
|
13
|
+
export declare const useAppContext: () => IAppContext;
|
|
14
|
+
/**
|
|
15
|
+
* Get original content of user config.
|
|
16
|
+
*/
|
|
17
|
+
export declare const useConfigContext: <Extends extends Record<string, any>>() => UserConfig<Extends>;
|
|
18
|
+
/**
|
|
19
|
+
* Get normalized content of user config.
|
|
20
|
+
*/
|
|
21
|
+
export declare const useResolvedConfigContext: <Extends extends Record<string, any>>() => NormalizedConfig<Extends>;
|
|
22
|
+
export declare const initAppContext: ({ appDirectory, plugins, configFile, options, serverConfigFile, serverInternalPlugins, toolsType, }: {
|
|
23
|
+
appDirectory: string;
|
|
24
|
+
plugins: CliPlugin[];
|
|
25
|
+
configFile: string | false;
|
|
26
|
+
options?: {
|
|
27
|
+
metaName?: string | undefined;
|
|
28
|
+
srcDir?: string | undefined;
|
|
29
|
+
distDir?: string | undefined;
|
|
30
|
+
sharedDir?: string | undefined;
|
|
31
|
+
} | undefined;
|
|
32
|
+
serverConfigFile: string;
|
|
33
|
+
serverInternalPlugins: InternalPlugins;
|
|
34
|
+
toolsType?: ToolsType | undefined;
|
|
35
|
+
}) => IAppContext;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
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.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, toolsType, }) => {
|
|
35
|
+
const { metaName = 'modern-js', srcDir = 'src', distDir = '', 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
|
+
distDirectory: distDir,
|
|
47
|
+
sharedDirectory: path_1.default.resolve(appDirectory, sharedDir),
|
|
48
|
+
nodeModulesDirectory: path_1.default.resolve(appDirectory, './node_modules'),
|
|
49
|
+
internalDirectory: path_1.default.resolve(appDirectory, `./node_modules/.${metaName}`),
|
|
50
|
+
plugins,
|
|
51
|
+
htmlTemplates: {},
|
|
52
|
+
serverRoutes: [],
|
|
53
|
+
entrypoints: [],
|
|
54
|
+
checkedEntries: [],
|
|
55
|
+
apiOnly: false,
|
|
56
|
+
internalDirAlias: `@_${metaName.replace(/-/g, '_')}_internal`,
|
|
57
|
+
internalSrcAlias: `@_${metaName.replace(/-/g, '_')}_src`,
|
|
58
|
+
toolsType,
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
exports.initAppContext = initAppContext;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { ErrorObject } from '@modern-js/utils/ajv';
|
|
2
|
+
import { InternalPlugins } from '@modern-js/types';
|
|
3
|
+
import { TransformPlugin } from './loadPlugins';
|
|
4
|
+
import { initAppContext } from './context';
|
|
5
|
+
import type { ToolsType } from './types';
|
|
6
|
+
export * from './types';
|
|
7
|
+
export * from '@modern-js/plugin';
|
|
8
|
+
export { manager, mountHook, createPlugin, registerHook } from './manager';
|
|
9
|
+
export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext, } from './context';
|
|
10
|
+
declare const initAppDir: (cwd?: string) => Promise<string>;
|
|
11
|
+
export interface CoreOptions {
|
|
12
|
+
cwd?: string;
|
|
13
|
+
version?: string;
|
|
14
|
+
configFile?: string;
|
|
15
|
+
serverConfigFile?: string;
|
|
16
|
+
packageJsonConfig?: string;
|
|
17
|
+
internalPlugins?: {
|
|
18
|
+
cli?: InternalPlugins;
|
|
19
|
+
server?: InternalPlugins;
|
|
20
|
+
autoLoad?: InternalPlugins;
|
|
21
|
+
};
|
|
22
|
+
transformPlugin?: TransformPlugin;
|
|
23
|
+
onSchemaError?: (error: ErrorObject) => void;
|
|
24
|
+
options?: {
|
|
25
|
+
metaName?: string;
|
|
26
|
+
srcDir?: string;
|
|
27
|
+
distDir?: string;
|
|
28
|
+
sharedDir?: string;
|
|
29
|
+
};
|
|
30
|
+
toolsType?: ToolsType;
|
|
31
|
+
/** force the modern-js core auto register plugin exist in the package.json */
|
|
32
|
+
forceAutoLoadPlugins?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export declare const mergeOptions: (options?: CoreOptions) => {
|
|
35
|
+
cwd?: string | undefined;
|
|
36
|
+
version?: string | undefined;
|
|
37
|
+
configFile?: string | undefined;
|
|
38
|
+
serverConfigFile: string;
|
|
39
|
+
packageJsonConfig?: string | undefined;
|
|
40
|
+
internalPlugins?: {
|
|
41
|
+
cli?: InternalPlugins | undefined;
|
|
42
|
+
server?: InternalPlugins | undefined;
|
|
43
|
+
autoLoad?: InternalPlugins | undefined;
|
|
44
|
+
} | undefined;
|
|
45
|
+
transformPlugin?: TransformPlugin | undefined;
|
|
46
|
+
onSchemaError?: ((error: ErrorObject) => void) | undefined;
|
|
47
|
+
options?: {
|
|
48
|
+
metaName?: string | undefined;
|
|
49
|
+
srcDir?: string | undefined;
|
|
50
|
+
distDir?: string | undefined;
|
|
51
|
+
sharedDir?: string | undefined;
|
|
52
|
+
} | undefined;
|
|
53
|
+
toolsType?: ToolsType | undefined;
|
|
54
|
+
forceAutoLoadPlugins?: boolean | undefined;
|
|
55
|
+
};
|
|
56
|
+
export declare const cli: {
|
|
57
|
+
init: (options?: CoreOptions) => Promise<{
|
|
58
|
+
resolved: import("./types").NormalizedConfig<{}, {}, {}, {}>;
|
|
59
|
+
appContext: import("./types").IAppContext;
|
|
60
|
+
}>;
|
|
61
|
+
run: (options?: CoreOptions) => Promise<void>;
|
|
62
|
+
test: (argv: string[], options?: {
|
|
63
|
+
coreOptions?: CoreOptions;
|
|
64
|
+
disableWatcher?: boolean;
|
|
65
|
+
}) => Promise<void>;
|
|
66
|
+
getPrevInitOptions: () => CoreOptions | undefined;
|
|
67
|
+
};
|
|
68
|
+
export { initAppDir, initAppContext };
|
|
69
|
+
declare module '@modern-js/utils/compiled/commander' {
|
|
70
|
+
interface Command {
|
|
71
|
+
commandsMap: Map<string, Command>;
|
|
72
|
+
}
|
|
73
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.initAppContext = exports.initAppDir = exports.cli = exports.mergeOptions = exports.useResolvedConfigContext = exports.useConfigContext = exports.useAppContext = exports.ResolvedConfigContext = exports.ConfigContext = exports.AppContext = exports.registerHook = exports.createPlugin = exports.mountHook = exports.manager = void 0;
|
|
21
|
+
const path_1 = __importDefault(require("path"));
|
|
22
|
+
const utils_1 = require("@modern-js/utils");
|
|
23
|
+
const commander_1 = require("./utils/commander");
|
|
24
|
+
const loadPlugins_1 = require("./loadPlugins");
|
|
25
|
+
const context_1 = require("./context");
|
|
26
|
+
Object.defineProperty(exports, "initAppContext", { enumerable: true, get: function () { return context_1.initAppContext; } });
|
|
27
|
+
const loadEnv_1 = require("./loadEnv");
|
|
28
|
+
const manager_1 = require("./manager");
|
|
29
|
+
const config_1 = require("./config");
|
|
30
|
+
__exportStar(require("./types"), exports);
|
|
31
|
+
__exportStar(require("@modern-js/plugin"), exports);
|
|
32
|
+
// TODO: remove export after refactor all plugins
|
|
33
|
+
var manager_2 = require("./manager");
|
|
34
|
+
Object.defineProperty(exports, "manager", { enumerable: true, get: function () { return manager_2.manager; } });
|
|
35
|
+
Object.defineProperty(exports, "mountHook", { enumerable: true, get: function () { return manager_2.mountHook; } });
|
|
36
|
+
Object.defineProperty(exports, "createPlugin", { enumerable: true, get: function () { return manager_2.createPlugin; } });
|
|
37
|
+
Object.defineProperty(exports, "registerHook", { enumerable: true, get: function () { return manager_2.registerHook; } });
|
|
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; } });
|
|
45
|
+
const initAppDir = async (cwd) => {
|
|
46
|
+
if (!cwd) {
|
|
47
|
+
// eslint-disable-next-line no-param-reassign
|
|
48
|
+
cwd = process.cwd();
|
|
49
|
+
}
|
|
50
|
+
const pkg = await (0, utils_1.pkgUp)({ cwd });
|
|
51
|
+
if (!pkg) {
|
|
52
|
+
throw new Error(`no package.json found in current work dir: ${cwd}`);
|
|
53
|
+
}
|
|
54
|
+
return path_1.default.dirname(pkg);
|
|
55
|
+
};
|
|
56
|
+
exports.initAppDir = initAppDir;
|
|
57
|
+
const mergeOptions = (options) => {
|
|
58
|
+
const defaultOptions = {
|
|
59
|
+
serverConfigFile: utils_1.DEFAULT_SERVER_CONFIG,
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
...defaultOptions,
|
|
63
|
+
...options,
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
exports.mergeOptions = mergeOptions;
|
|
67
|
+
const setProgramVersion = (version = 'unknown') => {
|
|
68
|
+
utils_1.program.name('modern').usage('<command> [options]').version(version);
|
|
69
|
+
};
|
|
70
|
+
const createCli = () => {
|
|
71
|
+
let hooksRunner;
|
|
72
|
+
let initOptions;
|
|
73
|
+
const init = async (options) => {
|
|
74
|
+
var _a, _b, _c, _d, _e;
|
|
75
|
+
manager_1.manager.clear();
|
|
76
|
+
const mergedOptions = (0, exports.mergeOptions)(options);
|
|
77
|
+
initOptions = mergedOptions;
|
|
78
|
+
const appDirectory = await initAppDir(options === null || options === void 0 ? void 0 : options.cwd);
|
|
79
|
+
(0, commander_1.initCommandsMap)();
|
|
80
|
+
setProgramVersion(options === null || options === void 0 ? void 0 : options.version);
|
|
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';
|
|
82
|
+
(0, loadEnv_1.loadEnv)(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
|
|
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, {
|
|
85
|
+
internalPlugins: (_c = mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.internalPlugins) === null || _c === void 0 ? void 0 : _c.cli,
|
|
86
|
+
transformPlugin: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.transformPlugin,
|
|
87
|
+
autoLoad: (_d = mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.internalPlugins) === null || _d === void 0 ? void 0 : _d.autoLoad,
|
|
88
|
+
forceAutoLoadPlugins: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.forceAutoLoadPlugins,
|
|
89
|
+
});
|
|
90
|
+
plugins.forEach(plugin => plugin && manager_1.manager.usePlugin(plugin));
|
|
91
|
+
const appContext = (0, context_1.initAppContext)({
|
|
92
|
+
toolsType: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.toolsType,
|
|
93
|
+
appDirectory,
|
|
94
|
+
plugins,
|
|
95
|
+
configFile: loaded.filePath,
|
|
96
|
+
options: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.options,
|
|
97
|
+
serverConfigFile: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.serverConfigFile,
|
|
98
|
+
serverInternalPlugins: ((_e = mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.internalPlugins) === null || _e === void 0 ? void 0 : _e.server) || utils_1.INTERNAL_SERVER_PLUGINS,
|
|
99
|
+
});
|
|
100
|
+
context_1.ConfigContext.set(loaded.config);
|
|
101
|
+
context_1.AppContext.set(appContext);
|
|
102
|
+
hooksRunner = await manager_1.manager.init();
|
|
103
|
+
['SIGINT', 'SIGTERM', 'unhandledRejection', 'uncaughtException'].forEach(event => {
|
|
104
|
+
process.on(event, async (err) => {
|
|
105
|
+
await hooksRunner.beforeExit();
|
|
106
|
+
if (err instanceof Error) {
|
|
107
|
+
utils_1.logger.error(err.stack);
|
|
108
|
+
}
|
|
109
|
+
process.nextTick(() => {
|
|
110
|
+
// eslint-disable-next-line no-process-exit
|
|
111
|
+
process.exit(1);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
const extraConfigs = await hooksRunner.config();
|
|
116
|
+
const extraSchemas = await hooksRunner.validateSchema();
|
|
117
|
+
const normalizedConfig = await (0, config_1.createResolveConfig)(loaded, extraConfigs, extraSchemas, options === null || options === void 0 ? void 0 : options.onSchemaError);
|
|
118
|
+
const { resolved } = await hooksRunner.resolvedConfig({
|
|
119
|
+
resolved: normalizedConfig,
|
|
120
|
+
});
|
|
121
|
+
// update context value
|
|
122
|
+
context_1.ConfigContext.set(loaded.config);
|
|
123
|
+
context_1.ResolvedConfigContext.set(resolved);
|
|
124
|
+
await hooksRunner.addRuntimeExports();
|
|
125
|
+
await hooksRunner.prepare();
|
|
126
|
+
return {
|
|
127
|
+
resolved,
|
|
128
|
+
appContext: (0, context_1.useAppContext)(),
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
async function run(options) {
|
|
132
|
+
await init(options);
|
|
133
|
+
await hooksRunner.commands({ program: utils_1.program });
|
|
134
|
+
utils_1.program.parse(process.argv);
|
|
135
|
+
}
|
|
136
|
+
async function test(argv, options) {
|
|
137
|
+
const newProgram = new utils_1.Command();
|
|
138
|
+
const { coreOptions } = options !== null && options !== void 0 ? options : {};
|
|
139
|
+
await init(coreOptions);
|
|
140
|
+
await hooksRunner.commands({ program: newProgram });
|
|
141
|
+
await newProgram.parseAsync(argv);
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
init,
|
|
145
|
+
run,
|
|
146
|
+
test,
|
|
147
|
+
getPrevInitOptions: () => initOptions,
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
exports.cli = createCli();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
export declare const CONFIG_FILE_NAME = "modern.config";
|
|
3
|
+
export declare const PACKAGE_JSON_CONFIG_NAME = "modernConfig";
|
|
4
|
+
/**
|
|
5
|
+
* Get user config from package.json.
|
|
6
|
+
* @param appDirectory - App root directory.
|
|
7
|
+
* @returns modernConfig or undefined
|
|
8
|
+
*/
|
|
9
|
+
export declare const getPackageConfig: <T>(appDirectory: string, packageJsonConfig?: string) => T | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Get the file dependencies by module.children, ignore file path in node_modules and this monorepo packages default.
|
|
12
|
+
* @param filePath - Absolute file path.
|
|
13
|
+
* @returns File dependencies array.
|
|
14
|
+
*/
|
|
15
|
+
export declare const getDependencies: (filePath: string) => string[];
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param targetDir target dir
|
|
19
|
+
* @param overtime Unit of second
|
|
20
|
+
*/
|
|
21
|
+
export declare const clearFilesOverTime: (targetDir: string, overtime: number) => Promise<void>;
|
|
22
|
+
export declare const getConfigFilePath: (appDirectory: string, filePath?: string) => string | false;
|
|
23
|
+
/**
|
|
24
|
+
* Parse and load user config file, support extensions like .ts, mjs, js, ejs.
|
|
25
|
+
* @param appDirectory - App root directory, from which start search user config file.
|
|
26
|
+
* @param filePath - Specific absolute config file path.
|
|
27
|
+
* @returns Object contain config file path, user config object and dependency files used by config file.
|
|
28
|
+
*/
|
|
29
|
+
export declare const loadConfig: <T>(appDirectory: string, filePath?: string, packageJsonConfig?: string) => Promise<{
|
|
30
|
+
path: string | false;
|
|
31
|
+
config?: T | undefined;
|
|
32
|
+
dependencies?: string[] | undefined;
|
|
33
|
+
pkgConfig?: T | undefined;
|
|
34
|
+
}>;
|