Skip to content

Commit e5192f2

Browse files
authored
fix: respect user defined included_files for functions from netlify.toml (#298)
1 parent eecf56b commit e5192f2

File tree

10 files changed

+72
-7
lines changed

10 files changed

+72
-7
lines changed

src/build/functions/server.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { cp, mkdir, readFile, rm, writeFile } from 'fs/promises'
22
import { join } from 'node:path'
3+
import { relative } from 'path'
34

45
import { glob } from 'fast-glob'
56

@@ -8,15 +9,43 @@ import { PluginContext, SERVER_HANDLER_NAME } from '../plugin-context.js'
89

910
/** Copies the runtime dist folder to the lambda */
1011
const copyHandlerDependencies = async (ctx: PluginContext) => {
12+
const promises: Promise<void>[] = []
13+
const { included_files: includedFiles = [] } = ctx.netlifyConfig.functions?.['*'] || {}
14+
// if the user specified some files to include in the lambda
15+
// we need to copy them to the functions-internal folder
16+
if (includedFiles.length !== 0) {
17+
const resolvedFiles = await Promise.all(
18+
includedFiles.map((globPattern) => glob(globPattern, { cwd: process.cwd() })),
19+
)
20+
for (const filePath of resolvedFiles.flat()) {
21+
promises.push(
22+
cp(
23+
join(process.cwd(), filePath),
24+
// the serverHandlerDir is aware of the dist dir.
25+
// The distDir must not be the package path therefore we need to rely on the
26+
// serverHandlerDir instead of the serverHandlerRootDir
27+
// therefore we need to remove the package path from the filePath
28+
join(ctx.serverHandlerDir, relative(ctx.packagePath, filePath)),
29+
{
30+
recursive: true,
31+
force: true,
32+
},
33+
),
34+
)
35+
}
36+
}
37+
1138
const fileList = await glob('dist/**/*', { cwd: ctx.pluginDir })
12-
await Promise.all(
13-
[...fileList].map((path) =>
14-
cp(join(ctx.pluginDir, path), join(ctx.serverHandlerDir, '.netlify', path), {
39+
40+
for (const filePath of fileList) {
41+
promises.push(
42+
cp(join(ctx.pluginDir, filePath), join(ctx.serverHandlerDir, '.netlify', filePath), {
1543
recursive: true,
1644
force: true,
1745
}),
18-
),
19-
)
46+
)
47+
}
48+
await Promise.all(promises)
2049
}
2150

2251
const writeHandlerManifest = async (ctx: PluginContext) => {

tests/e2e/nx-integrated.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ test('Renders the Home page correctly', async ({ page, nxIntegrated }) => {
1212

1313
const h1 = page.locator('h1')
1414
await expect(h1).toHaveText('Hello there,\nWelcome next-app 👋')
15+
16+
// test additional netlify.toml settings
17+
await page.goto(`${nxIntegrated.url}/api/static`)
18+
const body = (await page.$('body').then((el) => el?.textContent())) || '{}'
19+
expect(body).toBe('{"words":"hello world"}')
1520
})
1621

1722
test('Renders the Home page correctly with distDir', async ({ page, nxIntegratedDistDir }) => {

tests/e2e/simple-app.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ test('Renders the Home page correctly', async ({ page, simpleNextApp }) => {
1414
await expect(h1).toHaveText('Home')
1515

1616
await expectImageWasLoaded(page.locator('img'))
17+
18+
await page.goto(`${simpleNextApp.url}/api/static`)
19+
20+
const body = (await page.$('body').then((el) => el?.textContent())) || '{}'
21+
expect(body).toBe('{"words":"hello world"}')
1722
})
1823

1924
test('Renders the Home page correctly with distDir', async ({ page, simpleNextAppDistDir }) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse } from 'next/server'
2+
import { readFile } from 'node:fs/promises'
3+
4+
export async function GET(request) {
5+
const words = await readFile('static/words.txt', 'utf-8')
6+
return NextResponse.json({ words })
7+
}
8+
9+
export const dynamic = 'force-dynamic'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[functions]
2+
directory = "netlify/functions"
3+
included_files = ["apps/next-app/static/**"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse } from 'next/server'
2+
import { readFile } from 'node:fs/promises'
3+
4+
export async function GET(request) {
5+
const words = await readFile('static/words.txt', 'utf-8')
6+
return NextResponse.json({ words })
7+
}
8+
9+
export const dynamic = 'force-dynamic'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[functions]
2+
directory = "netlify/functions"
3+
included_files = ["static/**"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

tests/utils/create-e2e-fixture.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { execaCommand } from 'execa'
22
import fg from 'fast-glob'
33
import { exec } from 'node:child_process'
44
import { existsSync } from 'node:fs'
5-
import { copyFile, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
5+
import { appendFile, copyFile, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
66
import { tmpdir } from 'node:os'
77
import { dirname, join } from 'node:path'
88
import { env } from 'node:process'
@@ -111,7 +111,7 @@ async function buildAndPackRuntime(config: {
111111
)
112112
const [{ filename, name }] = JSON.parse(stdout)
113113

114-
await writeFile(
114+
await appendFile(
115115
join(join(dest, packagePath), 'netlify.toml'),
116116
`[build]
117117
command = "${buildCommand}"

0 commit comments

Comments
 (0)