Repository: nuxt/nuxt
File: packages/nuxt/src/components/plugins/transform.ts (TypeScript)

Code before:
                `${exportWording} createServerComponent(${JSON.stringify(name)})`,
              ].join('\n'),
              map: null,
            }
          } else {
            throw new Error(`Unknown component mode: ${mode}, this might be an internal bug of Nuxt.`)
          }
        },
      },
    },
    {
      name: 'nuxt:components:imports-alias',
      enforce: 'post',
      transformInclude (id) {
        id = normalize(id)
        return id.startsWith('virtual:') || id.startsWith('\0virtual:') || id.startsWith(nuxt.options.buildDir) || !isIgnored(id, undefined, nuxt)
      },
      transform: {
        filter: {
          code: /#components/,
        },
        async handler (code, id) {
          // If package defines a "#components" import mapping, assume is used internally by the package.
          if (isAbsolute(id) && (/node_modules[\\/](?!\.virtual)/.test(id) || relative(nuxt.options.rootDir, id).startsWith('..'))) {
            let pkg
            try { pkg = await readPackage(id) } catch { /* package.json not found */ }
            if (isObject(pkg) && isObject(pkg.imports) && Object.hasOwn(pkg.imports, '#components')) {
              return
            }
          }

          componentUnimport.modifyDynamicImports((imports) => {
            imports.length = 0
            imports.push(...getComponentsImports())
            return imports
          })

          const result = await componentUnimport.injectImports(code, id, { autoImport: false, transformVirtualImports: true })
          if (!result) { return }

          return {
            code: result.code,
            map: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client
              ? result.s.generateMap({ hires: true })
              : undefined,
          }
        },
      },
    },
  ])
}

Review comment (suggestion):
I think `try: true` (as it was before) should do the same thing:

```suggestion
            const pkg = await readPackage(id, { try: true })
```

Code after:
              code: [
                `import { createServerComponent } from ${JSON.stringify(options.serverComponentRuntime)}`,
                `${exportWording} createServerComponent(${JSON.stringify(name)})`,
              ].join('\n'),
              map: null,
            }
          } else {
            throw new Error(`Unknown component mode: ${mode}, this might be an internal bug of Nuxt.`)
          }
        },
      },
    },
    {
      name: 'nuxt:components:imports-alias',
      enforce: 'post',
      transformInclude (id) {
        id = normalize(id)
        return id.startsWith('virtual:') || id.startsWith('\0virtual:') || id.startsWith(nuxt.options.buildDir) || !isIgnored(id, undefined, nuxt)
      },
      transform: {
        filter: {
          code: /#components/,
        },
        async handler (code, id) {
          // If package defines a "#components" import mapping, assume is used internally by the package.
          if (isAbsolute(id) && (/node_modules[\\/](?!\.virtual)/.test(id) || !id.includes(rootDirWithSlash))) {
            let pkg: PackageJson | undefined
            try { pkg = await readPackage(id) } catch { /* ignore */ }
            if (isObject(pkg) && isObject(pkg.imports) && Object.keys(pkg.imports).some(k => k.includes('#components'))) {
              return
            }
          }

          componentUnimport.modifyDynamicImports((imports) => {
            imports.length = 0
            imports.push(...getComponentsImports())
            return imports
          })

          const result = await componentUnimport.injectImports(code, id, { autoImport: false, transformVirtualImports: true })
          if (!result) { return }

          return {
            code: result.code,
            map: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client
              ? result.s.generateMap({ hires: true })
              : undefined,
          }
        },
      },
    },