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

Function handler

packages/nuxt/src/components/plugins/islands-transform.ts:55–149  ·  view source on GitHub ↗
(code, id)

Source from the content-addressed store, hash-verified

53 },
54 },
55 async handler (code, id) {
56 const template = code.match(TEMPLATE_RE)
57 if (!template) { return }
58 const startingIndex = template.index || 0
59 const s = new MagicString(code)
60
61 const { pathname } = parseModuleId(normalize(id))
62 const isIsland = isIslandFile(pathname, options)
63
64 if (!SCRIPT_RE.test(code)) {
65 s.prepend('<script setup>' + IMPORT_CODE + '</script>')
66 } else {
67 s.replace(SCRIPT_RE_GLOBAL, (full) => {
68 return full + IMPORT_CODE
69 })
70 }
71
72 let hasNuxtClient = false
73
74 const ast = parse(template[0])
75 await walk(ast, (node) => {
76 if (node.type !== ELEMENT_NODE) {
77 return
78 }
79 if (node.name === 'slot') {
80 if (!isIsland) { return }
81 const { attributes, children, loc } = node
82
83 const slotName = attributes.name ?? 'default'
84
85 if (attributes.name) { delete attributes.name }
86 if (attributes['v-bind']) {
87 attributes._bind = extractAttributes(attributes, ['v-bind'])['v-bind']!
88 }
89 const teleportAttributes = extractAttributes(attributes, ['v-if', 'v-else-if', 'v-else'])
90 const bindings = getPropsToString(attributes)
91 // add the wrapper
92 s.appendLeft(startingIndex + loc[0].start, `<NuxtTeleportSsrSlot${attributeToString(teleportAttributes)} name="${slotName}" :props="${bindings}">`)
93
94 if (children.length) {
95 // pass slot fallback to NuxtTeleportSsrSlot fallback
96 const attrString = attributeToString(attributes)
97 const slice = code.slice(startingIndex + loc[0].end, startingIndex + loc[1].start).replaceAll(KEY_RE, '')
98 s.overwrite(startingIndex + loc[0].start, startingIndex + loc[1].end, `<slot${attrString.replaceAll(EXTRACTED_ATTRS_RE, '')}/><template #fallback>${attributes['v-for'] ? wrapWithVForDiv(slice, attributes['v-for']) : slice}</template>`)
99 } else {
100 s.overwrite(startingIndex + loc[0].start, startingIndex + loc[0].end, code.slice(startingIndex + loc[0].start, startingIndex + loc[0].end).replaceAll(EXTRACTED_ATTRS_RE, ''))
101 }
102
103 s.appendRight(startingIndex + loc[1].end, '</NuxtTeleportSsrSlot>')
104 return
105 }
106
107 if (node.name === 'NuxtTeleportIslandComponent') {
108 return
109 }
110
111 if (!('nuxt-client' in node.attributes) && !(':nuxt-client' in node.attributes)) {
112 return

Callers

nothing calls this directly

Calls 7

parseModuleIdFunction · 0.90
isIslandFileFunction · 0.85
extractAttributesFunction · 0.85
getPropsToStringFunction · 0.85
attributeToStringFunction · 0.85
wrapWithVForDivFunction · 0.85
toStringMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…