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

Code before:
        vite: {
          buildStart () {
            const components = options.getComponents().filter(c => c.mode === 'client' || c.mode === 'all')
            for (const component of components) {
              if (component.filePath) {
                if (isDev) {
                  ids.set(component.pascalName, `@fs/${component.filePath}`)
                } else {
                  const id = this.emitFile({
                    type: 'chunk',
                    fileName: '_nuxt/' + hash(component.filePath) + '.mjs',
                    id: component.filePath,
                    preserveSignature: 'strict',

                  })

                  ids.set(component.pascalName, this.getFileName(id))
                }
              }
            }
          },
          generateBundle (_, bundle) {
            for (const chunk of Object.values(bundle)) {
              if (chunk.type === 'chunk') {
                const list = Array.from(ids.values())
                if (list.includes(chunk.fileName)) {
                  chunk.isEntry = false
                }
              }
            }
          },
        },
      }
    }),
    server: createUnplugin(() => {
      return {
        name: 'nuxt:components-chunk:server',
        buildStart () {
          writeFileSync(
            join(useNuxt().options.buildDir, 'component-chunk.mjs'),
            `export default {${Array.from(ids.entries()).map(([name, id]) => {
              return `${JSON.stringify(name)}: ${JSON.stringify('/' + id)}`
            }).join(',\n')}}`,
          )
        },
      }
    }),
  }
}


Review comment (performance):
To improve performance, consider converting the ids collection into a Set outside the loop to allow O(1) lookup instead of converting it to an array for each iteration.
```suggestion
            const idSet = new Set(ids.values())
            for (const chunk of Object.values(bundle)) {
              if (chunk.type === 'chunk') {
                if (idSet.has(chunk.fileName)) {
```

Code after:
        vite: {
          buildStart () {
            const components = options.getComponents().filter(c => c.mode === 'client' || c.mode === 'all')
            for (const component of components) {
              if (component.filePath) {
                if (isDev) {
                  ids.set(component.pascalName, `@fs/${component.filePath}`)
                } else {
                  const id = this.emitFile({
                    type: 'chunk',
                    fileName: '_nuxt/' + hash(component.filePath) + '.mjs',
                    id: component.filePath,
                    preserveSignature: 'strict',

                  })

                  ids.set(component.pascalName, this.getFileName(id))
                }
              }
            }
          },
          generateBundle (_, bundle) {
            const idSet = new Set(ids.values())
            for (const chunk of Object.values(bundle)) {
              if (chunk.type === 'chunk') {
                if (idSet.has(chunk.fileName)) {
                  chunk.isEntry = false
                }
              }
            }
          },
        },
      }
    }),
    server: createUnplugin(() => {
      return {
        name: 'nuxt:components-chunk:server',
        buildStart () {
          writeFileSync(
            join(useNuxt().options.buildDir, 'component-chunk.mjs'),
            `export default {${Array.from(ids.entries()).map(([name, id]) => {
              return `${JSON.stringify(name)}: ${JSON.stringify('/' + id)}`
            }).join(',\n')}}`,
          )
        },
      }
    }),
  }
}