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

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

Merged
merged 7 commits into from Mar 18, 2022
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
Expand Up @@ -360,6 +360,9 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
fileList,
reasons,
(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)
const depMod = depModMap.get(file)
const isAsset = reasons.get(file)?.type.includes('asset')
Expand Down
13 changes: 13 additions & 0 deletions test/integration/production/pages/api/readfile-dirname.js
@@ -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)
}
@@ -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)
}
38 changes: 38 additions & 0 deletions test/integration/production/test/index.test.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -600,6 +622,22 @@ describe('Production Usage', () => {
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 () => {
const url = `http://localhost:${appPort}`
const res = await fetchViaHTTP(url, `/api/post-1?val=1`)
Expand Down