Skip to content

Commit b492477

Browse files
authored
Turbopack: fix ESM project in standalone mode (vercel#78774)
1 parent c3c071e commit b492477

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

packages/next/src/build/collect-build-traces.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export async function collectBuildTraces({
8282
hasSsrAmpPages,
8383
buildTraceContext,
8484
outputFileTracingRoot,
85+
isTurbopack,
8586
}: {
8687
dir: string
8788
distDir: string
@@ -93,6 +94,7 @@ export async function collectBuildTraces({
9394
nextBuildSpan?: Span
9495
config: NextConfigComplete
9596
buildTraceContext?: BuildTraceContext
97+
isTurbopack: boolean
9698
}) {
9799
const startTime = Date.now()
98100
debug('starting build traces')
@@ -278,6 +280,11 @@ export async function collectBuildTraces({
278280
)
279281
}
280282

283+
if (isTurbopack) {
284+
addToTracedFiles(distDir, './package.json', serverTracedFiles)
285+
addToTracedFiles(distDir, './package.json', minimalServerTracedFiles)
286+
}
287+
281288
{
282289
const chunksToTrace: string[] = [
283290
...(buildTraceContext?.chunksTrace?.action.input || []),

packages/next/src/build/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,7 @@ export default async function build(
15161516
hasSsrAmpPages: false,
15171517
buildTraceContext,
15181518
outputFileTracingRoot,
1519+
isTurbopack: false,
15191520
})
15201521
.catch((err) => {
15211522
console.error(err)
@@ -2441,6 +2442,7 @@ export default async function build(
24412442
hasSsrAmpPages,
24422443
buildTraceContext,
24432444
outputFileTracingRoot,
2445+
isTurbopack: true,
24442446
}).catch((err) => {
24452447
console.error(err)
24462448
process.exit(1)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { FileRef, nextTestSetup } from 'e2e-utils'
2+
import fs from 'fs-extra'
3+
import os from 'os'
4+
import path from 'path'
5+
import {
6+
findPort,
7+
initNextServerScript,
8+
killApp,
9+
fetchViaHTTP,
10+
} from 'next-test-utils'
11+
12+
if (!(globalThis as any).isNextStart) {
13+
it('should skip for non-next start', () => {})
14+
} else {
15+
describe('output: standalone with ESM app dir', () => {
16+
const { next, skipped } = nextTestSetup({
17+
files: {
18+
app: new FileRef(path.join(__dirname, 'app')),
19+
pages: new FileRef(path.join(__dirname, 'pages')),
20+
public: new FileRef(path.join(__dirname, 'public')),
21+
'next.config.js': 'export default {"output": "standalone"}',
22+
},
23+
packageJson: {
24+
type: 'module',
25+
},
26+
skipStart: true,
27+
})
28+
29+
if (skipped) {
30+
return
31+
}
32+
33+
beforeAll(async () => {
34+
await next.start()
35+
})
36+
37+
it('should work correctly with output standalone', async () => {
38+
const tmpFolder = path.join(os.tmpdir(), 'next-standalone-' + Date.now())
39+
await fs.mkdirp(tmpFolder)
40+
await fs.writeFile(
41+
path.join(tmpFolder, 'package.json'),
42+
'{"type": "module"}'
43+
)
44+
const distFolder = path.join(tmpFolder, 'test')
45+
await fs.move(path.join(next.testDir, '.next/standalone'), distFolder)
46+
let server: any
47+
try {
48+
const testServer = path.join(distFolder, 'server.js')
49+
const appPort = await findPort()
50+
server = await initNextServerScript(
51+
testServer,
52+
/- Local:/,
53+
{
54+
...process.env,
55+
PORT: appPort.toString(),
56+
},
57+
undefined,
58+
{
59+
cwd: distFolder,
60+
}
61+
)
62+
for (const testPath of ['/app', '/pages', '/opengraph-image']) {
63+
const res = await fetchViaHTTP(appPort, testPath)
64+
expect(res.status).toBe(200)
65+
}
66+
} finally {
67+
if (server) await killApp(server)
68+
await fs.remove(tmpFolder)
69+
}
70+
})
71+
})
72+
}

0 commit comments

Comments
 (0)