Unverified Commit 87d0fc9f authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

avoid using absolute paths in import/require (#29797)

Using absolute paths leads to unnecessary watched paths and tested paths for resolving
parent 77b65327
...@@ -18,14 +18,17 @@ function nextClientPagesLoader(this: any) { ...@@ -18,14 +18,17 @@ function nextClientPagesLoader(this: any) {
pagesLoaderSpan.setAttribute('absolutePagePath', absolutePagePath) pagesLoaderSpan.setAttribute('absolutePagePath', absolutePagePath)
const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath) const stringifiedPagePath = loaderUtils.stringifyRequest(
this,
absolutePagePath
)
const stringifiedPage = JSON.stringify(page) const stringifiedPage = JSON.stringify(page)
return ` return `
(window.__NEXT_P = window.__NEXT_P || []).push([ (window.__NEXT_P = window.__NEXT_P || []).push([
${stringifiedPage}, ${stringifiedPage},
function () { function () {
return require(${stringifiedAbsolutePagePath}); return require(${stringifiedPagePath});
} }
]); ]);
` `
......
import devalue from 'next/dist/compiled/devalue' import devalue from 'next/dist/compiled/devalue'
import escapeRegexp from 'next/dist/compiled/escape-string-regexp' import escapeRegexp from 'next/dist/compiled/escape-string-regexp'
import loaderUtils from 'next/dist/compiled/loader-utils'
import { join } from 'path' import { join } from 'path'
import { parse } from 'querystring' import { parse } from 'querystring'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
...@@ -112,7 +113,10 @@ const nextServerlessLoader: webpack.loader.Loader = function () { ...@@ -112,7 +113,10 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
} }
const apiHandler = getApiHandler({ const apiHandler = getApiHandler({
pageModule: require("${absolutePagePath}"), pageModule: require(${loaderUtils.stringifyRequest(
this,
absolutePagePath
)}),
rewrites: combinedRewrites, rewrites: combinedRewrites,
i18n: ${i18n || 'undefined'}, i18n: ${i18n || 'undefined'},
page: "${page}", page: "${page}",
...@@ -137,12 +141,21 @@ const nextServerlessLoader: webpack.loader.Loader = function () { ...@@ -137,12 +141,21 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
} }
import { getPageHandler } from 'next/dist/build/webpack/loaders/next-serverless-loader/page-handler' import { getPageHandler } from 'next/dist/build/webpack/loaders/next-serverless-loader/page-handler'
const documentModule = require("${absoluteDocumentPath}") const documentModule = require(${loaderUtils.stringifyRequest(
this,
absoluteDocumentPath
)})
const appMod = require('${absoluteAppPath}') const appMod = require(${loaderUtils.stringifyRequest(
this,
absoluteAppPath
)})
let App = appMod.default || appMod.then && appMod.then(mod => mod.default); let App = appMod.default || appMod.then && appMod.then(mod => mod.default);
const compMod = require('${absolutePagePath}') const compMod = require(${loaderUtils.stringifyRequest(
this,
absolutePagePath
)})
const Component = compMod.default || compMod.then && compMod.then(mod => mod.default) const Component = compMod.default || compMod.then && compMod.then(mod => mod.default)
export default Component export default Component
...@@ -175,9 +188,14 @@ const nextServerlessLoader: webpack.loader.Loader = function () { ...@@ -175,9 +188,14 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
pageConfig: config, pageConfig: config,
appModule: App, appModule: App,
documentModule: documentModule, documentModule: documentModule,
errorModule: require("${absoluteErrorPath}"), errorModule: require(${loaderUtils.stringifyRequest(
this,
absoluteErrorPath
)}),
notFoundModule: ${ notFoundModule: ${
absolute404Path ? `require("${absolute404Path}")` : undefined absolute404Path
? `require(${loaderUtils.stringifyRequest(this, absolute404Path)})`
: undefined
}, },
pageGetStaticProps: getStaticProps, pageGetStaticProps: getStaticProps,
pageGetStaticPaths: getStaticPaths, pageGetStaticPaths: getStaticPaths,
......
...@@ -2,7 +2,7 @@ import { getOverlayMiddleware } from '@next/react-dev-overlay/lib/middleware' ...@@ -2,7 +2,7 @@ import { getOverlayMiddleware } from '@next/react-dev-overlay/lib/middleware'
import { NextHandleFunction } from 'connect' import { NextHandleFunction } from 'connect'
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse } from 'http'
import { WebpackHotMiddleware } from './hot-middleware' import { WebpackHotMiddleware } from './hot-middleware'
import { join } from 'path' import { join, relative, isAbsolute } from 'path'
import { UrlObject } from 'url' import { UrlObject } from 'url'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
import { import {
...@@ -423,13 +423,22 @@ export default class HotReloader { ...@@ -423,13 +423,22 @@ export default class HotReloader {
absolutePagePath, absolutePagePath,
} }
entrypoints[bundlePath] = finalizeEntrypoint( if (isClientCompilation) {
bundlePath, entrypoints[bundlePath] = finalizeEntrypoint(
isClientCompilation bundlePath,
? `next-client-pages-loader?${stringify(pageLoaderOpts)}!` `next-client-pages-loader?${stringify(pageLoaderOpts)}!`,
: absolutePagePath, false
!isClientCompilation )
) } else {
let request = relative(config.context!, absolutePagePath)
if (!isAbsolute(request) && !request.startsWith('../'))
request = `./${request}`
entrypoints[bundlePath] = finalizeEntrypoint(
bundlePath,
request,
true
)
}
}) })
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment