@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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # @modern-js/core
2
2
 
3
+ ## 1.4.2
4
+
5
+ ### Patch Changes
6
+
7
+ - b376c8d6: feat: enhance custom env
8
+ - e62c4efd: fix error typo for 'styledComponents'
9
+ - e2a8233f: support add schem error hook to core.init
10
+
3
11
  ## 1.4.1
4
12
 
5
13
  ### Patch Changes
@@ -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.instancePath && error.params.additionalProperty) {
37
- const target = `${error.instancePath.substr(1)}.${error.params.additionalProperty}`;
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
- showAdditionalPropertiesError(validate === null || validate === void 0 ? void 0 : validate.errors[0]);
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
  })), {
@@ -28,6 +28,9 @@ export const tools = {
28
28
  },
29
29
  minifyCss: {
30
30
  typeof: ['object', 'function']
31
+ },
32
+ styledComponents: {
33
+ typeof: ['object', 'function']
31
34
  }
32
35
  }
33
36
  };
@@ -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
- loadEnv(appDirectory);
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.instancePath && error.params.additionalProperty) {
75
- const target = `${error.instancePath.substr(1)}.${error.params.additionalProperty}`;
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
- showAdditionalPropertiesError(validate === null || validate === void 0 ? void 0 : validate.errors[0]);
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
  })), {
@@ -34,6 +34,9 @@ const tools = {
34
34
  },
35
35
  minifyCss: {
36
36
  typeof: ['object', 'function']
37
+ },
38
+ styledComponents: {
39
+ typeof: ['object', 'function']
37
40
  }
38
41
  }
39
42
  };
@@ -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, _loadEnv.loadEnv)(appDirectory);
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 };
@@ -456,6 +456,9 @@ export declare const patchSchema: (pluginSchemas: Array<PluginValidateSchema | P
456
456
  minifyCss: {
457
457
  typeof: string[];
458
458
  };
459
+ styledComponents: {
460
+ typeof: string[];
461
+ };
459
462
  };
460
463
  };
461
464
  };
@@ -29,5 +29,8 @@ export declare const tools: {
29
29
  minifyCss: {
30
30
  typeof: string[];
31
31
  };
32
+ styledComponents: {
33
+ typeof: string[];
34
+ };
32
35
  };
33
36
  };
@@ -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.1",
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.1",
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",
@@ -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
  });
@@ -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
  {