Skip to content

Commit

Permalink
Leverage pageExtensions for resolving in loader (#36747)
Browse files Browse the repository at this point in the history
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
  • Loading branch information
timneutkens committed May 7, 2022
1 parent 49910ea commit 51d962d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
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

0 comments on commit 51d962d

Please sign in to comment.