MCPcopy
hub / github.com/nuxt/nuxt / handler

Function handler

packages/nuxt/src/components/plugins/lazy-hydration-macro-transform.ts:54–110  ·  view source on GitHub ↗
(code, id)

Source from the content-addressed store, hash-verified

52 },
53 },
54 handler (code, id) {
55 const s = new MagicString(code)
56 const names = new Set<string>()
57 type Edit = { start: number, end: number, replacement: string }
58 const edits: Edit[] = []
59
60 parseAndWalk(code, id, (node, parent) => {
61 if (node.type !== 'CallExpression') { return }
62 if (node.callee?.type !== 'Identifier') { return }
63 if (node.callee.name !== 'defineLazyHydrationComponent') { return }
64
65 if (parent?.type !== 'VariableDeclarator') { return }
66 if (parent.id.type !== 'Identifier') { return }
67
68 if (node.arguments.length < 2) { return }
69 const [strategyArgument, loaderArgument] = node.arguments
70
71 if (!isStringLiteral(strategyArgument)) { return }
72 const strategy: string = strategyArgument.value
73
74 const functionName = HYDRATION_TO_FACTORY.get(strategy)
75 if (!functionName) { return }
76
77 if (loaderArgument?.type !== 'ArrowFunctionExpression') { return }
78
79 const { importExpression, importLiteral } = findImportExpression(loaderArgument.body)
80 if (!importExpression || !isStringLiteral(importLiteral)) { return }
81
82 const rawPath = importLiteral.value
83 const filePath = resolveAlias(rawPath, options.alias || {})
84 const relativePath = relative(options.srcDir, filePath)
85
86 const originalLoader = code.slice(loaderArgument.start, loaderArgument.end)
87 const replacement = `__${functionName}(${JSON.stringify(relativePath)}, ${originalLoader})`
88
89 edits.push({ start: node.start, end: node.end, replacement })
90 names.add(functionName)
91 })
92
93 for (const edit of edits) {
94 s.overwrite(edit.start, edit.end, edit.replacement)
95 }
96
97 if (names.size) {
98 const imports = genImport(options.clientDelayedComponentRuntime, [...names].map(name => ({ name, as: `__${name}` })))
99 s.prepend(imports)
100 }
101
102 if (s.hasChanged()) {
103 return {
104 code: s.toString(),
105 map: options.sourcemap
106 ? s.generateMap({ hires: true })
107 : undefined,
108 }
109 }
110 },
111 },

Callers

nothing calls this directly

Calls 5

isStringLiteralFunction · 0.85
findImportExpressionFunction · 0.85
resolveAliasFunction · 0.85
getMethod · 0.80
toStringMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…