-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: gate ESZIP bundling with environment variable (netlify/edge-bun…
- Loading branch information
1 parent
89d7fb3
commit cd3eb3b
Showing
1 changed file
with
24 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
@@ -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, | ||
|
@@ -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";' | ||
|