Skip to content

Commit e7bf18f

Browse files
timneutkenspieh
andauthored
fix: glob subdirectories in server/chunks to support turbopack builds (#2987)
* Glob all server files instead of specific subdirectories * Add fixture for Turbopack app * Add test * scale back the globs to only allow sub directories in server/(edge-)chunks * test: make sure all ways to build turbopack fixture would set --turbopack cli toggle --------- Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
1 parent f90f3fb commit e7bf18f

File tree

7 files changed

+117
-1
lines changed

7 files changed

+117
-1
lines changed

src/build/content/server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
100100
const destDir = join(ctx.serverHandlerDir, nextFolder)
101101

102102
const paths = await glob(
103-
[`*`, `server/*`, `server/chunks/*`, `server/edge-chunks/*`, `server/+(app|pages)/**/*.js`],
103+
[
104+
`*`,
105+
`server/*`,
106+
`server/chunks/**/*`,
107+
`server/edge-chunks/**/*`,
108+
`server/+(app|pages)/**/*.js`,
109+
],
104110
{
105111
cwd: srcDir,
106112
extglob: true,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function RootLayout({ children }) {
2+
return (
3+
<html lang="en">
4+
<body>{children}</body>
5+
</html>
6+
)
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <h1>Hello, Next.js!</h1>
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
output: 'standalone',
4+
eslint: {
5+
ignoreDuringBuilds: true,
6+
},
7+
}
8+
9+
module.exports = nextConfig
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "hello-world-app",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"postinstall": "next build --turbopack",
7+
"dev": "next dev --turbopack",
8+
"build": "next build --turbopack"
9+
},
10+
"dependencies": {
11+
"next": "latest",
12+
"react": "18.2.0",
13+
"react-dom": "18.2.0"
14+
}
15+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { load } from 'cheerio'
2+
import { getLogger } from 'lambda-local'
3+
import { cp } from 'node:fs/promises'
4+
import { HttpResponse, http, passthrough } from 'msw'
5+
import { setupServer } from 'msw/node'
6+
import { v4 } from 'uuid'
7+
import { Mock, afterAll, afterEach, beforeAll, beforeEach, expect, test, vi } from 'vitest'
8+
import { type FixtureTestContext } from '../utils/contexts.js'
9+
import { createFixture, invokeFunction, runPlugin } from '../utils/fixture.js'
10+
import { generateRandomObjectID, startMockBlobStore } from '../utils/helpers.js'
11+
12+
vi.mock('node:fs/promises', async (importOriginal) => {
13+
const fsPromisesModule = (await importOriginal()) as typeof import('node:fs/promises')
14+
return {
15+
...fsPromisesModule,
16+
cp: vi.fn(fsPromisesModule.cp.bind(fsPromisesModule)),
17+
}
18+
})
19+
20+
let server: ReturnType<typeof setupServer>
21+
22+
// Disable the verbose logging of the lambda-local runtime
23+
getLogger().level = 'alert'
24+
25+
const purgeAPI = vi.fn()
26+
27+
beforeAll(() => {
28+
server = setupServer(
29+
http.post('https://api.netlify.com/api/v1/purge', async ({ request }) => {
30+
purgeAPI(await request.json())
31+
32+
return HttpResponse.json({
33+
ok: true,
34+
})
35+
}),
36+
http.all(/.*/, () => passthrough()),
37+
)
38+
server.listen()
39+
})
40+
41+
afterAll(() => {
42+
// Disable API mocking after the tests are done.
43+
server.close()
44+
})
45+
46+
beforeEach<FixtureTestContext>(async (ctx) => {
47+
// set for each test a new deployID and siteID
48+
ctx.deployID = generateRandomObjectID()
49+
ctx.siteID = v4()
50+
vi.stubEnv('SITE_ID', ctx.siteID)
51+
vi.stubEnv('DEPLOY_ID', ctx.deployID)
52+
// hide debug logs in tests
53+
vi.spyOn(console, 'debug').mockImplementation(() => {})
54+
55+
purgeAPI.mockClear()
56+
57+
await startMockBlobStore(ctx)
58+
})
59+
60+
afterEach(() => {
61+
vi.unstubAllEnvs()
62+
})
63+
64+
test<FixtureTestContext>('Test that the hello-world-turbopack next app is working', async (ctx) => {
65+
await createFixture('hello-world-turbopack', ctx)
66+
await runPlugin(ctx)
67+
68+
// test the function call
69+
const home = await invokeFunction(ctx)
70+
expect(home.statusCode).toBe(200)
71+
expect(load(home.body)('h1').text()).toBe('Hello, Next.js!')
72+
})

tests/utils/create-e2e-fixture.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ async function cleanup(dest: string, deployId?: string): Promise<void> {
316316

317317
export const fixtureFactories = {
318318
simple: () => createE2EFixture('simple'),
319+
helloWorldTurbopack: () =>
320+
createE2EFixture('hello-world-turbopack', {
321+
buildCommand: 'next build --turbopack',
322+
}),
319323
outputExport: () => createE2EFixture('output-export'),
320324
ouputExportPublishOut: () =>
321325
createE2EFixture('output-export', {

0 commit comments

Comments
 (0)