Skip to content

Commit

Permalink
refactor(nuxt): all use vite plugin in nuxt
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Aug 29, 2023
1 parent 194693c commit c848deb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 62 deletions.
8 changes: 8 additions & 0 deletions .changeset/eleven-parents-juggle.md
@@ -0,0 +1,8 @@
---
'@vue-macros/nuxt': minor
'@vue-macros/boolean-prop': patch
'@vue-macros/short-vmodel': patch
'@vue-macros/common': patch
---

use Vite plugin for booleanProp and shortVmodel in Nuxt
27 changes: 16 additions & 11 deletions packages/boolean-prop/src/index.ts
@@ -1,5 +1,4 @@
import { type Plugin } from 'rollup'
import { type Plugin as VitePlugin } from 'vite'
import { type Plugin } from 'vite'
import { type VuePluginApi, getVuePluginApi } from '@vue-macros/common'
import { type Options, transformBooleanProp } from './core/index'
import { generatePluginName } from '#macros' assert { type: 'macro' }
Expand All @@ -10,17 +9,23 @@ export * from './api'
const name = generatePluginName()

function rollup(options: Options = {}): Plugin {
let api: VuePluginApi

return {
name,
buildStart(rollupOpts) {
let api: VuePluginApi

configResolved(config) {
try {
api = getVuePluginApi(rollupOpts)
} catch (error: any) {
this.warn(error)
return
}
api = getVuePluginApi(config.plugins)
} catch {}
},
buildStart(rollupOpts) {
if (!api)
try {
api = getVuePluginApi(rollupOpts.plugins)
} catch (error: any) {
this.warn(error)
return
}

api.options.template ||= {}
api.options.template.compilerOptions ||= {}
Expand All @@ -35,5 +40,5 @@ function rollup(options: Options = {}): Plugin {

export default {
rollup,
vite: rollup as (options?: Options) => VitePlugin,
vite: rollup,
}
9 changes: 5 additions & 4 deletions packages/common/src/unplugin.ts
Expand Up @@ -4,7 +4,8 @@ import {
} from '@rollup/pluginutils'
import { generateTransform } from 'magic-string-ast'
import { type ResolvedOptions } from '@vitejs/plugin-vue'
import { type NormalizedInputOptions } from 'rollup'
import { type Plugin } from 'rollup'
import { type Plugin as VitePlugin } from 'vite'

/** @deprecated use `generateTransform` instead */
export const getTransformResult = generateTransform
Expand All @@ -27,14 +28,14 @@ export interface VuePluginApi {
}

export function getVuePluginApi(
rollupOpts: NormalizedInputOptions
plugins: Readonly<(Plugin | VitePlugin)[]> | undefined
): VuePluginApi {
const vuePlugin = rollupOpts.plugins.find(
const vuePlugin = (plugins || []).find(
(p) => p.name === 'vite:vue' || p.name === 'unplugin-vue'
)
if (!vuePlugin) {
throw new Error(
'Cannot find Vue plugin (@vitejs/plugin-vue or unplugin-vue).\nPlease make sure to add it before using shortVmodel.'
'Cannot find Vue plugin (@vitejs/plugin-vue or unplugin-vue). Please make sure to add it before using Vue Macros.'
)
}

Expand Down
2 changes: 0 additions & 2 deletions packages/nuxt/package.json
Expand Up @@ -49,9 +49,7 @@
},
"dependencies": {
"@nuxt/kit": "^3.7.0",
"@vue-macros/boolean-prop": "workspace:*",
"@vue-macros/common": "workspace:~",
"@vue-macros/short-vmodel": "workspace:*",
"@vue-macros/volar": "workspace:*",
"unplugin-vue-macros": "workspace:*"
},
Expand Down
36 changes: 8 additions & 28 deletions packages/nuxt/src/index.ts
@@ -1,20 +1,12 @@
import { defineNuxtModule, useNuxt } from '@nuxt/kit'
import VueMacros from 'unplugin-vue-macros/vite'
import {
type Options as OptionsShortVmodel,
transformShortVmodel,
} from '@vue-macros/short-vmodel'
import { transformBooleanProp } from '@vue-macros/boolean-prop'
import { type Options, resolveOptions } from 'unplugin-vue-macros'
import { type Plugin } from 'vite'
import type {} from '@nuxt/devtools'
import { type VolarOptions } from '@vue-macros/volar'
import { REGEX_SETUP_SFC } from '@vue-macros/common'

export type VueMacrosOptions = Options & {
booleanProp?: {} | false
shortVmodel?: OptionsShortVmodel | false
}
export type VueMacrosOptions = Options

export default defineNuxtModule<VueMacrosOptions>({
meta: {
Expand Down Expand Up @@ -104,6 +96,13 @@ export default defineNuxtModule<VueMacrosOptions>({
vueCompilerOptions.experimentalDefinePropProposal =
resolvedOptions.defineProp.edition || 'kevinEdition'

if (resolvedOptions.shortVmodel) {
volarPlugins.push('@vue-macros/volar/short-vmodel')
volarOptions.shortVmodel = {
prefix: resolvedOptions.shortVmodel.prefix,
}
}

const viteVue = (nuxt.options.vite.vue ||= {})

if (resolvedOptions.setupSFC) {
Expand Down Expand Up @@ -142,25 +141,6 @@ export default defineNuxtModule<VueMacrosOptions>({
})
)
})

if (options.shortVmodel !== false || options.booleanProp) {
viteVue.template ||= {}
viteVue.template.compilerOptions ||= {}
viteVue.template.compilerOptions.nodeTransforms ||= []
const { nodeTransforms } = viteVue.template.compilerOptions

if (options.shortVmodel !== false) {
volarPlugins.push('@vue-macros/volar/short-vmodel')
nodeTransforms.push(transformShortVmodel(options.shortVmodel))
if (options.shortVmodel) {
volarOptions.shortVmodel = {
prefix: options.shortVmodel.prefix,
}
}
}

if (options.booleanProp) nodeTransforms.push(transformBooleanProp())
}
},
})

Expand Down
27 changes: 16 additions & 11 deletions packages/short-vmodel/src/index.ts
@@ -1,5 +1,4 @@
import { type Plugin } from 'rollup'
import { type Plugin as VitePlugin } from 'vite'
import { type Plugin } from 'vite'
import { type VuePluginApi, getVuePluginApi } from '@vue-macros/common'
import { type Options, transformShortVmodel } from './core/index'
import { generatePluginName } from '#macros' assert { type: 'macro' }
Expand All @@ -10,17 +9,23 @@ export * from './api'
const name = generatePluginName()

function rollup(options: Options = {}): Plugin {
let api: VuePluginApi

return {
name,
buildStart(rollupOpts) {
let api: VuePluginApi

configResolved(config) {
try {
api = getVuePluginApi(rollupOpts)
} catch (error: any) {
this.warn(error)
return
}
api = getVuePluginApi(config.plugins)
} catch {}
},
buildStart(rollupOpts) {
if (!api)
try {
api = getVuePluginApi(rollupOpts.plugins)
} catch (error: any) {
this.warn(error)
return
}

api.options.template ||= {}
api.options.template.compilerOptions ||= {}
Expand All @@ -35,5 +40,5 @@ function rollup(options: Options = {}): Plugin {

export default {
rollup,
vite: rollup as (options?: Options) => VitePlugin,
vite: rollup,
}
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c848deb

Please sign in to comment.