diff --git a/src/bundler.ts b/src/bundler.ts index 4c096bcc..1dada348 100644 --- a/src/bundler.ts +++ b/src/bundler.ts @@ -7,6 +7,7 @@ import { v4 as uuidv4 } from 'uuid' import { DenoBridge, LifecycleHook } from './bridge.js' import type { Bundle } from './bundle.js' import type { Declaration } from './declaration.js' +import { EdgeFunction } from './edge_function.js' import { FeatureFlags, getFlags } from './feature_flags.js' import { findFunctions } from './finder.js' import { bundle as bundleESZIP } from './formats/eszip.js' @@ -24,7 +25,56 @@ interface BundleOptions { onBeforeDownload?: LifecycleHook } -// eslint-disable-next-line max-statements +interface BundleFormatOptions { + buildID: string + debug?: boolean + deno: DenoBridge + distDirectory: string + functions: EdgeFunction[] + featureFlags: Record + importMap: ImportMap + basePath: string +} + +const createBundleOps = ({ + basePath, + buildID, + debug, + deno, + distDirectory, + functions, + importMap, + featureFlags, +}: BundleFormatOptions) => { + const bundleOps = [] + + if (featureFlags.edge_functions_produce_eszip) { + bundleOps.push( + bundleESZIP({ + basePath, + buildID, + debug, + deno, + distDirectory, + functions, + }), + ) + } else { + bundleOps.push( + bundleJS({ + buildID, + debug, + deno, + distDirectory, + functions, + importMap, + }), + ) + } + + return bundleOps +} + const bundle = async ( sourceDirectories: string[], distDirectory: string, @@ -57,31 +107,17 @@ const bundle = async ( // if any. const importMap = new ImportMap(importMaps) const functions = await findFunctions(sourceDirectories) - const bundleOps = [] - if (featureFlags.edge_functions_produce_eszip) { - bundleOps.push( - bundleESZIP({ - basePath, - buildID, - debug, - deno, - distDirectory, - functions, - }), - ) - } else { - bundleOps.push( - bundleJS({ - buildID, - debug, - deno, - distDirectory, - functions, - importMap, - }), - ) - } + const bundleOps = createBundleOps({ + basePath, + buildID, + debug, + deno, + distDirectory, + functions, + importMap, + featureFlags, + }) const bundles = await Promise.all(bundleOps) @@ -90,7 +126,7 @@ const bundle = async ( // rename the bundles to their permanent names. await createFinalBundles(bundles, distDirectory, buildID) - await writeManifest({ + const manifest = await writeManifest({ bundles, declarations, distDirectory, @@ -101,7 +137,7 @@ const bundle = async ( await importMap.writeToFile(distImportMapPath) } - return { functions } + return { functions, manifest } } const createFinalBundles = async (bundles: Bundle[], distDirectory: string, buildID: string) => { diff --git a/src/manifest.ts b/src/manifest.ts index bd3213b1..20fbb69f 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -75,11 +75,13 @@ interface WriteManifestOptions { functions: EdgeFunction[] } -const writeManifest = ({ bundles, declarations = [], distDirectory, functions }: WriteManifestOptions) => { +const writeManifest = async ({ bundles, declarations = [], distDirectory, functions }: WriteManifestOptions) => { const manifest = generateManifest({ bundles, declarations, functions }) const manifestPath = join(distDirectory, 'manifest.json') - return fs.writeFile(manifestPath, JSON.stringify(manifest)) + await fs.writeFile(manifestPath, JSON.stringify(manifest)) + + return manifest } export { generateManifest, Manifest, writeManifest } diff --git a/test/bundler.ts b/test/bundler.ts index 9445ce9e..7f6750ef 100644 --- a/test/bundler.ts +++ b/test/bundler.ts @@ -34,6 +34,7 @@ test('Produces a JavaScript bundle and a manifest file', async (t) => { t.is(bundles.length, 1) t.is(bundles[0].format, 'js') t.true(generatedFiles.includes(bundles[0].asset)) + t.deepEqual(result.manifest, manifest) await fs.rmdir(tmpDir.path, { recursive: true }) })