Skip to content

Commit

Permalink
fix: use builtin resolve to support pnp
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Jan 9, 2021
1 parent 8bdff57 commit 2b6c752
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 100 deletions.
6 changes: 1 addition & 5 deletions packages/next/build/plugins/collect-plugins.ts
@@ -1,7 +1,6 @@
import findUp from 'next/dist/compiled/find-up'
import { promises } from 'fs'
import path from 'path'
import resolve from 'next/dist/compiled/resolve/index.js'
import { execOnce } from '../../next-server/lib/utils'

const { version } = require('next/package.json')
Expand Down Expand Up @@ -213,10 +212,7 @@ async function _collectPlugins(
nextPluginNames.map((name) =>
collectPluginMeta(
env,
resolve.sync(path.join(name, 'package.json'), {
basedir: dir,
preserveSymlinks: true,
}),
require.resolve(path.join(name, 'package.json'), { paths: [dir] }),
name,
version
)
Expand Down
44 changes: 23 additions & 21 deletions packages/next/build/webpack-config.ts
Expand Up @@ -18,7 +18,6 @@ import {
import { fileExists } from '../lib/file-exists'
import { getPackageVersion } from '../lib/get-package-version'
import { Rewrite } from '../lib/load-custom-routes'
import { resolveRequest } from '../lib/resolve-request'
import { getTypeScriptConfiguration } from '../lib/typescript/getTypeScriptConfiguration'
import {
CLIENT_STATIC_FILES_RUNTIME_MAIN,
Expand Down Expand Up @@ -308,7 +307,7 @@ export default async function getBaseWebpackConfig(

let typeScriptPath: string | undefined
try {
typeScriptPath = resolveRequest('typescript', `${dir}/`)
typeScriptPath = require.resolve('typescript', { paths: [dir] })
} catch (_) {}
const tsConfigPath = path.join(dir, 'tsconfig.json')
const useTypeScript = Boolean(
Expand Down Expand Up @@ -609,7 +608,7 @@ export default async function getBaseWebpackConfig(
// exist.
let res: string
try {
res = resolveRequest(request, `${context}/`)
res = require.resolve(request, { paths: [context] })
} catch (err) {
// If the request cannot be resolved, we need to tell webpack to
// "bundle" it so that webpack shows an error (that it cannot be
Expand Down Expand Up @@ -647,7 +646,7 @@ export default async function getBaseWebpackConfig(
// we need to bundle the code (even if it _should_ be external).
let baseRes: string | null
try {
baseRes = resolveRequest(request, `${dir}/`)
baseRes = require.resolve(request, { paths: [dir] })
} catch (err) {
baseRes = null
}
Expand Down Expand Up @@ -1426,29 +1425,32 @@ export default async function getBaseWebpackConfig(
try {
// Resolve the version of `@zeit/next-css` as depended on by the Sass,
// Less or Stylus plugin.
const correctNextCss = resolveRequest(
'@zeit/next-css',
isCss
? // Resolve `@zeit/next-css` from the base directory
`${dir}/`
: // Else, resolve it from the specific plugins
require.resolve(
isSass
? '@zeit/next-sass'
: isLess
? '@zeit/next-less'
: isStylus
? '@zeit/next-stylus'
: 'next'
)
)
const correctNextCss = require.resolve('@zeit/next-css', {
paths: [
isCss
? // Resolve `@zeit/next-css` from the base directory
dir
: // Else, resolve it from the specific plugins
require.resolve(
isSass
? '@zeit/next-sass'
: isLess
? '@zeit/next-less'
: isStylus
? '@zeit/next-stylus'
: 'next'
),
],
})

// If we found `@zeit/next-css` ...
if (correctNextCss) {
// ... resolve the version of `css-loader` shipped with that
// package instead of whichever was hoisted highest in your
// `node_modules` tree.
const correctCssLoader = resolveRequest(use.loader, correctNextCss)
const correctCssLoader = require.resolve(use.loader, {
paths: [correctNextCss],
})
if (correctCssLoader) {
// We saved the user from a failed build!
use.loader = correctCssLoader
Expand Down
3 changes: 1 addition & 2 deletions packages/next/build/webpack/config/blocks/css/plugins.ts
@@ -1,6 +1,5 @@
import chalk from 'chalk'
import { findConfig } from '../../../../../lib/find-config'
import { resolveRequest } from '../../../../../lib/resolve-request'
import browserslist from 'browserslist'

type CssPluginCollection_Array = (string | [string, boolean | object])[]
Expand Down Expand Up @@ -57,7 +56,7 @@ async function loadPlugin(
throw new Error(genericErrorText)
}

const pluginPath = resolveRequest(pluginName, `${dir}/`)
const pluginPath = require.resolve(pluginName, { paths: [dir] })
if (isIgnoredPlugin(pluginPath)) {
return false
} else if (options === true) {
Expand Down
18 changes: 0 additions & 18 deletions packages/next/compiled/resolve/LICENSE

This file was deleted.

1 change: 0 additions & 1 deletion packages/next/compiled/resolve/index.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/next/compiled/resolve/package.json

This file was deleted.

5 changes: 3 additions & 2 deletions packages/next/lib/get-package-version.ts
Expand Up @@ -2,7 +2,6 @@ import { promises as fs } from 'fs'
import findUp from 'next/dist/compiled/find-up'
import JSON5 from 'next/dist/compiled/json5'
import * as path from 'path'
import { resolveRequest } from './resolve-request'

type PackageJsonDependencies = {
dependencies: Record<string, string>
Expand Down Expand Up @@ -52,7 +51,9 @@ export async function getPackageVersion({
: `${cwd}/`

try {
const targetPath = resolveRequest(`${name}/package.json`, cwd2)
const targetPath = require.resolve(`${name}/package.json`, {
paths: [cwd2],
})
const targetContent = await fs.readFile(targetPath, 'utf-8')
return JSON5.parse(targetContent).version ?? null
} catch {
Expand Down
18 changes: 0 additions & 18 deletions packages/next/lib/resolve-request.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/next/lib/typescript/hasNecessaryDependencies.ts
Expand Up @@ -2,7 +2,6 @@ import chalk from 'chalk'
import path from 'path'
import { fileExists } from '../file-exists'
import { getOxfordCommaList } from '../oxford-comma-list'
import { resolveRequest } from '../resolve-request'
import { FatalTypeScriptError } from './FatalTypeScriptError'

const requiredPackages = [
Expand All @@ -22,7 +21,7 @@ export async function hasNecessaryDependencies(

const missingPackages = requiredPackages.filter((p) => {
try {
resolutions.set(p.pkg, resolveRequest(p.file, path.join(baseDir, '/')))
resolutions.set(p.pkg, require.resolve(p.file, { paths: [baseDir] }))
return false
} catch (_) {
return true
Expand Down
2 changes: 0 additions & 2 deletions packages/next/package.json
Expand Up @@ -172,7 +172,6 @@
"@types/react": "16.9.17",
"@types/react-dom": "16.9.4",
"@types/react-is": "16.7.1",
"@types/resolve": "0.0.8",
"@types/semver": "7.3.1",
"@types/send": "0.14.4",
"@types/sharp": "0.26.0",
Expand Down Expand Up @@ -220,7 +219,6 @@
"postcss-preset-env": "6.7.0",
"postcss-scss": "3.0.4",
"recast": "0.18.5",
"resolve": "1.11.0",
"semver": "7.3.2",
"send": "0.17.1",
"source-map": "0.6.1",
Expand Down

0 comments on commit 2b6c752

Please sign in to comment.