Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage pageExtensions for resolving in loader #36747

Merged
merged 4 commits into from May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions packages/next/build/entries.ts
Expand Up @@ -245,6 +245,7 @@ interface CreateEntrypointsParams {
target: 'server' | 'serverless' | 'experimental-serverless-trace'
viewsDir?: string
viewPaths?: Record<string, string>
pageExtensions: string[]
}

export function getEdgeServerEntry(opts: {
Expand Down Expand Up @@ -285,13 +286,12 @@ export function getEdgeServerEntry(opts: {
return `next-middleware-ssr-loader?${stringify(loaderParams)}!`
}

export function getViewsEntry(opts: { pagePath: string; viewsDir: string }) {
const loaderParams = {
pagePath: opts.pagePath,
viewsDir: opts.viewsDir,
}

return `next-view-loader?${stringify(loaderParams)}!`
export function getViewsEntry(opts: {
pagePath: string
viewsDir: string
pageExtensions: string[]
}) {
return `next-view-loader?${stringify(opts)}!`
}

export function getServerlessEntry(opts: {
Expand Down Expand Up @@ -358,7 +358,16 @@ export function getClientEntry(opts: {
}

export async function createEntrypoints(params: CreateEntrypointsParams) {
const { config, pages, pagesDir, isDev, target, viewsDir, viewPaths } = params
const {
config,
pages,
pagesDir,
isDev,
target,
viewsDir,
viewPaths,
pageExtensions,
} = params
const edgeServer: webpack5.EntryObject = {}
const server: webpack5.EntryObject = {}
const client: webpack5.EntryObject = {}
Expand Down Expand Up @@ -401,6 +410,7 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
server[serverBundlePath] = getViewsEntry({
pagePath: mappings[page],
viewsDir,
pageExtensions,
})
} else if (isTargetLikeServerless(target)) {
if (page !== '/_app' && page !== '/_document') {
Expand Down
1 change: 1 addition & 0 deletions packages/next/build/index.ts
Expand Up @@ -376,6 +376,7 @@ export default async function build(
target,
viewsDir,
viewPaths: mappedViewPaths,
pageExtensions: config.pageExtensions,
})
)

Expand Down
26 changes: 11 additions & 15 deletions packages/next/build/webpack/loaders/next-view-loader.ts
Expand Up @@ -37,26 +37,22 @@ async function resolveLayoutPathsByPage({
return layoutPaths
}

const extensions = [
...NODE_RESOLVE_OPTIONS.extensions,
...NODE_RESOLVE_OPTIONS.extensions.map((ext) => `.server${ext}`),
...NODE_RESOLVE_OPTIONS.extensions.map((ext) => `.client${ext}`),
]
const resolveOptions: any = {
...NODE_RESOLVE_OPTIONS,
extensions,
}

const nextViewLoader: webpack.LoaderDefinitionFunction<{
pagePath: string
viewsDir: string
pageExtensions: string[]
}> = async function nextViewLoader() {
const loaderOptions = this.getOptions() || {}
const { viewsDir, pagePath, pageExtensions } = this.getOptions() || {}

const extensions = pageExtensions.map((extension) => `.${extension}`)
const resolveOptions: any = {
...NODE_RESOLVE_OPTIONS,
extensions,
}
const resolve = this.getResolve(resolveOptions)
const viewsDir = loaderOptions.viewsDir

const layoutPaths = await resolveLayoutPathsByPage({
pagePath: loaderOptions.pagePath,
pagePath: pagePath,
resolve: async (pathname) => {
try {
return await resolve(this.rootContext, pathname)
Expand Down Expand Up @@ -87,11 +83,11 @@ const nextViewLoader: webpack.LoaderDefinitionFunction<{

// Add page itself to the list of components
componentsCode.push(
`'${pathToUrlPath(loaderOptions.pagePath).replace(
`'${pathToUrlPath(pagePath).replace(
new RegExp(`/page\\.+(${extensions.join('|')})$`),
''
// use require so that we can bust the require cache
)}': () => require('${loaderOptions.pagePath}')`
)}': () => require('${pagePath}')`
)

const result = `
Expand Down
3 changes: 3 additions & 0 deletions packages/next/server/dev/hot-reloader.ts
Expand Up @@ -421,6 +421,7 @@ export default class HotReloader {
pagesDir: this.pagesDir,
previewMode: this.previewProps,
target: 'server',
pageExtensions: this.config.pageExtensions,
})
)

Expand Down Expand Up @@ -488,6 +489,7 @@ export default class HotReloader {
pagesDir: this.pagesDir,
previewMode: this.previewProps,
target: 'server',
pageExtensions: this.config.pageExtensions,
})
).client,
hasReactRoot: this.hasReactRoot,
Expand Down Expand Up @@ -605,6 +607,7 @@ export default class HotReloader {
relative(this.viewsDir!, absolutePagePath)
),
viewsDir: this.viewsDir!,
pageExtensions: this.config.pageExtensions,
})
: request,
})
Expand Down