From 842c92d3c9b34810363588b5b03cb76edc583792 Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 5 Nov 2022 22:13:06 +0800 Subject: [PATCH] perf(@vant/cli): generate smaller web-types.json (#11226) --- .../src/compiler/web-types/formatter.ts | 34 +++++++++++-------- .../vant-cli/src/compiler/web-types/index.ts | 2 +- .../vant-cli/src/compiler/web-types/type.ts | 1 - .../vant-cli/src/compiler/web-types/utils.ts | 6 ---- .../src/compiler/web-types/web-types.ts | 2 +- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/packages/vant-cli/src/compiler/web-types/formatter.ts b/packages/vant-cli/src/compiler/web-types/formatter.ts index bf9f8efae..fdbabf504 100644 --- a/packages/vant-cli/src/compiler/web-types/formatter.ts +++ b/packages/vant-cli/src/compiler/web-types/formatter.ts @@ -1,11 +1,6 @@ /* eslint-disable no-continue */ import { Articles } from './parser.js'; -import { - formatOptions, - formatType, - removeVersion, - toKebabCase, -} from './utils.js'; +import { formatType, removeVersion, toKebabCase } from './utils.js'; import { VueEventArgument, VueTag } from './type.js'; function formatComponentName(name: string, tagPrefix: string) { @@ -66,9 +61,6 @@ function findTag(vueTags: VueTag[], name: string) { const newTag: VueTag = { name, - slots: [], - events: [], - attributes: [], }; vueTags.push(newTag); @@ -107,12 +99,16 @@ export function formatter( const tag = findTag(vueTags, name); table.body.forEach((line) => { - const [name, desc, type, defaultVal, options] = line; - tag.attributes!.push({ + const [name, desc, type, defaultVal] = line; + + if (!tag.attributes) { + tag.attributes = []; + } + + tag.attributes.push({ name: removeVersion(name), default: defaultVal, description: desc, - options: formatOptions(options), value: { type: formatType(type), kind: 'expression', @@ -128,7 +124,12 @@ export function formatter( table.body.forEach((line) => { const [name, desc, args] = line; - tag.events!.push({ + + if (!tag.events) { + tag.events = []; + } + + tag.events.push({ name: removeVersion(name), description: desc, arguments: formatArguments(args), @@ -143,7 +144,12 @@ export function formatter( table.body.forEach((line) => { const [name, desc] = line; - tag.slots!.push({ + + if (!tag.slots) { + tag.slots = []; + } + + tag.slots.push({ name: removeVersion(name), description: desc, }); diff --git a/packages/vant-cli/src/compiler/web-types/index.ts b/packages/vant-cli/src/compiler/web-types/index.ts index ff5bc6dd6..1d4dda415 100644 --- a/packages/vant-cli/src/compiler/web-types/index.ts +++ b/packages/vant-cli/src/compiler/web-types/index.ts @@ -36,7 +36,7 @@ export async function parseAndWrite(options: Options) { const webTypes = genWebTypes(vueTags, options); fse.outputFileSync( join(options.outputDir, 'web-types.json'), - JSON.stringify(webTypes, null, 2) + JSON.stringify(webTypes) ); } diff --git a/packages/vant-cli/src/compiler/web-types/type.ts b/packages/vant-cli/src/compiler/web-types/type.ts index 58648efd9..834bba41f 100644 --- a/packages/vant-cli/src/compiler/web-types/type.ts +++ b/packages/vant-cli/src/compiler/web-types/type.ts @@ -20,7 +20,6 @@ export type VueAttribute = { name: string; default: string; description: string; - options: string[]; value: { kind: 'expression'; type: string; diff --git a/packages/vant-cli/src/compiler/web-types/utils.ts b/packages/vant-cli/src/compiler/web-types/utils.ts index 314ef16e8..a25e944f4 100644 --- a/packages/vant-cli/src/compiler/web-types/utils.ts +++ b/packages/vant-cli/src/compiler/web-types/utils.ts @@ -20,9 +20,3 @@ export function formatType(type: string) { export function normalizePath(path: string): string { return path.replace(/\\/g, '/'); } - -// `default` `primary` -> ['default', 'primary'] -export function formatOptions(options?: string) { - if (!options) return []; - return options.replace(/`/g, '').split(' '); -} diff --git a/packages/vant-cli/src/compiler/web-types/web-types.ts b/packages/vant-cli/src/compiler/web-types/web-types.ts index 144f24890..b3993696e 100644 --- a/packages/vant-cli/src/compiler/web-types/web-types.ts +++ b/packages/vant-cli/src/compiler/web-types/web-types.ts @@ -12,8 +12,8 @@ export function genWebTypes(tags: VueTag[], options: Options) { html: { tags, attributes: [], - 'types-syntax': 'typescript', }, }, + 'js-types-syntax': 'typescript', }; }