mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-19 01:45:53 +00:00
fix(nuxt): auto import for components with external template (#6053)
This commit is contained in:
parent
82dab419a4
commit
416f98b6b4
@ -11,7 +11,7 @@ export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: {ctx
|
|||||||
enforce: 'post',
|
enforce: 'post',
|
||||||
transformInclude (id) {
|
transformInclude (id) {
|
||||||
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
||||||
const { type, macro } = parseQuery(search)
|
const query = parseQuery(search)
|
||||||
|
|
||||||
// Included
|
// Included
|
||||||
if (options.transform?.include?.some(pattern => id.match(pattern))) {
|
if (options.transform?.include?.some(pattern => id.match(pattern))) {
|
||||||
@ -24,8 +24,9 @@ export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: {ctx
|
|||||||
|
|
||||||
// Vue files
|
// Vue files
|
||||||
if (
|
if (
|
||||||
pathname.endsWith('.vue') &&
|
id.endsWith('.vue') ||
|
||||||
(type === 'template' || type === 'script' || macro || !search)
|
'macro' in query ||
|
||||||
|
('vue' in query && (query.type === 'template' || query.type === 'script' || 'setup' in query))
|
||||||
) {
|
) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,33 @@ interface LoaderOptions {
|
|||||||
transform?: ComponentsOptions['transform']
|
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) => {
|
export const loaderPlugin = createUnplugin((options: LoaderOptions) => {
|
||||||
const exclude = options.transform?.exclude || []
|
const exclude = options.transform?.exclude || []
|
||||||
const include = options.transform?.include || []
|
const include = options.transform?.include || []
|
||||||
@ -27,12 +54,7 @@ export const loaderPlugin = createUnplugin((options: LoaderOptions) => {
|
|||||||
if (include.some(pattern => id.match(pattern))) {
|
if (include.some(pattern => id.match(pattern))) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
return isVueTemplate(id)
|
||||||
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)
|
|
||||||
},
|
},
|
||||||
transform (code, id) {
|
transform (code, id) {
|
||||||
const components = options.getComponents()
|
const components = options.getComponents()
|
||||||
|
Loading…
Reference in New Issue
Block a user