MCPcopy
hub / github.com/nuxt/nuxt / transform

Function transform

packages/nuxt/src/components/plugins/loader.ts:50–176  ·  view source on GitHub ↗
(code, id)

Source from the content-addressed store, hash-verified

48 return isVue(id, { type: ['template', 'script'] }) || !!id.match(SX_RE)
49 },
50 transform (code, id) {
51 const components = options.getComponents()
52
53 let num = 0
54 const imports = new Set<string>()
55 const map = new Map<Component, string>()
56 const s = new MagicString(code)
57 // replace `_resolveComponent("...")` to direct import
58 s.replace(REPLACE_COMPONENT_TO_DIRECT_IMPORT_RE, (full: string, ...args) => {
59 const groups = args.pop()
60 const lazy = groups.hLazy || groups.lazy
61 const modifier = groups.hModifier || groups.modifier
62 const name = groups.hName || groups.name
63 const normalComponent = findComponent(components, name, options.mode)
64 const modifierComponent = !normalComponent && modifier ? findComponent(components, modifier + name, options.mode) : null
65 const component = normalComponent || modifierComponent
66
67 if (component) {
68 // TODO: refactor to @nuxt/cli
69 const internalInstall = ((component as any)._internal_install) as string
70 if (internalInstall && nuxt?.options.test === false) {
71 if (!nuxt.options.dev) {
72 throw new Error(`[nuxt] \`${resolveToAlias(id, nuxt)}\` is using \`${component.pascalName}\` which requires \`${internalInstall}\``)
73 }
74 installNuxtModule(internalInstall)
75 }
76 let identifier = map.get(component) || `__nuxt_component_${num++}`
77 map.set(component, identifier)
78
79 const isServerOnly = !component._raw && component.mode === 'server' &&
80 !components.some(c => c.pascalName === component.pascalName && c.mode === 'client')
81 if (isServerOnly) {
82 imports.add(genImport(options.serverComponentRuntime, [{ name: 'createServerComponent' }]))
83 imports.add(`const ${identifier} = createServerComponent(${JSON.stringify(component.pascalName)})`)
84 if (!options.experimentalComponentIslands) {
85 logger.warn(`Standalone server components (\`${name}\`) are not yet supported without enabling \`experimental.componentIslands\`.`)
86 }
87 return identifier
88 }
89
90 const isClientOnly = !component._raw && component.mode === 'client'
91 if (isClientOnly) {
92 imports.add(genImport('#app/components/client-only', [{ name: 'createClientOnly' }]))
93 identifier += '_client'
94 }
95
96 if (lazy) {
97 const dynamicImport = `${genDynamicImport(component.filePath, { interopDefault: false })}.then(c => c.${component.export ?? 'default'} || c)`
98 if (modifier && normalComponent) {
99 const relativePath = relative(options.srcDir, component.filePath)
100 switch (modifier) {
101 case 'Visible':
102 case 'visible-':
103 imports.add(genImport(options.clientDelayedComponentRuntime, [{ name: 'createLazyVisibleComponent' }]))
104 identifier += '_lazy_visible'
105 imports.add(`const ${identifier} = createLazyVisibleComponent(${JSON.stringify(relativePath)}, ${dynamicImport})`)
106 break
107 case 'Interaction':

Callers

nothing calls this directly

Calls 7

resolveToAliasFunction · 0.90
installNuxtModuleFunction · 0.90
findComponentFunction · 0.85
getMethod · 0.80
setMethod · 0.80
toStringMethod · 0.80
getComponentsMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…