MCPcopy
hub / github.com/nuxt/nuxt / extractMetadata

Function extractMetadata

packages/nuxt/src/core/plugins/plugin-metadata.ts:42–79  ·  view source on GitHub ↗
(code: string, loader = 'ts' as 'ts' | 'tsx')

Source from the content-addressed store, hash-verified

40
41const metaCache: Record<string, Omit<PluginMeta, 'enforce'>> = {}
42export 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
81type PluginMetaKey = keyof PluginMeta
82const keys: Record<PluginMetaKey, string> = {

Callers 2

annotatePluginsFunction · 0.90

Calls 1

extractMetaFromObjectFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…