Skip to content

Commit

Permalink
refactor: normalize fs/promises usage (#13441)
Browse files Browse the repository at this point in the history
  • Loading branch information
花果山大圣 committed Jun 7, 2023
1 parent 5503198 commit f201805
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path'
import { parse as parseUrl } from 'node:url'
import fs, { promises as fsp } from 'node:fs'
import fs from 'node:fs'
import fsp from 'node:fs/promises'
import { Buffer } from 'node:buffer'
import * as mrmime from 'mrmime'
import type {
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/loadFallback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promises as fs } from 'node:fs'
import fsp from 'node:fs/promises'
import type { Plugin } from '..'
import { cleanUrl } from '../utils'

Expand All @@ -11,9 +11,9 @@ export function loadFallbackPlugin(): Plugin {
async load(id) {
try {
// if we don't add `await` here, we couldn't catch the error in readFile
return await fs.readFile(cleanUrl(id), 'utf-8')
return await fsp.readFile(cleanUrl(id), 'utf-8')
} catch (e) {
return fs.readFile(id, 'utf-8')
return fsp.readFile(id, 'utf-8')
}
},
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path'
import { promises as fs } from 'node:fs'
import fsp from 'node:fs/promises'
import type { ExistingRawSourceMap, SourceMap } from 'rollup'
import type { Logger } from '../logger'
import { createDebugger } from '../utils'
Expand Down Expand Up @@ -27,7 +27,7 @@ export async function injectSourcesContent(
let sourceRoot: string | undefined
try {
// The source root is undefined for virtual modules and permission errors.
sourceRoot = await fs.realpath(
sourceRoot = await fsp.realpath(
path.resolve(path.dirname(file), map.sourceRoot || ''),
)
} catch {}
Expand All @@ -40,7 +40,7 @@ export async function injectSourcesContent(
if (sourceRoot) {
sourcePath = path.resolve(sourceRoot, sourcePath)
}
return fs.readFile(sourcePath, 'utf-8').catch(() => {
return fsp.readFile(sourcePath, 'utf-8').catch(() => {
missingSources.push(sourcePath)
return null
})
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promises as fs } from 'node:fs'
import fsp from 'node:fs/promises'
import path from 'node:path'
import { performance } from 'node:perf_hooks'
import getEtag from 'etag'
Expand Down Expand Up @@ -188,7 +188,7 @@ async function loadAndTransform(
// like /service-worker.js or /api/users
if (options.ssr || isFileServingAllowed(file, server)) {
try {
code = await fs.readFile(file, 'utf-8')
code = await fsp.readFile(file, 'utf-8')
debugLoad?.(`${timeFrom(loadStart)} [fs] ${prettyUrl}`)
} catch (e) {
if (e.code !== 'ENOENT') {
Expand Down Expand Up @@ -324,7 +324,7 @@ async function loadAndTransform(

function createConvertSourceMapReadMap(originalFileName: string) {
return (filename: string) => {
return fs.readFile(
return fsp.readFile(
path.resolve(path.dirname(originalFileName), filename),
'utf-8',
)
Expand Down

0 comments on commit f201805

Please sign in to comment.