Skip to content

Commit

Permalink
feat: add baseURL property to edge functions import maps (#5047)
Browse files Browse the repository at this point in the history
* fix(deps): update dependency @netlify/edge-bundler to v2

* feat: add support for edge functions import maps in Dev

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
eduardoboucas and renovate[bot] authored Sep 15, 2022
1 parent 4c3b3ee commit 934cde6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/lib/edge-functions/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const { promises: fs } = require('fs')
const path = require('path')
const { cwd } = require('process')
const { pathToFileURL } = require('url')

const { warn } = require('../../utils/command-helpers')
const { getPathInProject } = require('../settings')
Expand Down Expand Up @@ -58,7 +59,10 @@ const getInternalFunctions = async () => {
if (importMap !== null) {
return {
...data,
importMap,
importMap: {
baseURL: pathToFileURL(importMapPath),
...importMap,
},
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions tests/integration/100.command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,65 @@ test('Serves an Edge Function that terminates a response', async (t) => {
})
})

test('Serves an edge function with an import map', async (t) => {
await withSiteBuilder('site-with-edge-function-with-import-map', async (builder) => {
const publicDir = 'public'
builder
.withNetlifyToml({
config: {
build: {
publish: publicDir,
edge_functions: 'netlify/edge-functions',
},
},
})
.withContentFiles([
{
path: path.join(publicDir, 'index.html'),
content: '<html>index</html>',
},
{
path: path.join('.netlify', 'edge-functions', 'manifest.json'),
content: JSON.stringify({
functions: [{ function: 'hello', path: '/edge-function' }],
import_map: '../../import-map.json',
version: 1,
}),
},
{
path: 'import-map.json',
content: JSON.stringify({
imports: {
'alias:util': './util.js',
},
}),
},
{
path: 'util.js',
content: `export const name = "world"`,
},
])
.withEdgeFunction({
handler: `
import { name } from 'alias:util'
export default async () => new Response('Hello, ' + name)
`,
internal: true,
name: 'hello',
})

await builder.buildAsync()

await withDevServer({ cwd: builder.directory }, async (server) => {
const response = await got(`${server.url}/edge-function`)

t.is(response.statusCode, 200)
t.is(response.body, 'Hello, world')
})
})
})

test('Serves an Edge Function with a rewrite', async (t) => {
await withSiteBuilder('site-with-edge-function-that-rewrites', async (builder) => {
const publicDir = 'public'
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/utils/site-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ const createSiteBuilder = ({ siteName }) => {
})
return builder
},
withEdgeFunction: ({ handler, name = 'function', pathPrefix = '' }) => {
const dest = path.join(directory, pathPrefix, 'netlify/edge-functions', `${name}.js`)
withEdgeFunction: ({ handler, internal = false, name = 'function', pathPrefix = '' }) => {
const edgeFunctionsDirectory = internal ? '.netlify/edge-functions' : 'netlify/edge-functions'
const dest = path.join(directory, pathPrefix, edgeFunctionsDirectory, `${name}.js`)
tasks.push(async () => {
const content = `export default ${handler.toString()}`
const content = typeof handler === 'string' ? handler : `export default ${handler.toString()}`
await ensureDir(path.dirname(dest))
await writeFile(dest, content)
})
Expand Down

1 comment on commit 934cde6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 223 MB

Please sign in to comment.