From 84502d3590da8e982bc31152e720c3281c5ca785 Mon Sep 17 00:00:00 2001 From: liuqian Date: Mon, 27 Nov 2023 01:13:50 +0800 Subject: [PATCH 1/2] feat: support custom definitions --- packages/playground/src/custom.d.ts | 8 +++++++ packages/playground/src/pages/test.vue | 3 ++- packages/playground/volar.config.js | 2 +- packages/volar/src/index.ts | 7 ++++-- packages/volar/src/jsonLs.ts | 30 +++++++++++++++++++++++++- test/generate.spec.ts | 6 ++++-- 6 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 packages/playground/src/custom.d.ts diff --git a/packages/playground/src/custom.d.ts b/packages/playground/src/custom.d.ts new file mode 100644 index 0000000..407681b --- /dev/null +++ b/packages/playground/src/custom.d.ts @@ -0,0 +1,8 @@ +import { PageMetaDatum as SPageMetaDatum } from '@uni-helper/vite-plugin-uni-pages'; + +export interface UniPagesRouteMeta { + PageMetaDatum: Partial & { + /** 自定义属性 */ + customAttribute?: string + } +} diff --git a/packages/playground/src/pages/test.vue b/packages/playground/src/pages/test.vue index 14ad0b2..7122708 100644 --- a/packages/playground/src/pages/test.vue +++ b/packages/playground/src/pages/test.vue @@ -11,6 +11,7 @@ }, "middlewares": [ "auth" - ] + ], + "customAttribute": "custom attribute" } diff --git a/packages/playground/volar.config.js b/packages/playground/volar.config.js index a32de9e..1fdb6ec 100644 --- a/packages/playground/volar.config.js +++ b/packages/playground/volar.config.js @@ -2,6 +2,6 @@ const volarServiceUniPages = require('@uni-helper/volar-service-uni-pages') module.exports = { services: [ - volarServiceUniPages(), + volarServiceUniPages({ path: './src/custom.d.ts' }), ], } diff --git a/packages/volar/src/index.ts b/packages/volar/src/index.ts index 90fa1d1..70f8128 100644 --- a/packages/volar/src/index.ts +++ b/packages/volar/src/index.ts @@ -5,6 +5,9 @@ import { type TextDocument } from 'vscode-languageserver-textdocument' import { createJsonLs } from './jsonLs' import { createYamlLs } from './yamlLs' import { isYaml } from './utils' +import { type Config } from "ts-json-schema-generator"; + +export type PluginConfig = Pick; export interface Provide { 'json/jsonDocument': (document: TextDocument) => json.JSONDocument | undefined @@ -12,7 +15,7 @@ export interface Provide { 'yaml/languageService': () => LanguageService } -export default (): Service => (context): ReturnType> => { +export default (config: PluginConfig = {}): Service => (context): ReturnType> => { // https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/json-language-features/server/src/jsonServer.ts#L150 const triggerCharacters = ['"', ':'] @@ -20,7 +23,7 @@ export default (): Service => (context): ReturnType> = return { triggerCharacters } as any const jsonDocuments = new WeakMap() - const jsonLs = createJsonLs(context) + const jsonLs = createJsonLs(context, config) const yamlLs = createYamlLs(context) return { diff --git a/packages/volar/src/jsonLs.ts b/packages/volar/src/jsonLs.ts index 07134d0..831268c 100644 --- a/packages/volar/src/jsonLs.ts +++ b/packages/volar/src/jsonLs.ts @@ -1,9 +1,37 @@ import * as json from 'vscode-json-languageservice' import type { ServiceContext } from '@volar/language-service' import { schema } from './schema' +import { createGenerator } from "ts-json-schema-generator"; +import type { PluginConfig } from './index' +import { JSONSchema7 } from 'json-schema'; -export function createJsonLs(_context: ServiceContext) { +export function createJsonLs(_context: ServiceContext, config: PluginConfig) { const jsonLs = json.getLanguageService({}) + try { + const routeMetaSchema = createGenerator({ + skipTypeCheck: true, + type: "UniPagesRouteMeta", + tsconfig: './tsconfig.json', + ...config, + }).createSchema("UniPagesRouteMeta"); + + const routeMeta = routeMetaSchema.definitions.UniPagesRouteMeta as JSONSchema7 + if (routeMeta) { + for (const key in schema.definitions) { + const exist = routeMeta.properties[key] + if (exist && typeof exist === 'object') { + schema.definitions[key] = { + ...schema.definitions[key], + ...exist + } + } + } + } + } catch (e) { + console.log("[Error] @uni-helper/volar-service-uni-pages:"); + console.log(e); + } + jsonLs.configure({ allowComments: true, schemas: [ diff --git a/test/generate.spec.ts b/test/generate.spec.ts index 5af494f..bb4a57f 100644 --- a/test/generate.spec.ts +++ b/test/generate.spec.ts @@ -70,7 +70,8 @@ describe('generate routes', () => { }, \\"middlewares\\": [ \\"auth\\" - ] + ], + \\"customAttribute\\": \\"custom attribute\\" }, { \\"path\\": \\"../packages/playground/src/pages/blog/index\\", @@ -143,7 +144,8 @@ describe('generate routes', () => { }, \\"middlewares\\": [ \\"auth\\" - ] + ], + \\"customAttribute\\": \\"custom attribute\\" }, { \\"path\\": \\"../packages/playground/src/pages/blog/index\\", From 30358c0483aaada1795d2f4df6e2c5faee72d181 Mon Sep 17 00:00:00 2001 From: liuqian Date: Mon, 27 Nov 2023 13:38:50 +0800 Subject: [PATCH 2/2] chore: modify the type name and support typescript definition --- packages/playground/src/custom.d.ts | 16 ++++++++++------ packages/playground/tsconfig.json | 17 ++++++++++++++--- packages/volar/src/jsonLs.ts | 25 ++++++++++++------------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/packages/playground/src/custom.d.ts b/packages/playground/src/custom.d.ts index 407681b..a41a98c 100644 --- a/packages/playground/src/custom.d.ts +++ b/packages/playground/src/custom.d.ts @@ -1,8 +1,12 @@ -import { PageMetaDatum as SPageMetaDatum } from '@uni-helper/vite-plugin-uni-pages'; +import { PageMetaDatum as IPageMetaDatum } from '@uni-helper/vite-plugin-uni-pages'; -export interface UniPagesRouteMeta { - PageMetaDatum: Partial & { - /** 自定义属性 */ - customAttribute?: string - } +interface PageMeta { + /** 自定义属性 */ + customAttribute?: string +} + +interface ExtraPageMetaDatum extends PageMeta, Partial { } + +declare module '@uni-helper/vite-plugin-uni-pages' { + interface PageMetaDatum extends PageMeta { } } diff --git a/packages/playground/tsconfig.json b/packages/playground/tsconfig.json index 73b2a39..c26a417 100644 --- a/packages/playground/tsconfig.json +++ b/packages/playground/tsconfig.json @@ -2,15 +2,26 @@ "compilerOptions": { "target": "esnext", "jsx": "preserve", - "lib": ["esnext", "dom"], + "lib": [ + "esnext", + "dom" + ], "useDefineForClassFields": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, - "types": ["@dcloudio/types"], + "types": [ + "@dcloudio/types" + ], "strict": true, "sourceMap": true, "esModuleInterop": true }, - "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"] + "include": [ + "src/**/*.ts", + "src/**/*.d.ts", + "src/**/*.tsx", + "src/**/*.vue", + "pages.config.ts" + ] } diff --git a/packages/volar/src/jsonLs.ts b/packages/volar/src/jsonLs.ts index 831268c..17132fe 100644 --- a/packages/volar/src/jsonLs.ts +++ b/packages/volar/src/jsonLs.ts @@ -5,26 +5,24 @@ import { createGenerator } from "ts-json-schema-generator"; import type { PluginConfig } from './index' import { JSONSchema7 } from 'json-schema'; + export function createJsonLs(_context: ServiceContext, config: PluginConfig) { const jsonLs = json.getLanguageService({}) try { - const routeMetaSchema = createGenerator({ + const key = 'ExtraPageMetaDatum' + const pageMetaDatumSchema = createGenerator({ skipTypeCheck: true, - type: "UniPagesRouteMeta", + type: key, tsconfig: './tsconfig.json', ...config, - }).createSchema("UniPagesRouteMeta"); + }).createSchema(key); - const routeMeta = routeMetaSchema.definitions.UniPagesRouteMeta as JSONSchema7 - if (routeMeta) { - for (const key in schema.definitions) { - const exist = routeMeta.properties[key] - if (exist && typeof exist === 'object') { - schema.definitions[key] = { - ...schema.definitions[key], - ...exist - } - } + const pageMeta = pageMetaDatumSchema.definitions + if (pageMeta) { + schema.definitions.PageMetaDatum = { + ...schema.definitions.PageMetaDatum, + // @ts-expect-error Ignore type + ...pageMeta[key], } } } catch (e) { @@ -32,6 +30,7 @@ export function createJsonLs(_context: ServiceContext, config: PluginConfig) { console.log(e); } + jsonLs.configure({ allowComments: true, schemas: [