Repository: nuxt/nuxt
File: packages/vite/src/client.ts (TypeScript)

Code before:
  // In build mode we explicitly override any vite options that vite is relying on
  // to detect whether to inject production or development code (such as HMR code)
  if (!ctx.nuxt.options.dev) {
    clientConfig.server!.hmr = false
  }

  // Inject an h3-based CORS handler in preference to vite's
  const useViteCors = clientConfig.server?.cors !== undefined
  if (!useViteCors) {
    clientConfig.server!.cors = false
  }

  // We want to respect users' own rollup output options
  const fileNames = withoutLeadingSlash(join(ctx.nuxt.options.app.buildAssetsDir, '[hash].js'))
  clientConfig.build!.rollupOptions = defu(clientConfig.build!.rollupOptions!, {
    output: {
      chunkFileNames: ctx.nuxt.options.dev ? undefined : fileNames,
      entryFileNames: ctx.nuxt.options.dev ? 'entry.js' : fileNames,
      sourcemapPathTransform (relativeSourcePath, sourcemapPath) {
        // client build is running in a temporary build directory, like `.nuxt/dist/client`
        // so we need to transform the sourcemap path to be relative to the final build directory
        // which is normally `.output/public`
        if (relativeSourcePath.startsWith('..')) {
          const absoluteSourcePath = resolve(dirname(sourcemapPath), relativeSourcePath)
          const newRelativePath = relative(
            join(ctx.config.root!, '.output/public', ctx.nuxt.options.app.buildAssetsDir),
            absoluteSourcePath,
          )
          return newRelativePath
        }
        return relativeSourcePath
      },
    } satisfies NonNullable<BuildOptions['rollupOptions']>['output'],
  }) as any

  if (clientConfig.server && clientConfig.server.hmr !== false) {
    const serverDefaults: Omit<ServerOptions, 'hmr'> & { hmr: Exclude<ServerOptions['hmr'], boolean> } = {
      hmr: {
        protocol: ctx.nuxt.options.devServer.https ? 'wss' : undefined,
      },
    }
    if (typeof clientConfig.server.hmr !== 'object' || !clientConfig.server.hmr.server) {
      const hmrPortDefault = 24678 // Vite's default HMR port
      serverDefaults.hmr!.port = await getPort({
        port: hmrPortDefault,
        ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i),
      })
    }
    if (ctx.nuxt.options.devServer.https) {
      serverDefaults.https = ctx.nuxt.options.devServer.https === true ? {} : ctx.nuxt.options.devServer.https
    }

Review comment (suggestion):
we shouldn't hard-code `.output/public` as this will depend on the nitro preset used - we can get this from the resolved nitro options.

Code after:
  }

  // We want to respect users' own rollup output options
  const fileNames = withoutLeadingSlash(join(nuxt.options.app.buildAssetsDir, '[hash].js'))
  const clientOutputDir = join(useNitro().options.output.publicDir, nuxt.options.app.buildAssetsDir)
  clientConfig.build!.rollupOptions = defu(clientConfig.build!.rollupOptions!, {
    output: {
      chunkFileNames: nuxt.options.dev ? undefined : fileNames,
      entryFileNames: nuxt.options.dev ? 'entry.js' : fileNames,
      sourcemapPathTransform (relativeSourcePath, sourcemapPath) {
        // client build is running in a temporary build directory, like `.nuxt/dist/client`
        // so we need to transform the sourcemap path to be relative to the final build directory
        if (!isAbsolute(relativeSourcePath)) {
          const absoluteSourcePath = resolve(dirname(sourcemapPath), relativeSourcePath)
          return relative(clientOutputDir, absoluteSourcePath)
        }
        return relativeSourcePath
      },
    } satisfies NonNullable<BuildOptions['rollupOptions']>['output'],
  }) as any

  if (clientConfig.server && clientConfig.server.hmr !== false) {
    const serverDefaults: Omit<ServerOptions, 'hmr'> & { hmr: Exclude<ServerOptions['hmr'], boolean> } = {
      hmr: {
        protocol: nuxt.options.devServer.https ? 'wss' : undefined,
      },
    }
    if (typeof clientConfig.server.hmr !== 'object' || !clientConfig.server.hmr.server) {
      const hmrPortDefault = 24678 // Vite's default HMR port
      serverDefaults.hmr!.port = await getPort({
        port: hmrPortDefault,
        ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i),
      })
    }
    if (nuxt.options.devServer.https) {
      serverDefaults.https = nuxt.options.devServer.https === true ? {} : nuxt.options.devServer.https
    }
    clientConfig.server = defu(clientConfig.server, serverDefaults as ViteConfig['server'])
  }

  // Add analyze plugin if needed
  if (!nuxt.options.test && nuxt.options.build.analyze && (nuxt.options.build.analyze === true || nuxt.options.build.analyze.enabled)) {
    clientConfig.plugins!.push(...await import('./plugins/analyze').then(r => r.AnalyzePlugin(nuxt)))
  }

  await nuxt.callHook('vite:extendConfig', clientConfig, { isClient: true, isServer: false })

  clientConfig.plugins!.unshift(
    vuePlugin(clientConfig.vue),
    viteJsxPlugin(clientConfig.vueJsx),
  )