fix(nuxt): use import alias when checking if plugin is wrapped (#23617)

This commit is contained in:
Aaron_Zhou 2023-10-12 00:01:23 +08:00 committed by GitHub
parent f3810b132a
commit b3d3d7f4fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -132,6 +132,7 @@ export const RemovePluginMetadataPlugin = (nuxt: Nuxt) => createUnplugin(() => {
} }
let wrapped = false let wrapped = false
const wrapperNames = new Set(['defineNuxtPlugin', 'definePayloadPlugin'])
try { try {
walk(this.parse(code, { walk(this.parse(code, {
@ -139,6 +140,9 @@ export const RemovePluginMetadataPlugin = (nuxt: Nuxt) => createUnplugin(() => {
ecmaVersion: 'latest' ecmaVersion: 'latest'
}) as Node, { }) as Node, {
enter (_node) { enter (_node) {
if (_node.type === 'ImportSpecifier' && (_node.imported.name === 'defineNuxtPlugin' || _node.imported.name === 'definePayloadPlugin')) {
wrapperNames.add(_node.local.name)
}
if (_node.type === 'ExportDefaultDeclaration' && (_node.declaration.type === 'FunctionDeclaration' || _node.declaration.type === 'ArrowFunctionExpression')) { if (_node.type === 'ExportDefaultDeclaration' && (_node.declaration.type === 'FunctionDeclaration' || _node.declaration.type === 'ArrowFunctionExpression')) {
if ('params' in _node.declaration && _node.declaration.params.length > 1) { if ('params' in _node.declaration && _node.declaration.params.length > 1) {
logger.warn(`Plugin \`${plugin.src}\` is in legacy Nuxt 2 format (context, inject) which is likely to be broken and will be ignored.`) logger.warn(`Plugin \`${plugin.src}\` is in legacy Nuxt 2 format (context, inject) which is likely to be broken and will be ignored.`)
@ -150,7 +154,7 @@ export const RemovePluginMetadataPlugin = (nuxt: Nuxt) => createUnplugin(() => {
if (_node.type !== 'CallExpression' || (_node as CallExpression).callee.type !== 'Identifier') { return } if (_node.type !== 'CallExpression' || (_node as CallExpression).callee.type !== 'Identifier') { return }
const node = _node as CallExpression & { start: number, end: number } const node = _node as CallExpression & { start: number, end: number }
const name = 'name' in node.callee && node.callee.name const name = 'name' in node.callee && node.callee.name
if (name !== 'defineNuxtPlugin' && name !== 'definePayloadPlugin') { return } if (!name || !wrapperNames.has(name)) { return }
wrapped = true wrapped = true
if (node.arguments[0].type !== 'ObjectExpression') { if (node.arguments[0].type !== 'ObjectExpression') {