diff --git a/packages/playground/src/custom.d.ts b/packages/playground/src/custom.d.ts new file mode 100644 index 0000000..a41a98c --- /dev/null +++ b/packages/playground/src/custom.d.ts @@ -0,0 +1,12 @@ +import { PageMetaDatum as IPageMetaDatum } from '@uni-helper/vite-plugin-uni-pages'; + +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/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/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/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..17132fe 100644 --- a/packages/volar/src/jsonLs.ts +++ b/packages/volar/src/jsonLs.ts @@ -1,9 +1,36 @@ 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 key = 'ExtraPageMetaDatum' + const pageMetaDatumSchema = createGenerator({ + skipTypeCheck: true, + type: key, + tsconfig: './tsconfig.json', + ...config, + }).createSchema(key); + + const pageMeta = pageMetaDatumSchema.definitions + if (pageMeta) { + schema.definitions.PageMetaDatum = { + ...schema.definitions.PageMetaDatum, + // @ts-expect-error Ignore type + ...pageMeta[key], + } + } + } 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\\",