Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom definitions #117

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/playground/src/custom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PageMetaDatum as SPageMetaDatum } from '@uni-helper/vite-plugin-uni-pages';

export interface UniPagesRouteMeta {
PageMetaDatum: Partial<SPageMetaDatum> & {
/** 自定义属性 */
customAttribute?: string
}
}
3 changes: 2 additions & 1 deletion packages/playground/src/pages/test.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"middlewares": [
"auth"
]
],
"customAttribute": "custom attribute"
}
</route>
2 changes: 1 addition & 1 deletion packages/playground/volar.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ const volarServiceUniPages = require('@uni-helper/volar-service-uni-pages')

module.exports = {
services: [
volarServiceUniPages(),
volarServiceUniPages({ path: './src/custom.d.ts' }),
],
}
7 changes: 5 additions & 2 deletions packages/volar/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ 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<Config, "path" | "tsconfig">;

export interface Provide {
'json/jsonDocument': (document: TextDocument) => json.JSONDocument | undefined
'json/languageService': () => json.LanguageService
'yaml/languageService': () => LanguageService
}

export default (): Service<Provide> => (context): ReturnType<Service<Provide>> => {
export default (config: PluginConfig = {}): Service<Provide> => (context): ReturnType<Service<Provide>> => {
// https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/json-language-features/server/src/jsonServer.ts#L150
const triggerCharacters = ['"', ':']

if (!context)
return { triggerCharacters } as any

const jsonDocuments = new WeakMap<TextDocument, [number, json.JSONDocument]>()
const jsonLs = createJsonLs(context)
const jsonLs = createJsonLs(context, config)
const yamlLs = createYamlLs(context)

return {
Expand Down
30 changes: 29 additions & 1 deletion packages/volar/src/jsonLs.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down
6 changes: 4 additions & 2 deletions test/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ describe('generate routes', () => {
},
\\"middlewares\\": [
\\"auth\\"
]
],
\\"customAttribute\\": \\"custom attribute\\"
},
{
\\"path\\": \\"../packages/playground/src/pages/blog/index\\",
Expand Down Expand Up @@ -143,7 +144,8 @@ describe('generate routes', () => {
},
\\"middlewares\\": [
\\"auth\\"
]
],
\\"customAttribute\\": \\"custom attribute\\"
},
{
\\"path\\": \\"../packages/playground/src/pages/blog/index\\",
Expand Down