Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

fix(nuxt): auto import for components with external template #6053

Merged
merged 4 commits into from
Jul 22, 2022
Merged
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
7 changes: 4 additions & 3 deletions packages/nuxt/src/auto-imports/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: {ctx
enforce: 'post',
transformInclude (id) {
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
const { type, macro } = parseQuery(search)
const query = parseQuery(search)

// Included
if (options.transform?.include?.some(pattern => id.match(pattern))) {
Expand All @@ -24,8 +24,9 @@ export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: {ctx

// Vue files
if (
pathname.endsWith('.vue') &&
(type === 'template' || type === 'script' || macro || !search)
id.endsWith('.vue') ||
'macro' in query ||
('vue' in query && (query.type === 'template' || query.type === 'script' || 'setup' in query))
) {
return true
}
Expand Down
34 changes: 28 additions & 6 deletions packages/nuxt/src/components/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ interface LoaderOptions {
transform?: ComponentsOptions['transform']
}

function isVueTemplate (id: string) {
// Bare `.vue` file (in Vite)
if (id.endsWith('.vue')) {
return true
}

const { search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
if (!search) {
return false
}

const query = parseQuery(search)

// Macro
if (query.macro) {
return true
}

// Non-Vue or Styles
if (!('vue' in query) || query.type === 'style') {
return false
}

// Query `?vue&type=template` (in Webpack or external template)
return true
}

export const loaderPlugin = createUnplugin((options: LoaderOptions) => {
const exclude = options.transform?.exclude || []
const include = options.transform?.include || []
Expand All @@ -27,12 +54,7 @@ export const loaderPlugin = createUnplugin((options: LoaderOptions) => {
if (include.some(pattern => id.match(pattern))) {
return true
}

const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
const query = parseQuery(search)
// we only transform render functions
// from `type=template` (in Webpack), bare `.vue` file and setup=true (Vite 2/3)
return pathname.endsWith('.vue') && (query.type === 'template' || !!query.macro || !!query.setup || !search)
return isVueTemplate(id)
},
transform (code, id) {
const components = options.getComponents()
Expand Down