Unverified Commit 39498d60 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Prefer realpathSync.native over realpathSync (#48698)

Interesting learning from
[this](https://sun0day.github.io/blog/vite/why-vite4_3-is-faster.html#fs-realpathsync-issue),
that `fs.realpathSync` is 70x slower than `fs.realpathSync.native`.
Kudos to the Vite team!
parent db764c35
import type { webpack } from 'next/dist/compiled/webpack/webpack' import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { clearModuleContext } from '../../../server/web/sandbox' import { clearModuleContext } from '../../../server/web/sandbox'
import { realpathSync } from 'fs' import { realpathSync } from '../../../lib/realpath'
import path from 'path' import path from 'path'
import isError from '../../../lib/is-error' import isError from '../../../lib/is-error'
......
import fs from 'fs'
import path from 'path' import path from 'path'
import { commands } from './commands' import { commands } from './commands'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import { detectTypo } from './detect-typo' import { detectTypo } from './detect-typo'
import { realpathSync } from './realpath'
export function getProjectDir(dir?: string) { export function getProjectDir(dir?: string) {
try { try {
const resolvedDir = path.resolve(dir || '.') const resolvedDir = path.resolve(dir || '.')
const realDir = fs.realpathSync.native(resolvedDir) const realDir = realpathSync(resolvedDir)
if ( if (
resolvedDir !== realDir && resolvedDir !== realDir &&
......
import fs from 'fs'
const isWindows = process.platform === 'win32'
// Interesting learning from this, that fs.realpathSync is 70x slower than fs.realpathSync.native:
// https://sun0day.github.io/blog/vite/why-vite4_3-is-faster.html#fs-realpathsync-issue
// https://github.com/nodejs/node/issues/2680
// However, we can't use fs.realpathSync.native on Windows due to behavior differences.
export const realpathSync = isWindows ? fs.realpathSync : fs.realpathSync.native
// source: https://github.com/sindresorhus/resolve-from // source: https://github.com/sindresorhus/resolve-from
import fs from 'fs'
import path from 'path' import path from 'path'
import isError from './is-error' import isError from './is-error'
import { realpathSync } from './realpath'
const Module = require('module') const Module = require('module')
...@@ -23,7 +23,7 @@ export const resolveFrom = ( ...@@ -23,7 +23,7 @@ export const resolveFrom = (
} }
try { try {
fromDirectory = fs.realpathSync(fromDirectory) fromDirectory = realpathSync(fromDirectory)
} catch (error: unknown) { } catch (error: unknown) {
if (isError(error) && error.code === 'ENOENT') { if (isError(error) && error.code === 'ENOENT') {
fromDirectory = path.resolve(fromDirectory) fromDirectory = path.resolve(fromDirectory)
......
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