Skip to content

Commit

Permalink
feat: gate ESZIP bundling with environment variable (netlify/edge-bun…
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas authored Mar 3, 2022
1 parent 89d7fb3 commit cd3eb3b
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions packages/edge-bundler/src/bundler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { promises as fs } from 'fs'
import { join, relative } from 'path'
import { env } from 'process'
import { pathToFileURL } from 'url'

import { DenoBridge, LifecycleHook } from './bridge.js'
Expand Down Expand Up @@ -33,9 +34,15 @@ const bundle = async (
})

const { entrypointHash, handlers, preBundlePath } = await preBundle(sourceDirectories, distDirectory)
const preBundleFileURL = pathToFileURL(preBundlePath).toString()
const bundlePath = join(distDirectory, entrypointHash)
const bundleAlternates: BundleAlternate[] = ['js']
const bundleOps = [bundleJS(deno, preBundlePath, bundlePath)]

if (env.BUNDLE_ESZIP) {
bundleAlternates.push('eszip2')
bundleOps.push(bundleESZIP(deno, preBundlePath, bundlePath))
}

const bundleAlternates: BundleAlternate[] = ['js', 'eszip2']
const manifest = await writeManifest({
bundleAlternates,
bundlePath: entrypointHash,
Expand All @@ -44,19 +51,26 @@ const bundle = async (
handlers,
})

const eszipBundlePath = join(distDirectory, `${entrypointHash}.eszip2`)
const jsBundlePath = join(distDirectory, `${entrypointHash}.js`)

await Promise.all([
deno.run(['run', '-A', getESZIPBundler(), preBundleFileURL, eszipBundlePath]),
deno.run(['bundle', preBundlePath, jsBundlePath]),
])

await Promise.all(bundleOps)
await fs.unlink(preBundlePath)

return { handlers, manifest, preBundlePath }
}

const bundleESZIP = (deno: DenoBridge, preBundlePath: string, bundlePath: string) => {
const preBundleFileURL = pathToFileURL(preBundlePath).toString()
const eszipBundlePath = `${bundlePath}.eszip2`
const bundler = getESZIPBundler()

return deno.run(['run', '-A', bundler, preBundleFileURL, eszipBundlePath])
}

const bundleJS = (deno: DenoBridge, preBundlePath: string, bundlePath: string) => {
const jsBundlePath = `${bundlePath}.js`

return deno.run(['bundle', preBundlePath, jsBundlePath])
}

const generateEntrypoint = (handlers: Handler[], distDirectory: string) => {
const lines = handlers.map((handler, index) => generateHandlerReference(handler, index, distDirectory))
const bootImport = 'import { boot } from "https://dinosaurs:[email protected]/index.ts";'
Expand Down

0 comments on commit cd3eb3b

Please sign in to comment.