Skip to content

Commit

Permalink
fix: use import maps in config evaluation (#166)
Browse files Browse the repository at this point in the history
* fix: use import maps in config evaluation

* chore: add missing import to test

* chore: update test
  • Loading branch information
eduardoboucas committed Oct 19, 2022
1 parent dbf5b3a commit 8e5ab76
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Expand Up @@ -20,6 +20,7 @@ module.exports = {
{
files: ['node/**/*.test.ts', 'vitest.config.ts'],
rules: {
'max-lines-per-function': 'off',
'max-statements': 'off',
'no-magic-numbers': 'off',
},
Expand Down
2 changes: 1 addition & 1 deletion node/bundler.ts
Expand Up @@ -141,7 +141,7 @@ const bundle = async (
return {}
}

return getFunctionConfig(func, deno, logger)
return getFunctionConfig(func, importMap, deno, logger)
}),
)

Expand Down
12 changes: 12 additions & 0 deletions node/config.test.ts
@@ -1,5 +1,6 @@
import { promises as fs } from 'fs'
import { join, resolve } from 'path'
import { pathToFileURL } from 'url'

import del from 'del'
import { stub } from 'sinon'
Expand All @@ -12,6 +13,14 @@ import { DenoBridge } from './bridge.js'
import { bundle } from './bundler.js'
import { getFunctionConfig } from './config.js'
import type { Declaration } from './declaration.js'
import { ImportMap } from './import_map.js'

const importMapFile = {
baseURL: new URL('file:///some/path/import-map.json'),
imports: {
'alias:helper': pathToFileURL(join(fixturesDir, 'helper.ts')).toString(),
},
}

test('`getFunctionConfig` extracts configuration properties from function file', async () => {
const { path: tmpDir } = await tmp.dir()
Expand Down Expand Up @@ -118,6 +127,7 @@ test('`getFunctionConfig` extracts configuration properties from function file',
name: func.name,
path,
},
new ImportMap([importMapFile]),
deno,
logger,
)
Expand All @@ -144,6 +154,7 @@ test('Ignores function paths from the in-source `config` function if the feature
featureFlags: {
edge_functions_produce_eszip: true,
},
importMaps: [importMapFile],
})
const generatedFiles = await fs.readdir(tmpDir.path)

Expand Down Expand Up @@ -182,6 +193,7 @@ test('Loads function paths from the in-source `config` function', async () => {
edge_functions_config_export: true,
edge_functions_produce_eszip: true,
},
importMaps: [importMapFile],
})
const generatedFiles = await fs.readdir(tmpDir.path)

Expand Down
4 changes: 3 additions & 1 deletion node/config.ts
Expand Up @@ -6,6 +6,7 @@ import tmp from 'tmp-promise'

import { DenoBridge } from './bridge.js'
import { EdgeFunction } from './edge_function.js'
import { ImportMap } from './import_map.js'
import { Logger } from './logger.js'
import { getPackagePath } from './package_json.js'

Expand All @@ -31,7 +32,7 @@ const getConfigExtractor = () => {
return configExtractorPath
}

export const getFunctionConfig = async (func: EdgeFunction, deno: DenoBridge, log: Logger) => {
export const getFunctionConfig = async (func: EdgeFunction, importMap: ImportMap, deno: DenoBridge, log: Logger) => {
// The extractor is a Deno script that will import the function and run its
// `config` export, if one exists.
const extractorPath = getConfigExtractor()
Expand All @@ -53,6 +54,7 @@ export const getFunctionConfig = async (func: EdgeFunction, deno: DenoBridge, lo
'--allow-read',
`--allow-write=${collector.path}`,
'--quiet',
`--import-map=${importMap.toDataURL()}`,
extractorPath,
pathToFileURL(func.path).href,
pathToFileURL(collector.path).href,
Expand Down
@@ -1,4 +1,10 @@
export default async () => new Response('Hello from framework function 1')
import { greet } from 'alias:helper'

export default async () => {
const greeting = greet('framework function 1')

return new Response(greeting)
}

export const config = () => ({
path: '/framework-func1',
Expand Down
@@ -1 +1,7 @@
export default async () => new Response('Hello from framework function 2')
import { greet } from 'alias:helper'

export default async () => {
const greeting = greet('framework function 2')

return new Response(greeting)
}
@@ -1,4 +1,10 @@
export default async () => new Response('Hello from user function 1')
import { greet } from 'alias:helper'

export default async () => {
const greeting = greet('user function 1')

return new Response(greeting)
}

export const config = () => ({
path: '/user-func1',
Expand Down
@@ -1 +1,7 @@
export default async () => new Response('Hello from user function 2')
import { greet } from 'alias:helper'

export default async () => {
const greeting = greet('user function 2')

return new Response(greeting)
}

0 comments on commit 8e5ab76

Please sign in to comment.