diff --git a/.changeset/silent-impalas-call.md b/.changeset/silent-impalas-call.md new file mode 100644 index 000000000..4fab13bb5 --- /dev/null +++ b/.changeset/silent-impalas-call.md @@ -0,0 +1,7 @@ +--- +'@vue-macros/setup-sfc': minor +'@vue-macros/common': minor +'@vue-macros/nuxt': minor +--- + +improve nuxt support of setupSFC diff --git a/packages/common/package.json b/packages/common/package.json index e684177f7..a323d20d7 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -60,7 +60,7 @@ "@babel/types": "^7.22.5", "@rollup/pluginutils": "^5.0.2", "@vue/compiler-sfc": "^3.3.4", - "ast-kit": "^0.9.4", + "ast-kit": "^0.9.5", "local-pkg": "^0.4.3", "magic-string-ast": "^0.3.0" }, diff --git a/packages/common/src/constants.ts b/packages/common/src/constants.ts index 74a519fa3..dd4a35212 100644 --- a/packages/common/src/constants.ts +++ b/packages/common/src/constants.ts @@ -15,7 +15,10 @@ export const DEFINE_EMIT = 'defineEmit' export const REPO_ISSUE_URL = 'https://github.com/vue-macros/vue-macros/issues' export const REGEX_SRC_FILE = /\.[cm]?[jt]sx?$/ + export const REGEX_SETUP_SFC = /\.setup\.[cm]?[jt]sx?$/ +export const REGEX_SETUP_SFC_SUB = /\.setup\.[cm]?[jt]sx?((?!vue&).)*$/ + export const REGEX_VUE_SFC = /\.vue$/ export const REGEX_VUE_SUB = /\.vue\?vue&type=script/ export const REGEX_NODE_MODULES = /node_modules/ diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index 2a6c0aa1e..7418d6901 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -54,6 +54,7 @@ "dependencies": { "@nuxt/kit": "^3.6.5", "@vue-macros/boolean-prop": "workspace:*", + "@vue-macros/common": "workspace:~", "@vue-macros/short-vmodel": "workspace:*", "@vue-macros/volar": "workspace:*", "unplugin-vue-macros": "workspace:*" diff --git a/packages/nuxt/src/index.ts b/packages/nuxt/src/index.ts index a3253c970..b7d14731e 100644 --- a/packages/nuxt/src/index.ts +++ b/packages/nuxt/src/index.ts @@ -9,6 +9,7 @@ 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 @@ -103,17 +104,38 @@ export default defineNuxtModule({ vueCompilerOptions.experimentalDefinePropProposal = resolvedOptions.defineProp.edition || 'kevinEdition' - nuxt.options.vite.vue ||= {} - nuxt.options.vite.vue.include ||= [/\.vue$/] - if (!Array.isArray(nuxt.options.vite.vue.include)) - nuxt.options.vite.vue.include = [nuxt.options.vite.vue.include] - nuxt.options.vite.vue.include.push(/\.setup\.[cm]?[jt]sx?$/) + const viteVue = (nuxt.options.vite.vue ||= {}) + + if (resolvedOptions.setupSFC) { + viteVue.include ||= [/\.vue$/] + if (!Array.isArray(viteVue.include)) viteVue.include = [viteVue.include] + viteVue.include.push(REGEX_SETUP_SFC) + + nuxt.hook('components:extend', (components) => { + for (const component of components) { + component.pascalName = component.pascalName.replace(/Setup$/, '') + component.kebabName = component.kebabName.replace(/-setup$/, '') + } + }) + + nuxt.hook('pages:extend', (pages) => { + for (const page of pages) { + if (!page.file || !REGEX_SETUP_SFC.test(page.file)) continue + + if (page.name) page.name = page.name.replace(/\.setup$/, '') + if (page.path) + page.path = page.path + .replace(/\/index\.setup$/, '/') + .replace(/\.setup$/, '') + } + }) + } if (options.shortVmodel !== false || options.booleanProp) { - nuxt.options.vite.vue.template ||= {} - nuxt.options.vite.vue.template.compilerOptions ||= {} - nuxt.options.vite.vue.template.compilerOptions.nodeTransforms ||= [] - const { nodeTransforms } = nuxt.options.vite.vue.template.compilerOptions + 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') diff --git a/packages/setup-sfc/src/index.ts b/packages/setup-sfc/src/index.ts index 9fad803bf..71513859d 100644 --- a/packages/setup-sfc/src/index.ts +++ b/packages/setup-sfc/src/index.ts @@ -2,7 +2,7 @@ import { createUnplugin } from 'unplugin' import { type BaseOptions, type MarkRequired, - REGEX_SETUP_SFC, + REGEX_SETUP_SFC_SUB, createFilter, detectVueVersion, } from '@vue-macros/common' @@ -14,7 +14,7 @@ export type OptionsResolved = MarkRequired function resolveOption(options: Options): OptionsResolved { const version = options.version || detectVueVersion() return { - include: [REGEX_SETUP_SFC], + include: [REGEX_SETUP_SFC_SUB], ...options, version, } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 110958486..d7a0b76be 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -196,8 +196,8 @@ importers: specifier: ^3.3.4 version: 3.3.4 ast-kit: - specifier: ^0.9.4 - version: 0.9.4(rollup@3.27.2) + specifier: ^0.9.5 + version: 0.9.5 local-pkg: specifier: ^0.4.3 version: 0.4.3 @@ -531,6 +531,9 @@ importers: '@vue-macros/boolean-prop': specifier: workspace:* version: link:../boolean-prop + '@vue-macros/common': + specifier: workspace:~ + version: link:../common '@vue-macros/short-vmodel': specifier: workspace:* version: link:../short-vmodel @@ -4733,6 +4736,17 @@ packages: - rollup dev: false + /ast-kit@0.9.5: + resolution: {integrity: sha512-kbL7ERlqjXubdDd+szuwdlQ1xUxEz9mCz1+m07ftNVStgwRb2RWw+U6oKo08PAvOishMxiqz1mlJyLl8yQx2Qg==} + engines: {node: '>=16.14.0'} + dependencies: + '@babel/parser': 7.22.7 + '@rollup/pluginutils': 5.0.2(rollup@3.27.2) + pathe: 1.1.1 + transitivePeerDependencies: + - rollup + dev: false + /ast-types@0.16.1: resolution: {integrity: sha512-6t10qk83GOG8p0vKmaCr8eiilZwO171AvbROMtvvNiwrTly62t+7XkA8RdIIVbpMhCASAsxgAzdRSwh6nw/5Dg==} engines: {node: '>=4'}