@modern-js/core 1.4.1 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/js/modern/config/index.js +4 -4
- package/dist/js/modern/config/schema/tools.js +3 -0
- package/dist/js/modern/index.js +5 -2
- package/dist/js/node/config/index.js +4 -4
- package/dist/js/node/config/schema/tools.js +3 -0
- package/dist/js/node/index.js +5 -2
- package/dist/types/config/index.d.ts +2 -1
- package/dist/types/config/schema/index.d.ts +3 -0
- package/dist/types/config/schema/tools.d.ts +3 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +2 -2
- package/tests/index.test.ts +1 -1
- package/tests/loadEnv.test.ts +28 -0
package/CHANGELOG.md
CHANGED
|
@@ -33,8 +33,8 @@ export const loadUserConfig = async (appDirectory, filePath, packageJsonConfig)
|
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
const showAdditionalPropertiesError = error => {
|
|
36
|
-
if (error.keyword === 'additionalProperties' && error.
|
|
37
|
-
const target =
|
|
36
|
+
if (error.keyword === 'additionalProperties' && error.params.additionalProperty) {
|
|
37
|
+
const target = [error.instancePath.slice(1), error.params.additionalProperty].filter(Boolean).join('.');
|
|
38
38
|
const name = Object.keys(PLUGIN_SCHEMAS).find(key => PLUGIN_SCHEMAS[key].some(schemaItem => schemaItem.target === target));
|
|
39
39
|
|
|
40
40
|
if (name) {
|
|
@@ -45,7 +45,7 @@ const showAdditionalPropertiesError = error => {
|
|
|
45
45
|
/* eslint-disable max-statements, max-params */
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
export const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort, argv) => {
|
|
48
|
+
export const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort, argv, onSchemaError = showAdditionalPropertiesError) => {
|
|
49
49
|
var _validate$errors;
|
|
50
50
|
|
|
51
51
|
const {
|
|
@@ -67,7 +67,7 @@ export const resolveConfig = async (loaded, configs, schemas, restartWithExistin
|
|
|
67
67
|
if (!valid && (_validate$errors = validate.errors) !== null && _validate$errors !== void 0 && _validate$errors.length) {
|
|
68
68
|
var _validate$errors2;
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
onSchemaError(validate === null || validate === void 0 ? void 0 : validate.errors[0]);
|
|
71
71
|
const errors = betterAjvErrors(validateSchema, userConfig, (_validate$errors2 = validate.errors) === null || _validate$errors2 === void 0 ? void 0 : _validate$errors2.map(e => _objectSpread(_objectSpread({}, e), {}, {
|
|
72
72
|
dataPath: e.instancePath
|
|
73
73
|
})), {
|
package/dist/js/modern/index.js
CHANGED
|
@@ -65,11 +65,14 @@ const createCli = () => {
|
|
|
65
65
|
let restartOptions; // eslint-disable-next-line max-statements
|
|
66
66
|
|
|
67
67
|
const init = async (argv = [], options) => {
|
|
68
|
+
var _options$options$meta, _options$options;
|
|
69
|
+
|
|
68
70
|
enable();
|
|
69
71
|
manager.clear();
|
|
70
72
|
restartOptions = options;
|
|
71
73
|
const appDirectory = await initAppDir();
|
|
72
|
-
|
|
74
|
+
const metaName = (_options$options$meta = options === null || options === void 0 ? void 0 : (_options$options = options.options) === null || _options$options === void 0 ? void 0 : _options$options.metaName) !== null && _options$options$meta !== void 0 ? _options$options$meta : 'MODERN';
|
|
75
|
+
loadEnv(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
|
|
73
76
|
const loaded = await loadUserConfig(appDirectory, options === null || options === void 0 ? void 0 : options.configFile, options === null || options === void 0 ? void 0 : options.packageJsonConfig);
|
|
74
77
|
let plugins = loadPlugins(appDirectory, loaded.config.plugins || [], options === null || options === void 0 ? void 0 : options.plugins);
|
|
75
78
|
|
|
@@ -100,7 +103,7 @@ const createCli = () => {
|
|
|
100
103
|
});
|
|
101
104
|
const extraConfigs = await hooksRunner.config();
|
|
102
105
|
const extraSchemas = await hooksRunner.validateSchema();
|
|
103
|
-
const config = await resolveConfig(loaded, extraConfigs, extraSchemas, restartWithExistingPort, argv);
|
|
106
|
+
const config = await resolveConfig(loaded, extraConfigs, extraSchemas, restartWithExistingPort, argv, options === null || options === void 0 ? void 0 : options.onSchemaError);
|
|
104
107
|
const {
|
|
105
108
|
resolved
|
|
106
109
|
} = await hooksRunner.resolvedConfig({
|
|
@@ -71,8 +71,8 @@ const loadUserConfig = async (appDirectory, filePath, packageJsonConfig) => {
|
|
|
71
71
|
exports.loadUserConfig = loadUserConfig;
|
|
72
72
|
|
|
73
73
|
const showAdditionalPropertiesError = error => {
|
|
74
|
-
if (error.keyword === 'additionalProperties' && error.
|
|
75
|
-
const target =
|
|
74
|
+
if (error.keyword === 'additionalProperties' && error.params.additionalProperty) {
|
|
75
|
+
const target = [error.instancePath.slice(1), error.params.additionalProperty].filter(Boolean).join('.');
|
|
76
76
|
const name = Object.keys(_utils.PLUGIN_SCHEMAS).find(key => _utils.PLUGIN_SCHEMAS[key].some(schemaItem => schemaItem.target === target));
|
|
77
77
|
|
|
78
78
|
if (name) {
|
|
@@ -83,7 +83,7 @@ const showAdditionalPropertiesError = error => {
|
|
|
83
83
|
/* eslint-disable max-statements, max-params */
|
|
84
84
|
|
|
85
85
|
|
|
86
|
-
const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort, argv) => {
|
|
86
|
+
const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort, argv, onSchemaError = showAdditionalPropertiesError) => {
|
|
87
87
|
var _validate$errors;
|
|
88
88
|
|
|
89
89
|
const {
|
|
@@ -105,7 +105,7 @@ const resolveConfig = async (loaded, configs, schemas, restartWithExistingPort,
|
|
|
105
105
|
if (!valid && (_validate$errors = validate.errors) !== null && _validate$errors !== void 0 && _validate$errors.length) {
|
|
106
106
|
var _validate$errors2;
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
onSchemaError(validate === null || validate === void 0 ? void 0 : validate.errors[0]);
|
|
109
109
|
const errors = (0, _betterAjvErrors.default)(validateSchema, userConfig, (_validate$errors2 = validate.errors) === null || _validate$errors2 === void 0 ? void 0 : _validate$errors2.map(e => _objectSpread(_objectSpread({}, e), {}, {
|
|
110
110
|
dataPath: e.instancePath
|
|
111
111
|
})), {
|
package/dist/js/node/index.js
CHANGED
|
@@ -186,11 +186,14 @@ const createCli = () => {
|
|
|
186
186
|
let restartOptions; // eslint-disable-next-line max-statements
|
|
187
187
|
|
|
188
188
|
const init = async (argv = [], options) => {
|
|
189
|
+
var _options$options$meta, _options$options;
|
|
190
|
+
|
|
189
191
|
(0, _node.enable)();
|
|
190
192
|
manager.clear();
|
|
191
193
|
restartOptions = options;
|
|
192
194
|
const appDirectory = await initAppDir();
|
|
193
|
-
(0
|
|
195
|
+
const metaName = (_options$options$meta = options === null || options === void 0 ? void 0 : (_options$options = options.options) === null || _options$options === void 0 ? void 0 : _options$options.metaName) !== null && _options$options$meta !== void 0 ? _options$options$meta : 'MODERN';
|
|
196
|
+
(0, _loadEnv.loadEnv)(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
|
|
194
197
|
const loaded = await (0, _config.loadUserConfig)(appDirectory, options === null || options === void 0 ? void 0 : options.configFile, options === null || options === void 0 ? void 0 : options.packageJsonConfig);
|
|
195
198
|
let plugins = (0, _loadPlugins.loadPlugins)(appDirectory, loaded.config.plugins || [], options === null || options === void 0 ? void 0 : options.plugins);
|
|
196
199
|
|
|
@@ -222,7 +225,7 @@ const createCli = () => {
|
|
|
222
225
|
});
|
|
223
226
|
const extraConfigs = await hooksRunner.config();
|
|
224
227
|
const extraSchemas = await hooksRunner.validateSchema();
|
|
225
|
-
const config = await (0, _config.resolveConfig)(loaded, extraConfigs, extraSchemas, restartWithExistingPort, argv);
|
|
228
|
+
const config = await (0, _config.resolveConfig)(loaded, extraConfigs, extraSchemas, restartWithExistingPort, argv, options === null || options === void 0 ? void 0 : options.onSchemaError);
|
|
226
229
|
const {
|
|
227
230
|
resolved
|
|
228
231
|
} = await hooksRunner.resolvedConfig({
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ErrorObject } from 'ajv';
|
|
1
2
|
import { MetaOptions } from '@modern-js/utils';
|
|
2
3
|
import { PluginConfig } from '../loadPlugins';
|
|
3
4
|
import { defaults } from './defaults';
|
|
@@ -128,5 +129,5 @@ interface LoadedConfig {
|
|
|
128
129
|
}
|
|
129
130
|
export declare const defineConfig: (config: ConfigParam) => ConfigParam;
|
|
130
131
|
export declare const loadUserConfig: (appDirectory: string, filePath?: string | undefined, packageJsonConfig?: string | undefined) => Promise<LoadedConfig>;
|
|
131
|
-
export declare const resolveConfig: (loaded: LoadedConfig, configs: UserConfig[], schemas: PluginValidateSchema[], restartWithExistingPort: number, argv: string[]) => Promise<NormalizedConfig>;
|
|
132
|
+
export declare const resolveConfig: (loaded: LoadedConfig, configs: UserConfig[], schemas: PluginValidateSchema[], restartWithExistingPort: number, argv: string[], onSchemaError?: (error: ErrorObject) => void) => Promise<NormalizedConfig>;
|
|
132
133
|
export type { SourceConfig, OutputConfig, ServerConfig, DevConfig, DeployConfig, ToolsConfig, RuntimeConfig, RuntimeByEntriesConfig, UserConfig, ConfigParam, LoadedConfig };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { INTERNAL_PLUGINS } from '@modern-js/utils';
|
|
2
2
|
import { ParallelWorkflow, AsyncWorkflow, Progresses2Runners, AsyncWaterfall } from '@modern-js/plugin';
|
|
3
3
|
import type { Hooks } from '@modern-js/types';
|
|
4
|
+
import { ErrorObject } from 'ajv';
|
|
4
5
|
import { Command } from './utils/commander';
|
|
5
6
|
import { AppContext, ConfigContext, IAppContext, initAppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from './context';
|
|
6
7
|
import { NormalizedConfig } from './config/mergeConfig';
|
|
@@ -109,6 +110,7 @@ export interface CoreOptions {
|
|
|
109
110
|
server: any;
|
|
110
111
|
serverPath: any;
|
|
111
112
|
}[];
|
|
113
|
+
onSchemaError?: (error: ErrorObject) => void;
|
|
112
114
|
options?: {
|
|
113
115
|
metaName?: string;
|
|
114
116
|
srcDir?: string;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.4.
|
|
14
|
+
"version": "1.4.2",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"btsm": "2.2.2",
|
|
64
64
|
"@types/babel__code-frame": "^7.0.3",
|
|
65
|
-
"@modern-js/types": "^1.3.
|
|
65
|
+
"@modern-js/types": "^1.3.2",
|
|
66
66
|
"@types/jest": "^26",
|
|
67
67
|
"@types/lodash.clonedeep": "^4.5.6",
|
|
68
68
|
"@types/lodash.mergewith": "^4.6.6",
|
package/tests/index.test.ts
CHANGED
|
@@ -67,7 +67,7 @@ describe('@modern-js/core test', () => {
|
|
|
67
67
|
};
|
|
68
68
|
options.beforeUsePlugins.mockImplementation((plugins, _) => plugins);
|
|
69
69
|
await cli.init(['dev'], options);
|
|
70
|
-
expect(loadEnv).toHaveBeenCalledWith(cwd);
|
|
70
|
+
expect(loadEnv).toHaveBeenCalledWith(cwd, undefined);
|
|
71
71
|
expect(options.beforeUsePlugins).toHaveBeenCalledWith([], {});
|
|
72
72
|
// TODO: add more test cases
|
|
73
73
|
});
|
package/tests/loadEnv.test.ts
CHANGED
|
@@ -124,6 +124,34 @@ describe('load environment variables', () => {
|
|
|
124
124
|
delete process.env.NODE_ENV;
|
|
125
125
|
});
|
|
126
126
|
|
|
127
|
+
test(`get custom .env file by MODERN_ENV`, () => {
|
|
128
|
+
createFixtures('custom_environment', [
|
|
129
|
+
{
|
|
130
|
+
name: '.env',
|
|
131
|
+
content: `DB_HOST=localhost
|
|
132
|
+
DB_USER=root
|
|
133
|
+
DB_PASS=root
|
|
134
|
+
`,
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
name: '.env.staging',
|
|
138
|
+
content: `DB_HOST=localhost
|
|
139
|
+
DB_USER=root-local-dev
|
|
140
|
+
`,
|
|
141
|
+
},
|
|
142
|
+
]);
|
|
143
|
+
|
|
144
|
+
loadEnv(path.join(fixture, 'custom_environment'), 'staging');
|
|
145
|
+
|
|
146
|
+
expect(process.env.DB_HOST).toBe('localhost');
|
|
147
|
+
|
|
148
|
+
expect(process.env.DB_USER).toBe('root-local-dev');
|
|
149
|
+
|
|
150
|
+
expect(process.env.DB_PASS).toBe('root');
|
|
151
|
+
|
|
152
|
+
delete process.env.MODERN_ENV;
|
|
153
|
+
});
|
|
154
|
+
|
|
127
155
|
test(`support dotenv-expand`, () => {
|
|
128
156
|
createFixtures('expand', [
|
|
129
157
|
{
|