-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for multi-stage ESZIPs (#19)
* feat: add support for multi-stage ESZIPs * chore: format using Prettier * fix: add extension to import
- Loading branch information
1 parent
2e122c4
commit 2d78f5b
Showing
9 changed files
with
176 additions
and
140 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,31 +1,6 @@ | ||
import { load } from 'https://deno.land/x/eszip@v0.16.0/loader.ts' | ||
import { build, LoadResponse } from 'https://deno.land/x/eszip@v0.16.0/mod.ts' | ||
import { writeStage2 } from 'https://dinosaurs:are-the-future!@deploy-preview-20--edge-bootstrap.netlify.app/bundler/mod.ts' | ||
|
||
const IDENTIFIER_BUNDLE_COMBINED = 'netlify:bundle-combined' | ||
const [payload] = Deno.args | ||
const { basePath, destPath, functions } = JSON.parse(payload) | ||
|
||
const createLoader = (srcPath: string): ((specifier: string) => Promise<LoadResponse | undefined>) => { | ||
return async (specifier: string): Promise<LoadResponse | undefined> => { | ||
// If we're loading the combined bundle identifier, we override the loading | ||
// to read the file from disk and return the contents. | ||
if (specifier === IDENTIFIER_BUNDLE_COMBINED) { | ||
const content = await Deno.readTextFile(new URL(srcPath)) | ||
|
||
return { | ||
content, | ||
headers: { | ||
'content-type': 'application/typescript', | ||
}, | ||
kind: 'module', | ||
specifier, | ||
} | ||
} | ||
|
||
// Falling back to the default loading logic. | ||
return load(specifier) | ||
} | ||
} | ||
|
||
const [srcPath, destPath] = Deno.args | ||
const bytes = await build([IDENTIFIER_BUNDLE_COMBINED], createLoader(srcPath)) | ||
|
||
await Deno.writeFile(destPath, bytes) | ||
await writeStage2({ basePath, functions, destPath }) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { join, resolve } from 'path' | ||
|
||
import { DenoBridge } from '../bridge.js' | ||
import type { Bundle } from '../bundle.js' | ||
import { EdgeFunction } from '../edge_function.js' | ||
import { getFileHash } from '../utils/sha256.js' | ||
|
||
interface BundleESZIPOptions { | ||
basePath: string | ||
buildID: string | ||
debug?: boolean | ||
deno: DenoBridge | ||
distDirectory: string | ||
functions: EdgeFunction[] | ||
} | ||
|
||
const bundleESZIP = async ({ | ||
basePath, | ||
buildID, | ||
debug, | ||
deno, | ||
distDirectory, | ||
functions, | ||
}: BundleESZIPOptions): Promise<Bundle> => { | ||
const extension = '.eszip' | ||
const destPath = join(distDirectory, `${buildID}${extension}`) | ||
const bundler = getESZIPBundler() | ||
const payload = { | ||
basePath, | ||
destPath, | ||
functions, | ||
} | ||
const flags = ['--allow-all'] | ||
|
||
if (!debug) { | ||
flags.push('--quiet') | ||
} | ||
|
||
await deno.run(['run', ...flags, bundler, JSON.stringify(payload)]) | ||
|
||
const hash = await getFileHash(destPath) | ||
|
||
return { extension, format: 'eszip2', hash } | ||
} | ||
|
||
const getESZIPBundler = () => { | ||
const { pathname } = new URL(import.meta.url) | ||
const bundlerPath = resolve(pathname, '../../../deno/bundle.ts') | ||
|
||
return bundlerPath | ||
} | ||
|
||
export { bundleESZIP } |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { promises as fs } from 'fs' | ||
import { join } from 'path' | ||
|
||
import del from 'del' | ||
|
||
import { DenoBridge } from '../bridge.js' | ||
import type { Bundle } from '../bundle.js' | ||
import { EdgeFunction } from '../edge_function.js' | ||
import { generateEntryPoint } from '../entry_point.js' | ||
import { ImportMap } from '../import_map.js' | ||
import { getFileHash } from '../utils/sha256.js' | ||
|
||
interface BundleJSOptions { | ||
buildID: string | ||
debug?: boolean | ||
deno: DenoBridge | ||
distDirectory: string | ||
functions: EdgeFunction[] | ||
importMap: ImportMap | ||
} | ||
|
||
const bundleJS = async ({ | ||
buildID, | ||
debug, | ||
deno, | ||
distDirectory, | ||
functions, | ||
importMap, | ||
}: BundleJSOptions): Promise<Bundle> => { | ||
const stage2Path = await generateStage2(functions, distDirectory, `${buildID}-pre.js`) | ||
const extension = '.js' | ||
const jsBundlePath = join(distDirectory, `${buildID}${extension}`) | ||
const flags = [`--import-map=${importMap.toDataURL()}`] | ||
|
||
if (!debug) { | ||
flags.push('--quiet') | ||
} | ||
|
||
await deno.run(['bundle', ...flags, stage2Path, jsBundlePath]) | ||
await fs.unlink(stage2Path) | ||
|
||
const hash = await getFileHash(jsBundlePath) | ||
|
||
return { extension, format: 'js', hash } | ||
} | ||
|
||
const generateStage2 = async (functions: EdgeFunction[], distDirectory: string, fileName: string) => { | ||
await del(distDirectory, { force: true }) | ||
await fs.mkdir(distDirectory, { recursive: true }) | ||
|
||
const entrypoint = generateEntryPoint(functions) | ||
const stage2Path = join(distDirectory, fileName) | ||
|
||
await fs.writeFile(stage2Path, entrypoint) | ||
|
||
return stage2Path | ||
} | ||
|
||
export { bundleJS, generateStage2 } |
Oops, something went wrong.