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

perf(next): use require.resolve instead of resolve #19518

Merged
merged 2 commits into from Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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] }),
Copy link
Contributor Author

@merceyz merceyz Jan 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wont preserve the symlink but hopefully that's fine

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 @@ -306,7 +305,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 @@ -607,7 +606,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 @@ -645,7 +644,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 @@ -1420,29 +1419,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