Skip to content

Commit

Permalink
chore: remove ajv schema verification of modern configuration (#4911)
Browse files Browse the repository at this point in the history
  • Loading branch information
zllkjc authored Nov 6, 2023
1 parent 477f18d commit 95f15d2
Show file tree
Hide file tree
Showing 78 changed files with 204 additions and 2,556 deletions.
22 changes: 22 additions & 0 deletions .changeset/eighty-scissors-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@modern-js/plugin-router-v5': minor
'@modern-js/plugin-garfish': minor
'@modern-js/runtime': minor
'@modern-js/plugin-testing': minor
'@modern-js/module-tools': minor
'@modern-js/plugin-worker': minor
'@modern-js/plugin-tailwindcss': minor
'@modern-js/app-tools': minor
'@modern-js/main-doc': minor
'@modern-js/plugin-proxy': minor
'@modern-js/plugin-devtools': minor
'@modern-js/plugin-bff': minor
'@modern-js/plugin-ssg': minor
'@modern-js/plugin-swc': minor
'@modern-js/utils': minor
'@modern-js/core': minor
'@scripts/prebundle': minor
---

chore: remove ajv schema verification of configuration
chore: 移除 ajv 对项目配置的校验
69 changes: 3 additions & 66 deletions packages/cli/core/src/config/createResolvedConfig.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { createDebugger, logger } from '@modern-js/utils';
import type { ErrorObject } from '@modern-js/utils/ajv';
import { patchSchema } from '../schema/patchSchema';
import type {
UserConfig,
NormalizedConfig,
LoadedConfig,
PluginValidateSchema,
} from '../types';
import { repeatKeyWarning } from '../utils/repeatKeyWarning';
import { createDebugger } from '@modern-js/utils';
import type { UserConfig, NormalizedConfig, LoadedConfig } from '../types';
import { mergeConfig } from '../utils/mergeConfig';
import { createDefaultConfig } from './createDefaultConfig';

Expand All @@ -17,64 +9,9 @@ export const createResolveConfig = async (
// eslint-disable-next-line @typescript-eslint/ban-types
loaded: LoadedConfig<{}>,
configs: UserConfig[],
schemas: PluginValidateSchema[],
onSchemaError?: (error: ErrorObject) => void | Promise<void>,
): Promise<NormalizedConfig> => {
const { default: Ajv } = await import('@modern-js/utils/ajv');
const { default: ajvKeywords } = await import(
'@modern-js/utils/ajv-keywords'
);
const { default: betterAjvErrors } = await import(
'@modern-js/utils/better-ajv-errors'
);

const { config: userConfig, jsConfig, pkgConfig } = loaded;

const ajv = new Ajv({ $data: true, strict: false });

ajvKeywords(ajv);

const validateSchema = patchSchema(schemas);
const validate = ajv.compile(validateSchema);

repeatKeyWarning(validateSchema, jsConfig, pkgConfig);

// validate user config.
const valid = validate(userConfig);

const formatValidateError = (config: UserConfig) =>
betterAjvErrors(
validateSchema,
config,
validate.errors?.map(e => ({
...e,
dataPath: e.instancePath,
})),
{
indent: 2,
},
);

if (!valid && validate.errors?.length) {
if (onSchemaError) {
await onSchemaError(validate?.errors[0]);
}
const errors = formatValidateError(userConfig);
logger.log(errors);
throw new Error(`Validate configuration error`);
}

// validate config from plugins.
for (const config of configs) {
if (!validate(config)) {
const errors = formatValidateError(config);
logger.error(errors);
throw new Error(`Validate configuration error.`);
}
}

const { config: userConfig } = loaded;
const resolved = mergeConfig([createDefaultConfig(), ...configs, userConfig]);

debug('resolved %o', resolved);

return resolved;
Expand Down
9 changes: 1 addition & 8 deletions packages/cli/core/src/createCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,7 @@ export const createCli = () => {

const extraConfigs = await hooksRunner.config();

const extraSchemas = await hooksRunner.validateSchema();

const normalizedConfig = await createResolveConfig(
loaded,
extraConfigs,
extraSchemas,
options?.onSchemaError,
);
const normalizedConfig = await createResolveConfig(loaded, extraConfigs);

const { resolved } = await hooksRunner.resolvedConfig({
resolved: normalizedConfig,
Expand Down
1 change: 0 additions & 1 deletion packages/cli/core/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const baseHooks: BaseHooks<{}> = {
beforeConfig: createAsyncWorkflow(),
config: createParallelWorkflow(),
resolvedConfig: createAsyncWaterfall(),
validateSchema: createParallelWorkflow(),
prepare: createAsyncWorkflow(),
afterPrepare: createAsyncWorkflow(),
commands: createAsyncWorkflow(),
Expand Down
65 changes: 0 additions & 65 deletions packages/cli/core/src/schema/patchSchema.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/cli/core/src/schema/traverseSchema.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/cli/core/src/types/coreOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ErrorObject } from '@modern-js/utils/compiled/ajv';
import type { InternalPlugins } from './plugin';
import type { UserConfig } from './config';

Expand All @@ -17,7 +16,6 @@ export interface CoreOptions {
server?: InternalPlugins;
autoLoad?: InternalPlugins;
};
onSchemaError?: (error: ErrorObject) => void;
options?: {
metaName?: string;
srcDir?: string;
Expand Down
1 change: 0 additions & 1 deletion packages/cli/core/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type BaseHooks<
resolvedConfig: AsyncWaterfall<{
resolved: NormalizedConfig<Extends>;
}>;
validateSchema: ParallelWorkflow<void>;
prepare: AsyncWorkflow<void, void>;
afterPrepare: AsyncWorkflow<void, void>;
watchFiles: ParallelWorkflow<
Expand Down
7 changes: 0 additions & 7 deletions packages/cli/core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import type { JSONSchemaType } from '@modern-js/utils/ajv/json-schema';

export * from './plugin';
export * from './config';
export * from './pluginAPI';
export * from './hooks';
export * from './context';
export * from './coreOptions';

export interface PluginValidateSchema {
target: string;
schema: JSONSchemaType<any>;
}
1 change: 0 additions & 1 deletion packages/cli/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export * from './commander';
export * from './createFileWatcher';
export * from './initAppDir';
export * from './mergeConfig';
export * from './repeatKeyWarning';
export * from './checkIsDuplicationPlugin';
29 changes: 0 additions & 29 deletions packages/cli/core/src/utils/repeatKeyWarning.ts

This file was deleted.

68 changes: 0 additions & 68 deletions packages/cli/core/tests/repeatKeyWarning.test.ts

This file was deleted.

Loading

0 comments on commit 95f15d2

Please sign in to comment.