diff --git a/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts b/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts index 4239d87097f8..2f0e7ababae6 100644 --- a/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts +++ b/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts @@ -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') diff --git a/test/integration/production/pages/api/readfile-dirname.js b/test/integration/production/pages/api/readfile-dirname.js new file mode 100644 index 000000000000..a85b9716e518 --- /dev/null +++ b/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) +} diff --git a/test/integration/production/pages/api/readfile-processcwd.js b/test/integration/production/pages/api/readfile-processcwd.js new file mode 100644 index 000000000000..9b8f89b96654 --- /dev/null +++ b/test/integration/production/pages/api/readfile-processcwd.js @@ -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) +} diff --git a/test/integration/production/test/index.test.js b/test/integration/production/test/index.test.js index 6a4db30c04e7..5276dbf5db56 100644 --- a/test/integration/production/test/index.test.js +++ b/test/integration/production/test/index.test.js @@ -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) { @@ -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`)