From a0661b9d3d707317e328cfb32ec89cf4e4520b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Fri, 16 Sep 2022 15:10:09 +0100 Subject: [PATCH 1/2] feat: add `functions` level to `metadata` object --- deno/lib/stage2.ts | 33 +++++++++++++++++++++++++-------- node/formats/javascript.ts | 4 ++-- package.json | 3 ++- test/deno/stage2.test.ts | 2 +- test/node/stage_2.ts | 2 +- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/deno/lib/stage2.ts b/deno/lib/stage2.ts index 72f94195..8a2355fd 100644 --- a/deno/lib/stage2.ts +++ b/deno/lib/stage2.ts @@ -6,7 +6,30 @@ import type { InputFunction, WriteStage2Options } from '../../shared/stage2.ts' import { PUBLIC_SPECIFIER, STAGE2_SPECIFIER, virtualRoot } from './consts.ts' import { inlineModule, loadFromVirtualRoot, loadWithRetry } from './common.ts' -const getFunctionReference = (basePath: string, func: InputFunction, index: number) => { +interface FunctionReference { + exportLine: string + importLine: string + metadata: { + url: URL + } + name: string +} + +const getMetadata = (references: FunctionReference[]) => { + const functions = references.reduce( + (acc, { metadata, name }) => ({ + ...acc, + [name]: metadata, + }), + {}, + ) + + return { + functions, + } +} + +const getFunctionReference = (basePath: string, func: InputFunction, index: number): FunctionReference => { const importName = `func${index}` const exportLine = `"${func.name}": ${importName}` const url = getVirtualPath(basePath, func.path) @@ -25,13 +48,7 @@ export const getStage2Entry = (basePath: string, functions: InputFunction[]) => const lines = functions.map((func, index) => getFunctionReference(basePath, func, index)) const importLines = lines.map(({ importLine }) => importLine).join('\n') const exportLines = lines.map(({ exportLine }) => exportLine).join(', ') - const metadata = lines.reduce( - (acc, { metadata, name }) => ({ - ...acc, - [name]: metadata, - }), - {}, - ) + const metadata = getMetadata(lines) const functionsExport = `export const functions = {${exportLines}};` const metadataExport = `export const metadata = ${JSON.stringify(metadata)};` diff --git a/node/formats/javascript.ts b/node/formats/javascript.ts index c6e60815..95243821 100644 --- a/node/formats/javascript.ts +++ b/node/formats/javascript.ts @@ -108,7 +108,7 @@ const getLocalEntryPoint = ( }: GetLocalEntryPointOptions, ) => { const bootImport = `import { boot } from "${getBootstrapURL()}";` - const declaration = `const functions = {}; const metadata = {};` + const declaration = `const functions = {}; const metadata = { functions: {} };` const imports = functions.map((func) => { const url = pathToFileURL(func.path) const metadata = { @@ -121,7 +121,7 @@ const getLocalEntryPoint = ( if (typeof func === "function") { functions["${func.name}"] = func; - metadata["${func.name}"] = ${JSON.stringify(metadata)} + metadata.functions["${func.name}"] = ${JSON.stringify(metadata)} } else { console.log(${JSON.stringify(formatExportTypeError(func.name))}); } diff --git a/package.json b/package.json index ea62a0f7..1b2150cc 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "test:ci": "run-s build test:ci:*", "test:dev:ava": "ava", "test:dev:deno": "deno test --allow-all test/deno", - "test:ci:ava": "nyc -r lcovonly -r text -r json ava" + "test:ci:ava": "nyc -r lcovonly -r text -r json ava", + "test:ci:deno": "deno test --allow-all test/deno" }, "config": { "eslint": "--ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{node,scripts,.github}/**/*.{js,ts,md,html}\" \"*.{js,ts,md,html}\"", diff --git a/test/deno/stage2.test.ts b/test/deno/stage2.test.ts index 1c113a95..cefc87fc 100644 --- a/test/deno/stage2.test.ts +++ b/test/deno/stage2.test.ts @@ -52,6 +52,6 @@ Deno.test('`getStage2Entry` returns a valid stage 2 file', async () => { const result = await mod.functions[func.name]() assertEquals(await result.text(), func.response) - assertEquals(mod.metadata[func.name].url, pathToFileURL(func.path).toString()) + assertEquals(mod.metadata.functions[func.name].url, pathToFileURL(func.path).toString()) } }) diff --git a/test/node/stage_2.ts b/test/node/stage_2.ts index 17428fd1..45c2f3c8 100644 --- a/test/node/stage_2.ts +++ b/test/node/stage_2.ts @@ -59,7 +59,7 @@ test('`getLocalEntryPoint` returns a valid stage 2 file for local development', for (const func of functions) { t.is(responses[func.name], func.response) - t.is(metadata[func.name].url, pathToFileURL(func.path).toString()) + t.is(metadata.functions[func.name].url, pathToFileURL(func.path).toString()) } await del(tmpDir, { force: true }) From ac60aae324e2bac6e267b57aa80ac85dc063f097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Fri, 16 Sep 2022 15:23:37 +0100 Subject: [PATCH 2/2] chore: use file URL in test --- test/deno/stage2.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/deno/stage2.test.ts b/test/deno/stage2.test.ts index cefc87fc..79efe3ea 100644 --- a/test/deno/stage2.test.ts +++ b/test/deno/stage2.test.ts @@ -41,10 +41,11 @@ Deno.test('`getStage2Entry` returns a valid stage 2 file', async () => { const normalizedStage2 = stage2.replaceAll(virtualRoot, `${baseURL.href}/`) const stage2Path = join(directory, 'stage2.ts') + const stage2URL = pathToFileURL(stage2Path) await Deno.writeTextFile(stage2Path, normalizedStage2) - const mod = await import(stage2Path) + const mod = await import(stage2URL.href) await Deno.remove(directory, { recursive: true })