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 all commits
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
12 changes: 12 additions & 0 deletions packages/playground/src/custom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PageMetaDatum as IPageMetaDatum } from '@uni-helper/vite-plugin-uni-pages';

interface PageMeta {
/** 自定义属性 */
customAttribute?: string
}

interface ExtraPageMetaDatum extends PageMeta, Partial<IPageMetaDatum> { }

declare module '@uni-helper/vite-plugin-uni-pages' {
interface PageMetaDatum extends PageMeta { }
}
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>
17 changes: 14 additions & 3 deletions packages/playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
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
29 changes: 28 additions & 1 deletion packages/volar/src/jsonLs.ts
Original file line number Diff line number Diff line change
@@ -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: [
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