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 vue 2.7 #529

Merged
merged 14 commits into from Oct 24, 2022
10 changes: 10 additions & 0 deletions src/core/context.ts
Expand Up @@ -22,6 +22,7 @@ const debug = {
export class Context {
options: ResolvedOptions
transformer: Transformer = undefined!
vue27 = false

private _componentPaths = new Set<string>()
private _componentNameMap: Record<string, ComponentInfo> = {}
Expand Down Expand Up @@ -58,6 +59,15 @@ export class Context {
this.transformer = transformer(this, name || 'vue3')
}

setVue27(vue27Flag: boolean) {
debug.env('vue2.7+', vue27Flag)
this.vue27 = vue27Flag
}

getVue27() {
return this.vue27
}

transform(code: string, id: string) {
const { path, query } = parseId(id)
return this.transformer(code, id, path, query)
Expand Down
14 changes: 11 additions & 3 deletions src/core/declaration.ts
Expand Up @@ -114,15 +114,23 @@ export function getDeclaration(ctx: Context, filepath: string, originalImports?:
directive: stringifyDeclarationImports({ ...originalImports?.directive, ...imports.directive }),
}

let code = `// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
let dtsHead = ''
ctx.getVue27()
? dtsHead = `
export {}

declare module 'vue' {`
: dtsHead = `
import '@vue/runtime-core'

export {}

declare module '@vue/runtime-core' {`

let code = `// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399${dtsHead}`

if (Object.keys(declarations.component).length > 0) {
code += `
export interface GlobalComponents {
Expand Down
5 changes: 4 additions & 1 deletion src/core/unplugin.ts
Expand Up @@ -54,8 +54,11 @@ export default createUnplugin<Options>((options = {}) => {
ctx.setRoot(config.root)
ctx.sourcemap = true

if (config.plugins.find(i => i.name === 'vite-plugin-vue2'))
if (config.plugins.find(i => i.name === 'vite-plugin-vue2' || i.name === '@vitejs/plugin-vue2')) {
ctx.setTransformer('vue2')
if (config.plugins.find(i => i.name === '@vitejs/plugin-vue2'))
ctx.setVue27(true)
}

if (ctx.options.dts) {
ctx.searchGlob()
Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/dts.test.ts.snap
Expand Up @@ -18,6 +18,22 @@ declare module '@vue/runtime-core' {
"
`;

exports[`dts > components only vue2.7 1`] = `
"// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
export {}

declare module 'vue' {
export interface GlobalComponents {
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
TestComp: typeof import('test/component/TestComp')['default']
}
}
"
`;

exports[`dts > directive only 1`] = `
"// generated by unplugin-vue-components
// We suggest you to commit this file into source control
Expand Down
13 changes: 13 additions & 0 deletions test/dts.test.ts
Expand Up @@ -95,6 +95,19 @@ const _directive_loading = _resolveDirective("loading")`
expect(declarations).toMatchSnapshot()
})

test('components only vue2.7', async () => {
const ctx = new Context({
resolvers: resolver,
directives: true,
})
ctx.setVue27(true)
const code = 'const _component_test_comp = _resolveComponent("test-comp")'
await ctx.transform(code, '')

const declarations = getDeclaration(ctx, 'test.d.ts')
expect(declarations).toMatchSnapshot()
})

test('directive only', async () => {
const ctx = new Context({
resolvers: resolver,
Expand Down