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

avoid using absolute paths in import/require #29797

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ function nextClientPagesLoader(this: any) {

pagesLoaderSpan.setAttribute('absolutePagePath', absolutePagePath)

const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath)
const stringifiedPagePath = loaderUtils.stringifyRequest(
this,
absolutePagePath
)
const stringifiedPage = JSON.stringify(page)

return `
(window.__NEXT_P = window.__NEXT_P || []).push([
${stringifiedPage},
function () {
return require(${stringifiedAbsolutePagePath});
return require(${stringifiedPagePath});
}
]);
`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import devalue from 'next/dist/compiled/devalue'
import escapeRegexp from 'next/dist/compiled/escape-string-regexp'
import loaderUtils from 'next/dist/compiled/loader-utils'
import { join } from 'path'
import { parse } from 'querystring'
import { webpack } from 'next/dist/compiled/webpack/webpack'
Expand Down Expand Up @@ -112,7 +113,10 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
}

const apiHandler = getApiHandler({
pageModule: require("${absolutePagePath}"),
pageModule: require(${loaderUtils.stringifyRequest(
this,
absolutePagePath
)}),
rewrites: combinedRewrites,
i18n: ${i18n || 'undefined'},
page: "${page}",
Expand All @@ -137,12 +141,21 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
}
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);

const compMod = require('${absolutePagePath}')
const compMod = require(${loaderUtils.stringifyRequest(
this,
absolutePagePath
)})

const Component = compMod.default || compMod.then && compMod.then(mod => mod.default)
export default Component
Expand Down Expand Up @@ -175,9 +188,14 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
pageConfig: config,
appModule: App,
documentModule: documentModule,
errorModule: require("${absoluteErrorPath}"),
errorModule: require(${loaderUtils.stringifyRequest(
this,
absoluteErrorPath
)}),
notFoundModule: ${
absolute404Path ? `require("${absolute404Path}")` : undefined
absolute404Path
? `require(${loaderUtils.stringifyRequest(this, absolute404Path)})`
: undefined
},
pageGetStaticProps: getStaticProps,
pageGetStaticPaths: getStaticPaths,
Expand Down
25 changes: 17 additions & 8 deletions packages/next/server/dev/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getOverlayMiddleware } from '@next/react-dev-overlay/lib/middleware'
import { NextHandleFunction } from 'connect'
import { IncomingMessage, ServerResponse } from 'http'
import { WebpackHotMiddleware } from './hot-middleware'
import { join } from 'path'
import { join, relative, isAbsolute } from 'path'
import { UrlObject } from 'url'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import {
Expand Down Expand Up @@ -423,13 +423,22 @@ export default class HotReloader {
absolutePagePath,
}

entrypoints[bundlePath] = finalizeEntrypoint(
bundlePath,
isClientCompilation
? `next-client-pages-loader?${stringify(pageLoaderOpts)}!`
: absolutePagePath,
!isClientCompilation
)
if (isClientCompilation) {
entrypoints[bundlePath] = finalizeEntrypoint(
bundlePath,
`next-client-pages-loader?${stringify(pageLoaderOpts)}!`,
false
)
} else {
let request = relative(config.context!, absolutePagePath)
if (!isAbsolute(request) && !request.startsWith('../'))
request = `./${request}`
entrypoints[bundlePath] = finalizeEntrypoint(
bundlePath,
request,
true
)
}
})
)

Expand Down