Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return generated manifest object #48

Merged
merged 6 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 61 additions & 24 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<string, string>
}

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
}

EwanValentine marked this conversation as resolved.
Show resolved Hide resolved
const bundle = async (
sourceDirectories: string[],
distDirectory: string,
Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand All @@ -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) => {
Expand Down
6 changes: 4 additions & 2 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
1 change: 1 addition & 0 deletions test/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
Expand Down