next.js/packages/next/src/build/webpack/plugins/build-manifest-plugin-utils.ts
build-manifest-plugin-utils.ts59 lines1.7 KB
import type { CustomRoutes, Rewrite } from '../../../lib/load-custom-routes'
import type { BuildManifest } from '../../../server/get-page-files'

export type ClientBuildManifest = {
  [key: string]: string[]
}

// Add the runtime ssg manifest file as a lazy-loaded file dependency.
// We also stub this file out for development mode (when it is not
// generated).
export const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`

function normalizeRewrite(item: {
  source: string
  destination: string
  has?: any
}): CustomRoutes['rewrites']['beforeFiles'][0] {
  return {
    has: item.has,
    source: item.source,
    destination: item.destination,
  }
}

export const processRoute = (r: Rewrite) => {
  const rewrite = { ...r }

  // omit external rewrite destinations since these aren't
  // handled client-side
  if (!rewrite?.destination?.startsWith('/')) {
    delete (rewrite as any).destination
  }
  return rewrite
}

export function normalizeRewritesForBuildManifest(
  rewrites: CustomRoutes['rewrites']
): CustomRoutes['rewrites'] {
  return {
    afterFiles: rewrites.afterFiles
      ?.map(processRoute)
      ?.map((item) => normalizeRewrite(item)),
    beforeFiles: rewrites.beforeFiles
      ?.map(processRoute)
      ?.map((item) => normalizeRewrite(item)),
    fallback: rewrites.fallback
      ?.map(processRoute)
      ?.map((item) => normalizeRewrite(item)),
  }
}

export function createEdgeRuntimeManifest(
  assetMap: Partial<BuildManifest>
): string {
  // we use globalThis here because middleware can be node
  // which doesn't have "self"
  return `globalThis.__BUILD_MANIFEST = ${JSON.stringify(assetMap, null, 2)};\n`
}
Quest for Codev2.0.0
/
SIGN IN