Skip to content

Commit

Permalink
feat: load import maps from Deno config file (#5315)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas authored Dec 15, 2022
1 parent f9f45ed commit 22de9e2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/commands/deploy/deploy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ const bundleEdgeFunctions = async (options) => {
...options,
buffer: true,
featureFlags: {
edge_functions_produce_eszip: true,
edge_functions_read_deno_config: true,
},
})

Expand Down
1 change: 1 addition & 0 deletions src/lib/edge-functions/proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const prepareServer = async ({
const distImportMapPath = getPathInProject([DIST_IMPORT_MAP_PATH])
const runIsolate = await bundler.serve({
...getDownloadUpdateFunctions(),
basePath: projectDir,
certificatePath,
debug: env.NETLIFY_DENO_DEBUG === 'true',
distImportMapPath,
Expand Down
133 changes: 74 additions & 59 deletions tests/integration/100.command.dev.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -213,65 +213,6 @@ 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 Expand Up @@ -839,6 +780,80 @@ test('should respect in-source configuration from internal edge functions', asyn
})
})

test('Serves edge functions with import maps coming from the Deno config file and from the internal manifest', async (t) => {
await withSiteBuilder('site-with-edge-functions-and-import-maps', async (builder) => {
const internalEdgeFunctionsDir = path.join('.netlify', 'edge-functions')

await builder
.withNetlifyToml({
config: {
build: {
publish: 'public',
},
},
})
.withEdgeFunction({
config: () => ({ path: '/greet' }),
handler: `import { greet } from "greeter"; export default async () => new Response(greet("Netlify"))`,
name: 'greet',
})
.withEdgeFunction({
handler: `import { yell } from "yeller"; export default async () => new Response(yell("Netlify"))`,
name: 'yell',
internal: true,
})
// User-defined import map
.withContentFiles([
{
// eslint-disable-next-line no-template-curly-in-string
content: 'export const greet = (name: string) => `Hello, ${name}!`',
path: 'greeter.ts',
},
{
content: JSON.stringify({ importMap: 'import_map.json' }),
path: 'deno.json',
},
{
content: JSON.stringify({ imports: { greeter: './greeter.ts' } }),
path: 'import_map.json',
},
])
// Internal import map
.withContentFiles([
{
content: 'export const yell = (name: string) => name.toUpperCase()',
path: path.join(internalEdgeFunctionsDir, 'util', 'yeller.ts'),
},
{
content: JSON.stringify({
functions: [{ function: 'yell', path: '/yell' }],
import_map: 'import_map.json',
version: 1,
}),
path: path.join(internalEdgeFunctionsDir, 'manifest.json'),
},
{
content: JSON.stringify({ imports: { yeller: './util/yeller.ts' } }),
path: path.join(internalEdgeFunctionsDir, 'import_map.json'),
},
])

await builder.buildAsync()

await withDevServer({ cwd: builder.directory }, async ({ port }) => {
const res1 = await got(`http://localhost:${port}/greet`, { throwHttpErrors: false })

t.is(res1.statusCode, 200)
t.is(res1.body, 'Hello, Netlify!')

const res2 = await got(`http://localhost:${port}/yell`, { throwHttpErrors: false })

t.is(res2.statusCode, 200)
t.is(res2.body, 'NETLIFY')
})
})
})

test('should have only allowed environment variables set', async (t) => {
const siteInfo = {
account_slug: 'test-account',
Expand Down

1 comment on commit 22de9e2

@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: 249 MB

Please sign in to comment.