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(language-server): generate template virtual code #69

Merged
merged 22 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cc48e83
feat(language-server): generate template virtual code
johnsoncodehk Mar 13, 2024
1a5ee52
chore: lint fix
johnsoncodehk Mar 13, 2024
84133d1
fix mapping
johnsoncodehk Mar 15, 2024
421ec36
implement todo
johnsoncodehk Mar 15, 2024
c7b5119
fix component types
johnsoncodehk Mar 15, 2024
97120f1
fix auto closing tags not working
johnsoncodehk Mar 15, 2024
b560215
Merge branch 'main' into template-codegen
ShenQingchuan Mar 16, 2024
e126df4
wip: try migrating to ts plugin
ShenQingchuan Mar 21, 2024
3a81608
fix: refactor vine compiler options
ShenQingchuan Mar 21, 2024
db62684
wip: adjust ts plugin output dir
ShenQingchuan Mar 21, 2024
2df4f1e
Merge branch 'main' of https://github.com/vue-vine/vue-vine into temp…
ShenQingchuan Mar 21, 2024
604580b
Merge branch 'main' of github.com:vue-vine/vue-vine into template-cod…
ShenQingchuan Mar 22, 2024
06a074e
make ts plugin working
johnsoncodehk Mar 26, 2024
28ccc0a
avoid duplicate intellisense
johnsoncodehk Mar 26, 2024
5ee1998
Merge branch 'main' of github.com:vue-vine/vue-vine into template-cod…
ShenQingchuan Mar 26, 2024
c7dd136
Merge branch 'template-codegen' of github.com:johnsoncodehk/vue-vine …
ShenQingchuan Mar 26, 2024
c0233e5
fix formatting
johnsoncodehk Mar 26, 2024
49e7f33
pin volar typescript
johnsoncodehk Mar 26, 2024
206f831
Merge branch 'template-codegen' of github.com:johnsoncodehk/vue-vine …
ShenQingchuan Mar 26, 2024
34b624a
chore: fix lint
ShenQingchuan Mar 26, 2024
8f0b85b
fix semantic tokens not working
johnsoncodehk Mar 26, 2024
506a4ab
chore: fix lint issues
ShenQingchuan Mar 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
"dev:compiler": "cross-env NODE_ENV=development pnpm --filter @vue-vine/compiler run dev",
"dev:vite": "cross-env NODE_ENV=development pnpm --filter @vue-vine/vite-plugin run dev",
"dev:main": "cross-env NODE_ENV=development pnpm --filter vue-vine run dev",
"dev:ls": "cross-env NODE_ENV=production pnpm --filter @vue-vine/language-server run dev",
"dev:lang-service": "cross-env NODE_ENV=development pnpm --filter @vue-vine/language-service run dev",
"dev:lang-server": "cross-env NODE_ENV=development pnpm --filter @vue-vine/language-server run dev",
"dev:ext": "cross-env NODE_ENV=development esno scripts/ext-dev.js",
"build:compiler": "cross-env NODE_ENV=production pnpm --filter @vue-vine/compiler run build",
"build:vite": "cross-env NODE_ENV=production pnpm --filter @vue-vine/vite-plugin run build",
"build:ls": "cross-env NODE_ENV=production pnpm --filter @vue-vine/language-server run build",
"build:ls": "cross-env NODE_ENV=production pnpm build:lang-service && pnpm build:lang-server",
"build:lang-service": "cross-env NODE_ENV=production pnpm --filter @vue-vine/language-service run build",
"build:lang-server": "cross-env NODE_ENV=production pnpm --filter @vue-vine/language-server run build",
"build:eslint-parser": "cross-env NODE_ENV=production pnpm --filter @vue-vine/eslint-parser run build",
"build:main": "cross-env NODE_ENV=production pnpm --filter vue-vine run build",
"build:ext": "cross-env NODE_ENV=production pnpm --filter vue-vine-extension run build",
Expand Down Expand Up @@ -60,6 +63,7 @@
},
"pnpm": {
"overrides": {
"@volar/typescript": "2.1.5-patch.2",
"vscode-uri": "^3.0.8"
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"@babel/parser": "^7.22.14",
"@babel/types": "^7.22.11",
"@vue/compiler-dom": "^3.3.4",
"@vue/compiler-dom": "^3.4.21",
"estree-walker": "^2.0.2",
"hash-sum": "^2.0.0",
"line-column": "^1.0.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/src/template/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export function createSeparatedTemplateComposer(
const compileResult = compileVineTemplate(
templateSource,
{
...compilerHooks.getCompilerCtx()?.options?.vueCompilerOptions ?? {},
scopeId: `data-v-${vineFnCompCtx.scopeId}`,
bindingMetadata,
inline: false,
Expand Down Expand Up @@ -332,6 +333,7 @@ export function createInlineTemplateComposer(
const compileResult = compileVineTemplate(
templateSource,
{
...compilerHooks.getCompilerCtx()?.options?.vueCompilerOptions ?? {},
scopeId: `data-v-${vineCompFnCtx.scopeId}`,
bindingMetadata,
onError: (e) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export function transformFile(
compilerHooks: VineCompilerHooks,
inline = true,
) {
const isDev = compilerHooks.getCompilerCtx().options.mode !== 'production'
const isDev = compilerHooks.getCompilerCtx().options.envMode !== 'production'
const ms = vineFileCtx.fileMagicCode

// Traverse file context's `styleDefine`, and generate import statements.
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
CompilerOptions,
RootNode,
BindingTypes as VueBindingTypes,
} from '@vue/compiler-dom'
Expand Down Expand Up @@ -56,7 +57,8 @@ export interface VineCompilerHooks {
}

export interface VineCompilerOptions {
mode?: string // 'development' | 'production'
envMode?: string // 'development' | 'production'
vueCompilerOptions?: CompilerOptions
inlineTemplate?: boolean
preprocessOptions?: Record<string, any>
postcssOptions?: any
Expand Down
40 changes: 32 additions & 8 deletions packages/compiler/tests/__snapshots__/analyze.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"children": [
{
"content": {
"ast": null,
"constType": 0,
"content": "__props.title",
"isStatic": false,
Expand Down Expand Up @@ -899,6 +900,7 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"codegenNode": {
"children": {
"content": {
"ast": null,
"constType": 0,
"content": "__props.title",
"isStatic": false,
Expand Down Expand Up @@ -955,7 +957,6 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"tag": ""h1"",
"type": 13,
},
"isSelfClosing": false,
"loc": {
"end": {
"column": 27,
Expand Down Expand Up @@ -1025,6 +1026,7 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"children": [
{
"content": {
"ast": null,
"constType": 0,
"content": "__props.title",
"isStatic": false,
Expand Down Expand Up @@ -1062,6 +1064,7 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"codegenNode": {
"children": {
"content": {
"ast": null,
"constType": 0,
"content": "__props.title",
"isStatic": false,
Expand Down Expand Up @@ -1118,7 +1121,6 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"tag": ""h1"",
"type": 13,
},
"isSelfClosing": false,
"loc": {
"end": {
"column": 27,
Expand Down Expand Up @@ -1208,7 +1210,6 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"tag": ""div"",
"type": 13,
},
"isSelfClosing": false,
"loc": {
"end": {
"column": 11,
Expand Down Expand Up @@ -1238,6 +1239,7 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"children": [
{
"content": {
"ast": null,
"constType": 0,
"content": "__props.title",
"isStatic": false,
Expand Down Expand Up @@ -1275,6 +1277,7 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"codegenNode": {
"children": {
"content": {
"ast": null,
"constType": 0,
"content": "__props.title",
"isStatic": false,
Expand Down Expand Up @@ -1331,7 +1334,6 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"tag": ""h1"",
"type": 13,
},
"isSelfClosing": false,
"loc": {
"end": {
"column": 27,
Expand Down Expand Up @@ -1451,7 +1453,14 @@ exports[`test Vine compiler analyze > analyze vine template 1`] = `
"offset": 0,
},
},
"source": "
<div>
<h1>{{ title }}</h1>
<slot />
</div>
",
"temps": 0,
"transformed": true,
"type": 0,
}
`;
Expand Down Expand Up @@ -1542,7 +1551,6 @@ exports[`test Vine compiler analyze > analyze vine template 2`] = `
},
"type": 4,
},
"isSelfClosing": false,
"loc": {
"end": {
"column": 36,
Expand Down Expand Up @@ -1713,7 +1721,6 @@ exports[`test Vine compiler analyze > analyze vine template 2`] = `
},
"type": 4,
},
"isSelfClosing": false,
"loc": {
"end": {
"column": 36,
Expand Down Expand Up @@ -1892,7 +1899,6 @@ exports[`test Vine compiler analyze > analyze vine template 2`] = `
"tag": "MyBox",
"type": 13,
},
"isSelfClosing": false,
"loc": {
"end": {
"column": 13,
Expand Down Expand Up @@ -1925,6 +1931,19 @@ exports[`test Vine compiler analyze > analyze vine template 2`] = `
},
},
"name": "title",
"nameLoc": {
"end": {
"column": 17,
"line": 2,
"offset": 17,
},
"source": "title",
"start": {
"column": 12,
"line": 2,
"offset": 12,
},
},
"type": 6,
"value": {
"content": "Test template",
Expand Down Expand Up @@ -2100,7 +2119,6 @@ exports[`test Vine compiler analyze > analyze vine template 2`] = `
},
"type": 4,
},
"isSelfClosing": false,
"loc": {
"end": {
"column": 36,
Expand Down Expand Up @@ -2351,7 +2369,13 @@ exports[`test Vine compiler analyze > analyze vine template 2`] = `
"offset": 0,
},
},
"source": "
<MyBox title="Test template">
<div>Test inner content</div>
</MyBox>
",
"temps": 0,
"transformed": true,
"type": 0,
}
`;
2 changes: 1 addition & 1 deletion packages/compiler/tests/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('test transform', () => {

it('not output HMR content in non-dev mode', async () => {
const { mockCompilerCtx, mockCompilerHooks } = createMockTransformCtx({
mode: 'production',
envMode: 'production',
})
compileVineTypeScriptFile(testContent, 'testNoHMRContentOnProduction', mockCompilerHooks)
expect(mockCompilerCtx.vineCompileErrors.length).toBe(0)
Expand Down
17 changes: 9 additions & 8 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@
"prepublishOnly": "pnpm run build"
},
"dependencies": {
"@volar/language-core": "~2.1.2",
"@volar/language-server": "~2.1.2",
"@volar/language-service": "~2.1.2",
"@vue-vine/compiler": "^0.0.1",
"@volar/language-core": "~2.1.5",
"@volar/language-server": "~2.1.5",
"@volar/language-service": "~2.1.5",
"@vue-vine/compiler": "workspace:*",
"@vue-vine/language-service": "workspace:*",
"@vue/language-core": "^2.0.6",
"@vue/language-service": "^2.0.6",
"volar-service-css": "0.0.31",
"volar-service-emmet": "0.0.31",
"volar-service-html": "0.0.31",
"volar-service-typescript": "0.0.31-patch.1"
"volar-service-css": "0.0.34",
"volar-service-emmet": "0.0.34",
"volar-service-html": "0.0.34",
"volar-service-typescript": "0.0.34"
},
"devDependencies": {
"@types/node": "^20.11.25"
Expand Down
51 changes: 39 additions & 12 deletions packages/language-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,74 @@
import {
createConnection,
createServer,
createSimpleProjectProviderFactory,
createTypeScriptProjectProviderFactory,
loadTsdkByPath,
} from '@volar/language-server/node'
import type * as ts from 'typescript'
import { create as createCssService } from 'volar-service-css'
import { create as createEmmetService } from 'volar-service-emmet'
import { create as createHtmlService } from 'volar-service-html'
import { create as createTypeScriptService } from 'volar-service-typescript'
import { create as createTypeScriptServices } from 'volar-service-typescript'

import { createVueVineLanguagePlugin } from './language-service'
import type { VueCompilerOptions } from '@vue/language-core'
import { createParsedCommandLine, resolveVueCompilerOptions } from '@vue/language-core'
import { createVueVineLanguagePlugin } from '@vue-vine/language-service'

const debug = false

const connection = createConnection()
const server = createServer(connection)

connection.listen()

connection.onInitialize((params) => {
connection.onInitialize(async (params) => {
const tsdk = loadTsdkByPath(
params.initializationOptions.typescript.tsdk,
params.locale,
)

return server.initialize(
const result = await server.initialize(
params,
createTypeScriptProjectProviderFactory(
tsdk.typescript,
tsdk.diagnosticMessages,
),
debug
? createTypeScriptProjectProviderFactory(
tsdk.typescript,
tsdk.diagnosticMessages,
)
: createSimpleProjectProviderFactory(),
{
getLanguagePlugins() {
return [createVueVineLanguagePlugin(tsdk.typescript)]
getLanguagePlugins(env, projectContext) {
let compilerOptions: ts.CompilerOptions = {}
let vueCompilerOptions: VueCompilerOptions
if (projectContext.typescript?.configFileName) {
const { vueOptions, options } = createParsedCommandLine(tsdk.typescript, tsdk.typescript.sys, projectContext.typescript.configFileName)
vueCompilerOptions = resolveVueCompilerOptions(vueOptions)
compilerOptions = options
}
else {
vueCompilerOptions = resolveVueCompilerOptions({})
}
return [createVueVineLanguagePlugin(tsdk.typescript, compilerOptions, vueCompilerOptions)]
},
getServicePlugins() {
return [
const plugins = [
createHtmlService(),
createCssService(),
createEmmetService(),
createTypeScriptService(tsdk.typescript),
]
if (debug) {
plugins.push(...createTypeScriptServices(tsdk.typescript))
}
return plugins
},
},
)

// tsserver already provides semantic tokens
// TODO: handle in upstream instead of here
result.capabilities.semanticTokensProvider = undefined

return result
})

connection.onInitialized(server.initialized)
Expand Down