Skip to content

Commit

Permalink
refactor!(volar): move options to vueMacros option
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed May 23, 2023
1 parent 18f11f2 commit 66f18d7
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 32 deletions.
6 changes: 6 additions & 0 deletions .changeset/fluffy-gorillas-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@vue-macros/volar': minor
'@vue-macros/nuxt': patch
---

move volar options to vueMacros option
8 changes: 5 additions & 3 deletions docs/macros/define-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ emit('update:count', count + 1)
"@vue-macros/volar/define-models"
// ...more feature
],
"defineModels": {
// Only works when target is 2.7.
"unified": true
"vueMacros": {
"defineModels": {
// Only works when target is 2.7.
"unified": true
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions docs/macros/short-vmodel.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ export default defineConfig({
"@vue-macros/volar/short-vmodel"
// ...
],
// prefix
"shortVmodel": {
"prefix": "$"
"vueMacros": {
"shortVmodel": {
"prefix": "$"
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions docs/zh-CN/macros/define-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ emit('update:count', count + 1)
"@vue-macros/volar/define-models"
// ...更多功能
],
"defineModels": {
// 仅在 target 是 2.7 时有效
"unified": true
"vueMacros": {
"defineModels": {
// 仅在 target 是 2.7 时有效
"unified": true
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions docs/zh-CN/macros/short-vmodel.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ export default defineConfig({
"@vue-macros/volar/short-vmodel"
// ...
],
// prefix
"shortVmodel": {
"prefix": "$"
"vueMacros": {
"shortVmodel": {
"prefix": "$"
}
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/nuxt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
} from '@vue-macros/short-vmodel'
import { type Options, resolveOptions } from 'unplugin-vue-macros'
import { type Plugin } from 'vite'
import {} from '@nuxt/devtools'
// eslint-disable-next-line antfu/prefer-inline-type-import
import type {} from '@nuxt/devtools'
import { type VolarOptions } from '@vue-macros/volar'

export type VueMacrosOptions = Options & {
shortVmodel?: OptionsShortVmodel | false
Expand Down Expand Up @@ -67,6 +69,9 @@ export default defineNuxtModule<VueMacrosOptions>({
const vueCompilerOptions =
nuxt.options.typescript.tsConfig.vueCompilerOptions

vueCompilerOptions.vueMacros ||= {}
const volarOptions = vueCompilerOptions.vueMacros as VolarOptions

vueCompilerOptions.plugins ||= []
const volarPlugins = vueCompilerOptions.plugins

Expand Down Expand Up @@ -102,7 +107,7 @@ export default defineNuxtModule<VueMacrosOptions>({
volarPlugins.push('@vue-macros/volar/short-vmodel')

if (options.shortVmodel)
vueCompilerOptions.shortVmodel = {
volarOptions.shortVmodel = {
prefix: options.shortVmodel.prefix,
}

Expand Down
8 changes: 8 additions & 0 deletions packages/volar/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface VolarOptions {
defineModels?: {
unified?: boolean
}
shortVmodel?: {
prefix?: '::' | '$' | '*'
}
}
6 changes: 5 additions & 1 deletion packages/volar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
},
"author": "三咲智子 <sxzz@sxzz.moe>",
"files": [
"dist"
"dist",
"index.d.ts"
],
"exports": {
".": {
"types": "./index.d.ts"
},
"./*": {
"require": "./dist/*.js"
}
Expand Down
10 changes: 9 additions & 1 deletion packages/volar/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { type Segment, replace } from '@volar/vue-language-core'
import {
type Segment,
type VueCompilerOptions,
replace,
} from '@volar/vue-language-core'
import { type FileRangeCapabilities } from '@volar/language-core'

export function getVueLibraryName(vueVersion: number) {
Expand Down Expand Up @@ -42,3 +46,7 @@ export function addEmits(
)
return true
}

export function getVolarOptions(vueCompilerOptions: VueCompilerOptions) {
return vueCompilerOptions.vueMacros
}
10 changes: 8 additions & 2 deletions packages/volar/src/define-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
type Sfc,
type VueLanguagePlugin,
} from '@volar/vue-language-core'
import { addEmits, addProps, getVueLibraryName } from './common'
import {
addEmits,
addProps,
getVolarOptions,
getVueLibraryName,
} from './common'

function transformDefineModels({
codes,
Expand Down Expand Up @@ -117,8 +122,9 @@ const plugin: VueLanguagePlugin = ({

const vueVersion = vueCompilerOptions.target
const vueLibName = getVueLibraryName(vueVersion)
const volarOptions = getVolarOptions(vueCompilerOptions)
const unified =
(vueVersion < 3 && vueCompilerOptions?.defineModels?.unified) ?? true
vueVersion < 3 && (volarOptions?.defineModels?.unified ?? true)

transformDefineModels({
codes: embeddedFile.content,
Expand Down
4 changes: 1 addition & 3 deletions packages/volar/src/shim.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
declare module '@volar/vue-language-core' {
export interface VueCompilerOptions {
defineModels?: {
unified?: boolean
}
vueMacros?: import('..').VolarOptions
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/volar/src/short-vmodel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { transformShortVmodel } from '@vue-macros/short-vmodel'
import { type VueLanguagePlugin } from '@volar/vue-language-core'
import { getVolarOptions } from './common'

const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
return {
name: 'vue-macros-short-vmodel',
version: 1,
resolveTemplateCompilerOptions(options) {
const volarOptions = getVolarOptions(vueCompilerOptions)

options.nodeTransforms ||= []
options.nodeTransforms.push(
transformShortVmodel({
prefix: (vueCompilerOptions as any)?.shortVmodel?.prefix ?? '$',
prefix: volarOptions?.shortVmodel?.prefix || '$',
})
)
return options
Expand Down
10 changes: 6 additions & 4 deletions playground/vue2/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
"@vue-macros/volar/define-models",
"@vue-macros/volar/define-slots"
],
"defineModels": {
"unified": true
},
"experimentalDefinePropProposal": "johnsonEdition"
"experimentalDefinePropProposal": "johnsonEdition",
"vueMacros": {
"defineModels": {
"unified": true
}
}
},
"include": ["src", "*"]
}
10 changes: 6 additions & 4 deletions playground/vue3/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
"@vue-macros/volar/define-slots",
"@vue-macros/volar/export-props"
],
"shortVmodel": {
"prefix": "$"
},
"experimentalDefinePropProposal": "kevinEdition"
"experimentalDefinePropProposal": "kevinEdition",
"vueMacros": {
"shortVmodel": {
"prefix": "$"
}
}
},
"include": ["src", "*", "../../packages/shim.d.ts"]
}
6 changes: 4 additions & 2 deletions tsconfig.fixture.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"@vue-macros/volar/define-slots",
"@vue-macros/volar/export-props"
],
"shortVmodel": {
"prefix": "$"
"vueMacros": {
"shortVmodel": {
"prefix": "$"
}
}
}
}

0 comments on commit 66f18d7

Please sign in to comment.