next.js/packages/next/src/build/turbopack-analyze/index.ts
index.ts131 lines4.1 KB
import type { NextConfigComplete } from '../../server/config-shared'
import type { __ApiPreviewProps } from '../../server/api-utils'

import path from 'path'
import { validateTurboNextConfig } from '../../lib/turbopack-warning'
import { createDefineEnv, loadBindings } from '../swc'
import { isCI } from '../../server/ci-info'
import { backgroundLogCompilationEvents } from '../../shared/lib/turbopack/compilation-events'
import { getSupportedBrowsers } from '../get-supported-browsers'
import { trace } from '../../trace'
import { normalizePath } from '../../lib/normalize-path'
import { PHASE_PRODUCTION_BUILD } from '../../shared/lib/constants'

export type AnalyzeContext = {
  config: NextConfigComplete
  distDir: string
  dir: string
  noMangling: boolean
  appDirOnly: boolean
}

export async function turbopackAnalyze(
  analyzeContext: AnalyzeContext
): Promise<{
  duration: number
  shutdownPromise: Promise<void>
}> {
  await validateTurboNextConfig({
    dir: analyzeContext.dir,
    configPhase: PHASE_PRODUCTION_BUILD,
  })

  const { config, dir, distDir, noMangling } = analyzeContext
  const currentNodeJsVersion = process.versions.node

  const startTime = process.hrtime()
  const bindings = await loadBindings(config?.experimental?.useWasmBinary)

  if (bindings.isWasm) {
    throw new Error(
      `Turbopack analyze is not supported on this platform (${process.platform}/${process.arch}) because native bindings are not available. ` +
        `Only WebAssembly (WASM) bindings were loaded, and Turbopack requires native bindings.\n\n` +
        `For more information, see: https://nextjs.org/docs/app/api-reference/turbopack#supported-platforms`
    )
  }

  const dev = false

  const supportedBrowsers = getSupportedBrowsers(dir, dev)

  const persistentCaching =
    config.experimental?.turbopackFileSystemCacheForBuild || false
  const rootPath = config.turbopack?.root || config.outputFileTracingRoot || dir
  const project = await bindings.turbo.createProject(
    {
      rootPath: config.turbopack?.root || config.outputFileTracingRoot || dir,
      projectPath: normalizePath(path.relative(rootPath, dir) || '.'),
      distDir,
      nextConfig: config,
      watch: {
        enable: false,
      },
      dev,
      env: process.env as Record<string, string>,
      defineEnv: createDefineEnv({
        isTurbopack: true,
        config,
        dev,
        distDir,
        projectPath: dir,
        fetchCacheKeyPrefix: config.experimental.fetchCacheKeyPrefix,
        hasRewrites: false,
        // Implemented separately in Turbopack, doesn't have to be passed here.
        middlewareMatchers: undefined,
        rewrites: {
          beforeFiles: [],
          afterFiles: [],
          fallback: [],
        },
      }),
      buildId: 'analyze-build',
      encryptionKey: '',
      previewProps: {
        previewModeId: '',
        previewModeEncryptionKey: '',
        previewModeSigningKey: '',
      },
      browserslistQuery: supportedBrowsers.join(', '),
      noMangling,
      writeRoutesHashesManifest: false,
      currentNodeJsVersion,
      isPersistentCachingEnabled: persistentCaching,
      nextVersion: process.env.__NEXT_VERSION as string,
    },
    {
      memoryLimit: config.experimental?.turbopackMemoryLimit,
      dependencyTracking: persistentCaching,
      isCi: isCI,
      isShortSession: true,
    }
  )

  try {
    const analyzeEventsSpan = trace('turbopack-analyze-events')
    // Stop immediately: this span is only used as a parent for
    // manualTraceChild calls which carry their own timestamps.
    analyzeEventsSpan.stop()
    backgroundLogCompilationEvents(project, { parentSpan: analyzeEventsSpan })

    await project.writeAnalyzeData(analyzeContext.appDirOnly)

    const shutdownPromise = project.shutdown()

    const time = process.hrtime(startTime)
    return {
      duration: time[0] + time[1] / 1e9,
      shutdownPromise,
    }
  } catch (err) {
    await project.shutdown()
    throw err
  }
}

let shutdownPromise: Promise<void> | undefined
export async function waitForShutdown(): Promise<void> {
  if (shutdownPromise) {
    await shutdownPromise
  }
}
Quest for Codev2.0.0
/
SIGN IN