Skip to content

Commit

Permalink
fix: respect user defined included_files for functions from netlify.t…
Browse files Browse the repository at this point in the history
…oml (#298)
  • Loading branch information
lukasholzer authored Feb 23, 2024
1 parent eecf56b commit e5192f2
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 7 deletions.
39 changes: 34 additions & 5 deletions src/build/functions/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cp, mkdir, readFile, rm, writeFile } from 'fs/promises'
import { join } from 'node:path'
import { relative } from 'path'

import { glob } from 'fast-glob'

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

/** Copies the runtime dist folder to the lambda */
const copyHandlerDependencies = async (ctx: PluginContext) => {
const promises: Promise<void>[] = []
const { included_files: includedFiles = [] } = ctx.netlifyConfig.functions?.['*'] || {}
// if the user specified some files to include in the lambda
// we need to copy them to the functions-internal folder
if (includedFiles.length !== 0) {
const resolvedFiles = await Promise.all(
includedFiles.map((globPattern) => glob(globPattern, { cwd: process.cwd() })),
)
for (const filePath of resolvedFiles.flat()) {
promises.push(
cp(
join(process.cwd(), filePath),
// the serverHandlerDir is aware of the dist dir.
// The distDir must not be the package path therefore we need to rely on the
// serverHandlerDir instead of the serverHandlerRootDir
// therefore we need to remove the package path from the filePath
join(ctx.serverHandlerDir, relative(ctx.packagePath, filePath)),
{
recursive: true,
force: true,
},
),
)
}
}

const fileList = await glob('dist/**/*', { cwd: ctx.pluginDir })
await Promise.all(
[...fileList].map((path) =>
cp(join(ctx.pluginDir, path), join(ctx.serverHandlerDir, '.netlify', path), {

for (const filePath of fileList) {
promises.push(
cp(join(ctx.pluginDir, filePath), join(ctx.serverHandlerDir, '.netlify', filePath), {
recursive: true,
force: true,
}),
),
)
)
}
await Promise.all(promises)
}

const writeHandlerManifest = async (ctx: PluginContext) => {
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/nx-integrated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ test('Renders the Home page correctly', async ({ page, nxIntegrated }) => {

const h1 = page.locator('h1')
await expect(h1).toHaveText('Hello there,\nWelcome next-app 👋')

// test additional netlify.toml settings
await page.goto(`${nxIntegrated.url}/api/static`)
const body = (await page.$('body').then((el) => el?.textContent())) || '{}'
expect(body).toBe('{"words":"hello world"}')
})

test('Renders the Home page correctly with distDir', async ({ page, nxIntegratedDistDir }) => {
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/simple-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ test('Renders the Home page correctly', async ({ page, simpleNextApp }) => {
await expect(h1).toHaveText('Home')

await expectImageWasLoaded(page.locator('img'))

await page.goto(`${simpleNextApp.url}/api/static`)

const body = (await page.$('body').then((el) => el?.textContent())) || '{}'
expect(body).toBe('{"words":"hello world"}')
})

test('Renders the Home page correctly with distDir', async ({ page, simpleNextAppDistDir }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextResponse } from 'next/server'
import { readFile } from 'node:fs/promises'

export async function GET(request) {
const words = await readFile('static/words.txt', 'utf-8')
return NextResponse.json({ words })
}

export const dynamic = 'force-dynamic'
3 changes: 3 additions & 0 deletions tests/fixtures/nx-integrated/apps/next-app/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[functions]
directory = "netlify/functions"
included_files = ["apps/next-app/static/**"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
9 changes: 9 additions & 0 deletions tests/fixtures/simple-next-app/app/api/static/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextResponse } from 'next/server'
import { readFile } from 'node:fs/promises'

export async function GET(request) {
const words = await readFile('static/words.txt', 'utf-8')
return NextResponse.json({ words })
}

export const dynamic = 'force-dynamic'
3 changes: 3 additions & 0 deletions tests/fixtures/simple-next-app/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[functions]
directory = "netlify/functions"
included_files = ["static/**"]
1 change: 1 addition & 0 deletions tests/fixtures/simple-next-app/static/words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
4 changes: 2 additions & 2 deletions tests/utils/create-e2e-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { execaCommand } from 'execa'
import fg from 'fast-glob'
import { exec } from 'node:child_process'
import { existsSync } from 'node:fs'
import { copyFile, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { appendFile, copyFile, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import { env } from 'node:process'
Expand Down Expand Up @@ -111,7 +111,7 @@ async function buildAndPackRuntime(config: {
)
const [{ filename, name }] = JSON.parse(stdout)

await writeFile(
await appendFile(
join(join(dest, packagePath), 'netlify.toml'),
`[build]
command = "${buildCommand}"
Expand Down

0 comments on commit e5192f2

Please sign in to comment.