Add test to ensure fs.readFile() works with Output File Tracing (#35338)

Add test for `fs.readFile()`

- Related to https://github.com/vercel/vercel/issues/7256
- Related to https://github.com/vercel/next.js/discussions/32236

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
This commit is contained in:
Steven 2022-03-18 18:51:26 -04:00 committed by GitHub
parent 7ca958de88
commit 73b83a0b88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 0 deletions

View file

@ -360,6 +360,9 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
fileList, fileList,
reasons, reasons,
(file) => { (file) => {
// if a file was imported and a loader handled it
// we don't include it in the trace e.g.
// static image imports, CSS imports
file = nodePath.join(this.tracingRoot, file) file = nodePath.join(this.tracingRoot, file)
const depMod = depModMap.get(file) const depMod = depModMap.get(file)
const isAsset = reasons.get(file)?.type.includes('asset') const isAsset = reasons.get(file)?.type.includes('asset')

View file

@ -0,0 +1,13 @@
import { readFileSync } from 'fs'
import { join } from 'path'
// __dirname is going to be different after build since the file
// is located in .next/server/pages/api instead of the src location
// so this is not currently expected to work
const file = join(__dirname, '../../static/data/item.txt')
const content = readFileSync(file, 'utf8')
console.log({ file, content })
export default (req, res) => {
res.end(content)
}

View file

@ -0,0 +1,9 @@
import { readFileSync } from 'fs'
import { join } from 'path'
const file = join(process.cwd(), 'static/data/item.txt')
const content = readFileSync(file, 'utf8')
console.log({ file, content })
export default (req, res) => {
res.end(content)
}

View file

@ -234,6 +234,28 @@ describe('Production Usage', () => {
/!/, /!/,
], ],
}, },
{
page: '/api/readfile-dirname',
tests: [/webpack-api-runtime\.js/, /static\/data\/item\.txt/],
notTests: [
/next\/dist\/server\/next\.js/,
/next\/dist\/bin/,
/\0/,
/\?/,
/!/,
],
},
{
page: '/api/readfile-processcwd',
tests: [/webpack-api-runtime\.js/, /static\/data\/item\.txt/],
notTests: [
/next\/dist\/server\/next\.js/,
/next\/dist\/bin/,
/\0/,
/\?/,
/!/,
],
},
] ]
for (const check of checks) { for (const check of checks) {
@ -600,6 +622,22 @@ describe('Production Usage', () => {
expect(body).toEqual('API hello works') expect(body).toEqual('API hello works')
}) })
// Today, `__dirname` usage fails because Next.js moves the source file
// to .next/server/pages/api but it doesn't move the asset file.
// In the future, it would be nice to make `__dirname` work too.
it('does not work with pages/api/readfile-dirname.js', async () => {
const url = `http://localhost:${appPort}`
const res = await fetchViaHTTP(url, `/api/readfile-dirname`)
expect(res.status).toBe(500)
})
it('should work with pages/api/readfile-processcwd.js', async () => {
const url = `http://localhost:${appPort}`
const res = await fetchViaHTTP(url, `/api/readfile-processcwd`)
const body = await res.text()
expect(body).toBe('item')
})
it('should work with dynamic params and search string', async () => { it('should work with dynamic params and search string', async () => {
const url = `http://localhost:${appPort}` const url = `http://localhost:${appPort}`
const res = await fetchViaHTTP(url, `/api/post-1?val=1`) const res = await fetchViaHTTP(url, `/api/post-1?val=1`)