Skip to content

Commit

Permalink
Update to latest @vercel/nft and use async fs methods (#29341)
Browse files Browse the repository at this point in the history
This updates to the latest version of `node-file-trace` and leverages the new async fs handling with webpack. In a follow-up PR we will implement the async resolver to share resolving with webpack as well. 

x-ref: vercel/nft#236
  • Loading branch information
ijjk authored Sep 23, 2021
1 parent f2c266b commit c3a395a
Show file tree
Hide file tree
Showing 7 changed files with 105,285 additions and 105,270 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import nodePath from 'path'
import { Span } from '../../../trace'
import { spans } from './profiling-plugin'
import isError from '../../../lib/is-error'
import { nodeFileTrace } from 'next/dist/compiled/@vercel/nft'
import { TRACE_OUTPUT_VERSION } from '../../../shared/lib/constants'
import {
webpack,
isWebpack5,
sources,
} from 'next/dist/compiled/webpack/webpack'
import { TRACE_OUTPUT_VERSION } from '../../../shared/lib/constants'
import { spans } from './profiling-plugin'
import { Span } from '../../../trace'
import isError from '../../../lib/is-error'

const PLUGIN_NAME = 'TraceEntryPointsPlugin'
const TRACE_IGNORES = [
Expand Down Expand Up @@ -147,10 +147,9 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
})
})

// TODO: investigate allowing non-sync fs calls in node-file-trace
// for better performance
const readFile = (path: string, _span: Span) => {
// return span.traceChild('read-file', { path }).traceFn(() => {
const readFile = async (
path: string
): Promise<Buffer | string | null> => {
const mod = depModMap.get(path) || entryModMap.get(path)

// map the transpiled source when available to avoid
Expand All @@ -162,7 +161,15 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
}

try {
return compilation.inputFileSystem.readFileSync(path)
return await new Promise((resolve, reject) => {
;(
compilation.inputFileSystem
.readFile as typeof import('fs').readFile
)(path, (err, data) => {
if (err) return reject(err)
resolve(data)
})
})
} catch (e) {
if (
isError(e) &&
Expand All @@ -172,12 +179,18 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
}
throw e
}
// })
}
const readlink = (path: string, _span: Span) => {
// return span.traceChild('read-link', { path }).traceFn(() => {
const readlink = async (path: string): Promise<string | null> => {
try {
return compilation.inputFileSystem.readlinkSync(path)
return await new Promise((resolve, reject) => {
;(
compilation.inputFileSystem
.readlink as typeof import('fs').readlink
)(path, (err, link) => {
if (err) return reject(err)
resolve(link)
})
})
} catch (e) {
if (
isError(e) &&
Expand All @@ -189,19 +202,25 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
}
throw e
}
// })
}
const stat = (path: string, _span: Span) => {
// return span.traceChild('stat', { path }).traceFn(() => {
const stat = async (
path: string
): Promise<import('fs').Stats | null> => {
try {
return compilation.inputFileSystem.statSync(path)
return await new Promise((resolve, reject) => {
;(
compilation.inputFileSystem.stat as typeof import('fs').stat
)(path, (err, stats) => {
if (err) return reject(err)
resolve(stats)
})
})
} catch (e) {
if (isError(e) && e.code === 'ENOENT') {
return null
}
throw e
}
// })
}

const nftCache = {}
Expand Down Expand Up @@ -260,9 +279,9 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
base: root,
cache: nftCache,
processCwd: this.appDir,
readFile: (path) => readFile(path, fileTraceSpan),
readlink: (path) => readlink(path, fileTraceSpan),
stat: (path) => stat(path, fileTraceSpan),
readFile,
readlink,
stat,
ignore: [...TRACE_IGNORES, ...this.excludeFiles],
mixedModules: true,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/next/compiled/@vercel/nft/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/next/compiled/terser/bundle.min.js

Large diffs are not rendered by default.

210,456 changes: 105,228 additions & 105,228 deletions packages/next/compiled/webpack/bundle5.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"@types/text-table": "0.2.1",
"@types/webpack-sources1": "npm:@types/webpack-sources@0.1.5",
"@vercel/ncc": "0.27.0",
"@vercel/nft": "0.12.2",
"@vercel/nft": "0.15.0",
"amphtml-validator": "1.0.33",
"arg": "4.1.0",
"async-retry": "1.2.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ export async function ncc_mini_css_extract_plugin(task, opts) {
externals: {
...externals,
'./index': './index.js',
'schema-utils': 'next/dist/compiled/schema-utils3',
'schema-utils': externals['schema-utils3'],
'webpack-sources': externals['webpack-sources1'],
},
})
Expand Down
30 changes: 13 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5053,17 +5053,18 @@
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.27.0.tgz#c0cfeebb0bebb56052719efa4a0ecc090a932c76"
integrity sha512-DllIJQapnU2YwewIhh/4dYesmMQw3h2cFtabECc/zSJHqUbNa0eJuEkRa6DXbZvh1YPWBtYQoPV17NlDpBw1Vw==

"@vercel/nft@0.12.2":
version "0.12.2"
resolved "https://registry.yarnpkg.com/@vercel/nft/-/nft-0.12.2.tgz#67ea9f231d24639b3783e3e69bef173659972d3b"
integrity sha512-H8n44GboVnJaVVX4+WfuOTAaNLDnUIYH4KpMZcXll7KMNIcg0JTd0IFRsIBe/uvuXisqm6nEANp8Tr3/1dlRQw==
"@vercel/nft@0.15.0":
version "0.15.0"
resolved "https://registry.yarnpkg.com/@vercel/nft/-/nft-0.15.0.tgz#af8b7a8583d6ce3d12334f2ccf9b2c29acfd6e2c"
integrity sha512-ZEEQXTL2yB1Hv9WkcLtLZXV9JKZmg/ttWTt2mwKQeb8DNVwKXBfhV0zSuKGDSl0q1yAutR7PoFCkrh7Myz2aow==
dependencies:
"@mapbox/node-pre-gyp" "^1.0.5"
acorn "^8.1.0"
acorn "^8.3.0"
acorn-class-fields "^1.0.0"
acorn-private-class-elements "^1.0.0"
acorn-static-class-features "^1.0.0"
bindings "^1.4.0"
estree-walker "^0.6.1"
estree-walker "2.0.2"
glob "^7.1.3"
graceful-fs "^4.1.15"
micromatch "^4.0.2"
Expand Down Expand Up @@ -5476,17 +5477,12 @@ acorn@^8.0.4:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.0.4.tgz#7a3ae4191466a6984eee0fe3407a4f3aa9db8354"
integrity sha512-XNP0PqF1XD19ZlLKvB7cMmnZswW4C/03pRHgirB30uSJTaS3A3V1/P4sS3HPvFmjoriPCJQs+JDSbm4bL1TxGQ==

acorn@^8.1.0:
version "8.2.4"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.2.4.tgz#caba24b08185c3b56e3168e97d15ed17f4d31fd0"
integrity sha512-Ibt84YwBDDA890eDiDCEqcbwvHlBvzzDkU2cGBBDDI1QWT12jTiXIOn2CIw5KK4i6N5Z2HUxwYjzriDyqaqqZg==

acorn@^8.2.4:
version "8.4.1"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.4.1.tgz#56c36251fc7cabc7096adc18f05afe814321a28c"
integrity sha512-asabaBSkEKosYKMITunzX177CXxQ4Q8BSSzMTKD+FefUhipQC70gfW5SiUDhYQ3vk8G+81HqQk7Fv9OXwwn9KA==

acorn@^8.4.1:
acorn@^8.3.0, acorn@^8.4.1:
version "8.5.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.5.0.tgz#4512ccb99b3698c752591e9bb4472e38ad43cee2"
integrity sha512-yXbYeFy+jUuYd3/CDcg2NkIYE991XYX/bje7LmjJigUciaeO1JR4XxXgCIV1/Zc/dRuFEyw1L0pbA+qynJkW5Q==
Expand Down Expand Up @@ -9114,6 +9110,11 @@ estree-util-is-identifier-name@^1.0.0:
resolved "https://registry.yarnpkg.com/estree-util-is-identifier-name/-/estree-util-is-identifier-name-1.1.0.tgz#2e3488ea06d9ea2face116058864f6370b37456d"
integrity sha512-OVJZ3fGGt9By77Ix9NhaRbzfbDV/2rx9EP7YIDJTmsZSEc5kYn2vWcNccYyahJL2uAQZK2a5Or2i0wtIKTPoRQ==

estree-walker@2.0.2, estree-walker@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac"
integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==

estree-walker@^0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
Expand All @@ -9123,11 +9124,6 @@ estree-walker@^1.0.1:
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-1.0.1.tgz#31bc5d612c96b704106b477e6dd5d8aa138cb700"
integrity sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==

estree-walker@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac"
integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==

esutils@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
Expand Down

0 comments on commit c3a395a

Please sign in to comment.