Skip to content

Commit

Permalink
Update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jul 18, 2023
1 parent 20fdc6a commit 0379b34
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 58 deletions.
13 changes: 11 additions & 2 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ import { createClientRouterFilter } from '../lib/create-client-router-filter'
import { createValidFileMatcher } from '../server/lib/find-page-file'
import { startTypeChecking } from './type-check'
import { generateInterceptionRoutesRewrites } from '../lib/generate-interception-routes-rewrites'
import { baseOverrides, experimentalOverrides } from '../server/require-hook'
import { buildDataRoute } from '../shared/lib/router/utils/sorted-routes'
import { buildDataRoute } from '../server/lib/router-utils/build-data-route'
import {
baseOverrides,
defaultOverrides,
experimentalOverrides,
} from '../server/require-hook'

export type SsgRoute = {
initialRevalidateSeconds: number | false
Expand Down Expand Up @@ -1979,6 +1983,11 @@ export default async function build(
...Object.values(experimentalOverrides).map((override) =>
require.resolve(override)
),
...(config.experimental.turbotrace
? []
: Object.values(defaultOverrides).map((value) =>
require.resolve(value)
)),
]

// ensure we trace any dependencies needed for custom
Expand Down
48 changes: 48 additions & 0 deletions packages/next/src/server/lib/router-utils/build-data-route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import path from '../../../shared/lib/isomorphic/path'
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
import { isDynamicRoute } from '../../../shared/lib/router/utils/is-dynamic'
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'
import { normalizeRouteRegex } from '../../../lib/load-custom-routes'
import { escapeStringRegexp } from '../../../shared/lib/escape-regexp'

export function buildDataRoute(page: string, buildId: string) {
const pagePath = normalizePagePath(page)
const dataRoute = path.posix.join('/_next/data', buildId, `${pagePath}.json`)

let dataRouteRegex: string
let namedDataRouteRegex: string | undefined
let routeKeys: { [named: string]: string } | undefined

if (isDynamicRoute(page)) {
const routeRegex = getNamedRouteRegex(
dataRoute.replace(/\.json$/, ''),
true
)

dataRouteRegex = normalizeRouteRegex(
routeRegex.re.source.replace(/\(\?:\\\/\)\?\$$/, `\\.json$`)
)
namedDataRouteRegex = routeRegex.namedRegex!.replace(
/\(\?:\/\)\?\$$/,
`\\.json$`
)
routeKeys = routeRegex.routeKeys
} else {
dataRouteRegex = normalizeRouteRegex(
new RegExp(
`^${path.posix.join(
'/_next/data',
escapeStringRegexp(buildId),
`${pagePath}.json`
)}$`
).source
)
}

return {
page,
routeKeys,
dataRouteRegex,
namedDataRouteRegex,
}
}
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/router-utils/setup-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { verifyTypeScriptSetup } from '../../../lib/verifyTypeScriptSetup'
import { verifyPartytownSetup } from '../../../lib/verify-partytown-setup'
import { getRouteRegex } from '../../../shared/lib/router/utils/route-regex'
import { normalizeAppPath } from '../../../shared/lib/router/utils/app-paths'
import { buildDataRoute } from '../../../shared/lib/router/utils/sorted-routes'
import { buildDataRoute } from './build-data-route'
import { MiddlewareMatcher } from '../../../build/analysis/get-page-static-info'
import { getRouteMatcher } from '../../../shared/lib/router/utils/route-matcher'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
Expand Down
12 changes: 6 additions & 6 deletions packages/next/src/server/require-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const resolve = process.env.NEXT_MINIMAL
const toResolveMap = (map: Record<string, string>): [string, string][] =>
Object.entries(map).map(([key, value]) => [key, resolve(value)])

export const defaultOverrides = {
'styled-jsx': dirname(resolve('styled-jsx/package.json')),
'styled-jsx/style': resolve('styled-jsx/style'),
}

export const baseOverrides = {
react: 'next/dist/compiled/react',
'react/package.json': 'next/dist/compiled/react/package.json',
Expand Down Expand Up @@ -73,12 +78,7 @@ export function addHookAliases(aliases: [string, string][] = []) {
}

// Add default aliases
addHookAliases([
// Use `require.resolve` explicitly to make them statically analyzable
// styled-jsx needs to be resolved as the external dependency.
['styled-jsx', dirname(resolve('styled-jsx/package.json'))],
['styled-jsx/style', resolve('styled-jsx/style')],
])
addHookAliases(toResolveMap(defaultOverrides))

// Override built-in React packages if necessary
function overrideReact() {
Expand Down
49 changes: 0 additions & 49 deletions packages/next/src/shared/lib/router/utils/sorted-routes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import path from '../../../lib/isomorphic/path'
import { normalizePagePath } from '../../page-path/normalize-page-path'
import { isDynamicRoute } from './is-dynamic'
import { getNamedRouteRegex } from './route-regex'
import { normalizeRouteRegex } from '../../../../lib/load-custom-routes'
import { escapeStringRegexp } from '../../escape-regexp'

class UrlNode {
placeholder: boolean = true
children: Map<string, UrlNode> = new Map()
Expand Down Expand Up @@ -202,48 +195,6 @@ class UrlNode {
}
}

export function buildDataRoute(page: string, buildId: string) {
const pagePath = normalizePagePath(page)
const dataRoute = path.posix.join('/_next/data', buildId, `${pagePath}.json`)

let dataRouteRegex: string
let namedDataRouteRegex: string | undefined
let routeKeys: { [named: string]: string } | undefined

if (isDynamicRoute(page)) {
const routeRegex = getNamedRouteRegex(
dataRoute.replace(/\.json$/, ''),
true
)

dataRouteRegex = normalizeRouteRegex(
routeRegex.re.source.replace(/\(\?:\\\/\)\?\$$/, `\\.json$`)
)
namedDataRouteRegex = routeRegex.namedRegex!.replace(
/\(\?:\/\)\?\$$/,
`\\.json$`
)
routeKeys = routeRegex.routeKeys
} else {
dataRouteRegex = normalizeRouteRegex(
new RegExp(
`^${path.posix.join(
'/_next/data',
escapeStringRegexp(buildId),
`${pagePath}.json`
)}$`
).source
)
}

return {
page,
routeKeys,
dataRouteRegex,
namedDataRouteRegex,
}
}

export function getSortedRoutes(
normalizedPages: ReadonlyArray<string>
): string[] {
Expand Down

0 comments on commit 0379b34

Please sign in to comment.