Commit 9fa8baa0 authored by Maël Nison's avatar Maël Nison Committed by Joe Haddad

Uses the PnP API when available (#8668)

* Uses the PnP API when available

* Moves the resolution into an helper

* Update packages/next/lib/resolve-request.ts
Co-Authored-By: 's avatarJoe Haddad <joe.haddad@zeit.co>

* Apply Prettier
parent 93d6a670
...@@ -6,7 +6,7 @@ import { ...@@ -6,7 +6,7 @@ import {
SERVER_DIRECTORY, SERVER_DIRECTORY,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
} from '../next-server/lib/constants' } from '../next-server/lib/constants'
import resolve from 'next/dist/compiled/resolve/index.js' import { resolveRequest } from '../lib/resolve-request'
import path from 'path' import path from 'path'
import crypto from 'crypto' import crypto from 'crypto'
import webpack from 'webpack' import webpack from 'webpack'
...@@ -113,7 +113,7 @@ export default async function getBaseWebpackConfig( ...@@ -113,7 +113,7 @@ export default async function getBaseWebpackConfig(
let typeScriptPath let typeScriptPath
try { try {
typeScriptPath = resolve.sync('typescript', { basedir: dir }) typeScriptPath = resolveRequest('typescript', `${dir}/`)
} catch (_) {} } catch (_) {}
const tsConfigPath = path.join(dir, 'tsconfig.json') const tsConfigPath = path.join(dir, 'tsconfig.json')
const useTypeScript = Boolean( const useTypeScript = Boolean(
...@@ -314,11 +314,10 @@ export default async function getBaseWebpackConfig( ...@@ -314,11 +314,10 @@ export default async function getBaseWebpackConfig(
return callback() return callback()
} }
resolve( let res
request, try {
{ basedir: context, preserveSymlinks: true }, res = resolveRequest(request, context)
(err, res) => { } catch (err) {
if (err) {
return callback() return callback()
} }
...@@ -331,9 +330,7 @@ export default async function getBaseWebpackConfig( ...@@ -331,9 +330,7 @@ export default async function getBaseWebpackConfig(
!res.match(/next[/\\]dist[/\\]next-server[/\\]/) && !res.match(/next[/\\]dist[/\\]next-server[/\\]/) &&
(res.match(/next[/\\]dist[/\\]/) || (res.match(/next[/\\]dist[/\\]/) ||
res.match(/node_modules[/\\]@babel[/\\]runtime[/\\]/) || res.match(/node_modules[/\\]@babel[/\\]runtime[/\\]/) ||
res.match( res.match(/node_modules[/\\]@babel[/\\]runtime-corejs2[/\\]/))
/node_modules[/\\]@babel[/\\]runtime-corejs2[/\\]/
))
) { ) {
return callback() return callback()
} }
...@@ -351,8 +348,6 @@ export default async function getBaseWebpackConfig( ...@@ -351,8 +348,6 @@ export default async function getBaseWebpackConfig(
} }
callback() callback()
}
)
}, },
] ]
: [ : [
......
import resolve from 'next/dist/compiled/resolve/index.js'
import path from 'path'
export function resolveRequest(req: string, issuer: string) {
// The `resolve` package is prebuilt through ncc, which prevents
// PnP from being able to inject itself into it. To circumvent
// this, we simply use PnP directly when available.
// @ts-ignore
if (process.versions.pnp) {
const { resolveRequest } = require(`pnpapi`)
return resolveRequest(req, issuer, { considerBuiltins: false })
} else {
const basedir =
issuer.endsWith(path.posix.sep) || issuer.endsWith(path.win32.sep)
? issuer
: path.dirname(issuer)
return resolve.sync(req, { basedir })
}
}
import chalk from 'chalk' import chalk from 'chalk'
import fs from 'fs' import fs from 'fs'
import resolve from 'next/dist/compiled/resolve/index.js'
import os from 'os' import os from 'os'
import path from 'path' import path from 'path'
import { promisify } from 'util' import { promisify } from 'util'
import { fileExists } from './file-exists' import { fileExists } from './file-exists'
import { recursiveReadDir } from './recursive-readdir' import { recursiveReadDir } from './recursive-readdir'
import { resolveRequest } from './resolve-request'
const writeFile = promisify(fs.writeFile) const writeFile = promisify(fs.writeFile)
const readFile = promisify(fs.readFile) const readFile = promisify(fs.readFile)
...@@ -43,7 +43,7 @@ async function checkDependencies({ ...@@ -43,7 +43,7 @@ async function checkDependencies({
const missingPackages = requiredPackages.filter(p => { const missingPackages = requiredPackages.filter(p => {
try { try {
resolve.sync(p.file, { basedir: dir }) resolveRequest(p.file, `${dir}/`)
} catch (_) { } catch (_) {
return true return 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