From 8e5ab76ff01217068668e9c65d6cd9f6d2936029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 19 Oct 2022 20:57:17 +0100 Subject: [PATCH] fix: use import maps in config evaluation (#166) * fix: use import maps in config evaluation * chore: add missing import to test * chore: update test --- .eslintrc.cjs | 1 + node/bundler.ts | 2 +- node/config.test.ts | 12 ++++++++++++ node/config.ts | 4 +++- .../.netlify/edge-functions/framework-func1.ts | 8 +++++++- .../.netlify/edge-functions/framework-func2.ts | 8 +++++++- .../with_config/netlify/edge-functions/user-func1.ts | 8 +++++++- .../with_config/netlify/edge-functions/user-func2.ts | 8 +++++++- 8 files changed, 45 insertions(+), 6 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 7309e4e9..327f9ca5 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -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', }, diff --git a/node/bundler.ts b/node/bundler.ts index 51f8b025..b60718cf 100644 --- a/node/bundler.ts +++ b/node/bundler.ts @@ -141,7 +141,7 @@ const bundle = async ( return {} } - return getFunctionConfig(func, deno, logger) + return getFunctionConfig(func, importMap, deno, logger) }), ) diff --git a/node/config.test.ts b/node/config.test.ts index 1e0809e0..2ba78a43 100644 --- a/node/config.test.ts +++ b/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' @@ -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() @@ -118,6 +127,7 @@ test('`getFunctionConfig` extracts configuration properties from function file', name: func.name, path, }, + new ImportMap([importMapFile]), deno, logger, ) @@ -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) @@ -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) diff --git a/node/config.ts b/node/config.ts index 5dfa8d89..723fb6f7 100644 --- a/node/config.ts +++ b/node/config.ts @@ -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' @@ -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() @@ -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, diff --git a/test/fixtures/with_config/.netlify/edge-functions/framework-func1.ts b/test/fixtures/with_config/.netlify/edge-functions/framework-func1.ts index 0b833ea9..79f6c9ff 100644 --- a/test/fixtures/with_config/.netlify/edge-functions/framework-func1.ts +++ b/test/fixtures/with_config/.netlify/edge-functions/framework-func1.ts @@ -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', diff --git a/test/fixtures/with_config/.netlify/edge-functions/framework-func2.ts b/test/fixtures/with_config/.netlify/edge-functions/framework-func2.ts index 2d631e2a..800f8b5c 100644 --- a/test/fixtures/with_config/.netlify/edge-functions/framework-func2.ts +++ b/test/fixtures/with_config/.netlify/edge-functions/framework-func2.ts @@ -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) +} diff --git a/test/fixtures/with_config/netlify/edge-functions/user-func1.ts b/test/fixtures/with_config/netlify/edge-functions/user-func1.ts index 3e59f8e6..bce3bfa8 100644 --- a/test/fixtures/with_config/netlify/edge-functions/user-func1.ts +++ b/test/fixtures/with_config/netlify/edge-functions/user-func1.ts @@ -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', diff --git a/test/fixtures/with_config/netlify/edge-functions/user-func2.ts b/test/fixtures/with_config/netlify/edge-functions/user-func2.ts index 59b6a97d..c7d3fcfe 100644 --- a/test/fixtures/with_config/netlify/edge-functions/user-func2.ts +++ b/test/fixtures/with_config/netlify/edge-functions/user-func2.ts @@ -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) +}