From 188ab4a9677fe3ee6ec24baf05734c8fb4ab0abc Mon Sep 17 00:00:00 2001 From: Ewan Valentine Date: Tue, 14 Jun 2022 15:47:49 +0100 Subject: [PATCH 1/2] feat: return generated manifest object --- src/bundler.ts | 85 +++++++++++++++++++++++++++++++++++-------------- src/manifest.ts | 6 ++-- test/bundler.ts | 1 + 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/src/bundler.ts b/src/bundler.ts index 27a2895c..2a884e6e 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,6 +25,54 @@ interface BundleOptions { onBeforeDownload?: LifecycleHook } +interface BundleOpsArgs { + buildID: string + debug?: boolean + deno: DenoBridge + basePath: string + distDirectory: string + functions: EdgeFunction[] + importMap: ImportMap + featureFlags: Record +} + +const createBundleOps = ({ + basePath, + buildID, + debug, + deno, + distDirectory, + functions, + importMap, + featureFlags, +}: BundleOpsArgs) => { + const bundleOps = [ + bundleJS({ + buildID, + debug, + deno, + distDirectory, + functions, + importMap, + }), + ] + + if (featureFlags.edge_functions_produce_eszip) { + bundleOps.push( + bundleESZIP({ + basePath, + buildID, + debug, + deno, + distDirectory, + functions, + }), + ) + } + + return bundleOps +} + const bundle = async ( sourceDirectories: string[], distDirectory: string, @@ -56,29 +105,17 @@ const bundle = async ( // if any. const importMap = new ImportMap(importMaps) const functions = await findFunctions(sourceDirectories) - const bundleOps = [ - bundleJS({ - buildID, - debug, - deno, - distDirectory, - functions, - importMap, - }), - ] - if (featureFlags.edge_functions_produce_eszip) { - bundleOps.push( - bundleESZIP({ - basePath, - buildID, - debug, - deno, - distDirectory, - functions, - }), - ) - } + const bundleOps = createBundleOps({ + basePath, + buildID, + debug, + deno, + distDirectory, + functions, + importMap, + featureFlags, + }) const bundles = await Promise.all(bundleOps) @@ -87,7 +124,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, @@ -98,7 +135,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 d92a9708..18af2ba9 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 }) }) From 614f4f69a9508e4ee77d0e6aaeee2c90988c2ceb Mon Sep 17 00:00:00 2001 From: Ewan Valentine Date: Wed, 15 Jun 2022 16:07:46 +0100 Subject: [PATCH 2/2] chore: updated options params --- src/bundler.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/bundler.ts b/src/bundler.ts index 2a884e6e..80a41515 100644 --- a/src/bundler.ts +++ b/src/bundler.ts @@ -25,17 +25,23 @@ interface BundleOptions { onBeforeDownload?: LifecycleHook } -interface BundleOpsArgs { +interface CommonBundleOptions { buildID: string debug?: boolean deno: DenoBridge - basePath: string distDirectory: string functions: EdgeFunction[] - importMap: ImportMap featureFlags: Record } +interface BundleJSOptions { + importMap: ImportMap +} + +interface BundleESZIPOptions { + basePath: string +} + const createBundleOps = ({ basePath, buildID, @@ -45,7 +51,7 @@ const createBundleOps = ({ functions, importMap, featureFlags, -}: BundleOpsArgs) => { +}: CommonBundleOptions & BundleJSOptions & BundleESZIPOptions) => { const bundleOps = [ bundleJS({ buildID,