mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-22 19:32:40 +00:00
chore: type issues
This commit is contained in:
parent
9da1105dec
commit
012950d7e5
@ -1,6 +1,6 @@
|
|||||||
import { createUnplugin } from 'unplugin'
|
import { createUnplugin } from 'unplugin'
|
||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
import type { ImportSpecifier } from 'estree'
|
import type { Identifier, ImportSpecifier } from 'estree'
|
||||||
import { relative } from 'pathe'
|
import { relative } from 'pathe'
|
||||||
import { unheadVueComposablesImports } from '@unhead/vue'
|
import { unheadVueComposablesImports } from '@unhead/vue'
|
||||||
import { parseAndWalk, withLocations } from '../../core/utils/parse'
|
import { parseAndWalk, withLocations } from '../../core/utils/parse'
|
||||||
@ -17,11 +17,14 @@ const UNHEAD_LIB_RE = /node_modules\/(?:@unhead\/[^/]+|unhead)\//
|
|||||||
|
|
||||||
function toImports (specifiers: ImportSpecifier[]) {
|
function toImports (specifiers: ImportSpecifier[]) {
|
||||||
return specifiers.map((specifier) => {
|
return specifiers.map((specifier) => {
|
||||||
const isNamedImport = specifier.imported && specifier.imported.name !== specifier.local.name
|
const imported = specifier.imported as Identifier | null
|
||||||
return isNamedImport ? `${specifier.imported.name} as ${specifier.local.name}` : specifier.local.name
|
const isNamedImport = imported && imported.name !== specifier.local.name
|
||||||
|
return isNamedImport ? `${imported.name} as ${specifier.local.name}` : specifier.local.name
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const UnheadVue = '@unhead/vue'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To use composable in an async context we need to pass Nuxt context to the Unhead composables.
|
* To use composable in an async context we need to pass Nuxt context to the Unhead composables.
|
||||||
*
|
*
|
||||||
@ -35,7 +38,7 @@ export const UnheadImportsPlugin = (options: UnheadImportsPluginOptions) => crea
|
|||||||
return (isJS(id) || isVue(id, { type: ['script'] })) && !id.startsWith(distDir) && !UNHEAD_LIB_RE.test(id)
|
return (isJS(id) || isVue(id, { type: ['script'] })) && !id.startsWith(distDir) && !UNHEAD_LIB_RE.test(id)
|
||||||
},
|
},
|
||||||
transform (code, id) {
|
transform (code, id) {
|
||||||
if (!code.includes('@unhead/vue')) {
|
if (!code.includes(UnheadVue)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const s = new MagicString(code)
|
const s = new MagicString(code)
|
||||||
@ -43,25 +46,25 @@ export const UnheadImportsPlugin = (options: UnheadImportsPluginOptions) => crea
|
|||||||
// Without setup function, vue compiler does not generate __name
|
// Without setup function, vue compiler does not generate __name
|
||||||
parseAndWalk(code, id, function (node) {
|
parseAndWalk(code, id, function (node) {
|
||||||
// find any imports from @unhead/vue, swap the matchImports for an import from #app/composables/head
|
// find any imports from @unhead/vue, swap the matchImports for an import from #app/composables/head
|
||||||
if (node.type === 'ImportDeclaration' && ['@unhead/vue', '#app/composables/head'].includes(node.source.value)) {
|
if (node.type === 'ImportDeclaration' && [UnheadVue, '#app/composables/head'].includes(String(node.source.value))) {
|
||||||
importsToAdd.push(...node.specifiers as ImportSpecifier[])
|
importsToAdd.push(...node.specifiers as ImportSpecifier[])
|
||||||
const { start, end } = withLocations(node)
|
const { start, end } = withLocations(node)
|
||||||
s.remove(start, end)
|
s.remove(start, end)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const importsFromUnhead = importsToAdd.filter(specifier => unheadVueComposablesImports['@unhead/vue'].includes(specifier.imported.name))
|
const importsFromUnhead = importsToAdd.filter(specifier => unheadVueComposablesImports[UnheadVue].includes((specifier.imported as Identifier)?.name))
|
||||||
const importsFromHead = importsToAdd.filter(specifier => !unheadVueComposablesImports['@unhead/vue'].includes(specifier.imported.name))
|
const importsFromHead = importsToAdd.filter(specifier => !unheadVueComposablesImports[UnheadVue].includes((specifier.imported as Identifier)?.name))
|
||||||
if (importsFromUnhead.length) {
|
if (importsFromUnhead.length) {
|
||||||
// warn if user has imported from @unhead/vue themselves
|
// warn if user has imported from @unhead/vue themselves
|
||||||
if (!id.includes('node_modules')) {
|
if (!id.includes('node_modules')) {
|
||||||
logger.warn(`You are importing from \`@unhead/vue\` in \`./${relative(options.rootDir, id)}\`. Please import from \`#app\` instead for full type safety.`)
|
logger.warn(`You are importing from \`${UnheadVue}\` in \`./${relative(options.rootDir, id)}\`. Please import from \`#app\` instead for full type safety.`)
|
||||||
}
|
}
|
||||||
s.prepend(`import { ${toImports(importsFromUnhead).join(', ')} } from '#app/composables/head'\n`)
|
s.prepend(`import { ${toImports(importsFromUnhead).join(', ')} } from '#app/composables/head'\n`)
|
||||||
}
|
}
|
||||||
// if there are imports from #app/composables/head, add an import from @unhead/vue
|
// if there are imports from #app/composables/head, add an import from @unhead/vue
|
||||||
if (importsFromHead.length) {
|
if (importsFromHead.length) {
|
||||||
s.prepend(`import { ${toImports(importsFromHead).join(', ')} } from '@unhead/vue'\n`)
|
s.prepend(`import { ${toImports(importsFromHead).join(', ')} } from ${JSON.stringify(UnheadVue)}'\n`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s.hasChanged()) {
|
if (s.hasChanged()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user