(code: string, loader = 'ts' as 'ts' | 'tsx')
| 40 | |
| 41 | const metaCache: Record<string, Omit<PluginMeta, 'enforce'>> = {} |
| 42 | export function extractMetadata (code: string, loader = 'ts' as 'ts' | 'tsx') { |
| 43 | let meta: PluginMeta = {} |
| 44 | if (metaCache[code]) { |
| 45 | return metaCache[code] |
| 46 | } |
| 47 | // non-object syntax plugin |
| 48 | if (/defineNuxtPlugin\s*\([\w(]/.test(code)) { |
| 49 | return {} |
| 50 | } |
| 51 | parseAndWalk(code, `file.${loader}`, (node) => { |
| 52 | if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier') { return } |
| 53 | |
| 54 | const name = 'name' in node.callee && node.callee.name |
| 55 | if (name !== 'defineNuxtPlugin' && name !== 'definePayloadPlugin') { return } |
| 56 | |
| 57 | if (name === 'definePayloadPlugin') { |
| 58 | meta.order = internalOrderMap['user-revivers'] |
| 59 | } |
| 60 | |
| 61 | const metaArg = node.arguments[1] |
| 62 | if (metaArg) { |
| 63 | if (metaArg.type !== 'ObjectExpression') { |
| 64 | throw new Error('Invalid plugin metadata') |
| 65 | } |
| 66 | meta = extractMetaFromObject(metaArg.properties) |
| 67 | } |
| 68 | |
| 69 | const plugin = node.arguments[0] |
| 70 | if (plugin?.type === 'ObjectExpression') { |
| 71 | meta = defu(extractMetaFromObject(plugin.properties), meta) |
| 72 | } |
| 73 | |
| 74 | meta.order ||= orderMap[meta.enforce || 'default'] || orderMap.default |
| 75 | delete meta.enforce |
| 76 | }) |
| 77 | metaCache[code] = meta |
| 78 | return meta as Omit<PluginMeta, 'enforce'> |
| 79 | } |
| 80 | |
| 81 | type PluginMetaKey = keyof PluginMeta |
| 82 | const keys: Record<PluginMetaKey, string> = { |
no test coverage detected
searching dependent graphs…