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

feature: Add vue3 support #3

Open
wants to merge 1 commit into
base: master
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: 7 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ts_module from 'typescript/lib/tsserverlibrary';
import { parseComponent } from 'vue-template-compiler';
import { parse as parseComponent} from '@vue/compiler-sfc';
import path = require('path');

function isVue(filename: string): boolean {
Expand All @@ -12,7 +12,7 @@ function isVueProject(path: string) {

function parse(text: string) {
const output = parseComponent(text, { pad: "space" });
return output && output.script && output.script.content || 'export default {}';
return output && output.descriptor && output.descriptor.script && output.descriptor.script.content || 'export default {}';
}

let clssf: typeof ts_module.createLanguageServiceSourceFile;
Expand Down Expand Up @@ -141,15 +141,17 @@ function init({ typescript: ts } : {typescript: typeof ts_module}) {
//logger.info(exportDefaultObject.toString());
const vueImport = b(ts.createImportDeclaration(undefined,
undefined,
b(ts.createImportClause(b(ts.createIdentifier("Vue")), undefined)),
b(ts.createImportClause(undefined, b(ts.createNamedImports([
b(ts.createImportSpecifier(undefined,b(ts.createIdentifier("defineComponent"))))
])))),
b(ts.createLiteral("vue"))));
statements.unshift(vueImport);
const obj = (exportDefaultObject as ts.ExportAssignment).expression as ts.ObjectLiteralExpression;
(exportDefaultObject as ts.ExportAssignment).expression = ts.setTextRange(ts.createNew(ts.setTextRange(ts.createIdentifier("Vue"), { pos: obj.pos, end: obj.pos + 1 }),
(exportDefaultObject as ts.ExportAssignment).expression = ts.setTextRange(ts.createCall(ts.setTextRange(ts.createIdentifier("defineComponent"), { pos: obj.pos, end: obj.pos + 1 }),
undefined,
[obj]),
obj);
ts.setTextRange(((exportDefaultObject as ts.ExportAssignment).expression as ts.NewExpression).arguments!, obj);
ts.setTextRange(((exportDefaultObject as ts.ExportAssignment).expression as ts.CallExpression).arguments!, obj);
}
}

Expand Down
30 changes: 24 additions & 6 deletions lib/typing.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
declare module 'vue-template-compiler' {
export function parseComponent(text: string, option: {pad: string}): {
script?: {
content: string
}
}
declare module '@vue/compiler-sfc' {
export interface SFCParseOptions {
pad?: boolean | 'line' | 'space'
}

export interface SFCBlock {
content: string
}

export interface SFCScriptBlock extends SFCBlock {
}

export interface SFCDescriptor {
script: SFCScriptBlock | null
}

export interface SFCParseResult {
descriptor: SFCDescriptor
}

export function parse(
source: string,
options: SFCParseOptions
): SFCParseResult
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
},
"homepage": "https://github.com/HerringtonDarkholme/vue-ts-plugin#readme",
"devDependencies": {
"@types/node": "^7.0.5",
"@types/node": "7.0.5",
"typescript": "^2.7.0"
},
"peer-dependencies": {
"vue-template-compiler": "*"
"@vue/compiler-sfc": "*"
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"noImplicitAny": false,
"strict": true,
"sourceMap": false,
"outDir": "bin"
"outDir": "bin",
"skipLibCheck": true
}
}