-
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: inline stage 2 ESZIP bundler (#102)
- Loading branch information
1 parent
7d12ec2
commit 5df5291
Showing
4 changed files
with
133 additions
and
1 deletion.
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
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,47 @@ | ||
import { load } from "https://deno.land/x/[email protected]/loader.ts"; | ||
import { LoadResponse } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import * as path from "https://deno.land/[email protected]/path/mod.ts"; | ||
import { retryAsync } from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
const inlineModule = ( | ||
specifier: string, | ||
content: string, | ||
): LoadResponse => { | ||
return { | ||
content, | ||
headers: { | ||
"content-type": "application/typescript", | ||
}, | ||
kind: "module", | ||
specifier, | ||
}; | ||
}; | ||
|
||
const loadFromVirtualRoot = async ( | ||
specifier: string, | ||
virtualRoot: string, | ||
basePath: string, | ||
) => { | ||
const basePathURL = path.toFileUrl(basePath).toString(); | ||
const filePath = specifier.replace(virtualRoot.slice(0, -1), basePathURL); | ||
const file = await load(filePath); | ||
|
||
if (file === undefined) { | ||
throw new Error(`Could not find file: ${filePath}`); | ||
} | ||
|
||
return { ...file, specifier }; | ||
}; | ||
|
||
const loadWithRetry = (specifier: string, delay = 1000, maxTry = 3) => { | ||
if (!specifier.startsWith("https://")) { | ||
return load(specifier); | ||
} | ||
|
||
return retryAsync(() => load(specifier), { | ||
delay, | ||
maxTry, | ||
}); | ||
}; | ||
|
||
export { inlineModule, loadFromVirtualRoot, loadWithRetry }; |
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,4 @@ | ||
export const PUBLIC_SPECIFIER = "netlify:edge"; | ||
export const STAGE1_SPECIFIER = "netlify:bootstrap-stage1"; | ||
export const STAGE2_SPECIFIER = "netlify:bootstrap-stage2"; | ||
export const virtualRoot = "file:///root/"; |
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,81 @@ | ||
import { build, LoadResponse } from 'https://deno.land/x/[email protected]/mod.ts' | ||
|
||
import * as path from 'https://deno.land/[email protected]/path/mod.ts' | ||
|
||
import { PUBLIC_SPECIFIER, STAGE2_SPECIFIER, virtualRoot } from './consts.ts' | ||
import { inlineModule, loadFromVirtualRoot, loadWithRetry } from './common.ts' | ||
|
||
interface InputFunction { | ||
name: string | ||
path: string | ||
} | ||
|
||
interface WriteStage2Options { | ||
basePath: string | ||
destPath: string | ||
functions: InputFunction[] | ||
imports?: Record<string, string> | ||
} | ||
|
||
const getFunctionReference = (basePath: string, func: InputFunction, index: number) => { | ||
const importName = `func${index}` | ||
const exportLine = `"${func.name}": ${importName}` | ||
const url = getVirtualPath(basePath, func.path) | ||
|
||
return { | ||
exportLine, | ||
importLine: `import ${importName} from "${url}";`, | ||
} | ||
} | ||
|
||
const getStage2Entry = (basePath: string, functions: InputFunction[]) => { | ||
const lines = functions.map((func, index) => getFunctionReference(basePath, func, index)) | ||
const importLines = lines.map(({ importLine }) => importLine).join('\n') | ||
const exportLines = lines.map(({ exportLine }) => exportLine).join(', ') | ||
const exportDeclaration = `export const functions = {${exportLines}};` | ||
|
||
return [importLines, exportDeclaration].join('\n\n') | ||
} | ||
|
||
const getVirtualPath = (basePath: string, filePath: string) => { | ||
const relativePath = path.relative(basePath, filePath) | ||
const url = new URL(relativePath, virtualRoot) | ||
|
||
return url | ||
} | ||
|
||
const stage2Loader = (basePath: string, functions: InputFunction[], imports: Record<string, string> = {}) => { | ||
return async (specifier: string): Promise<LoadResponse | undefined> => { | ||
if (specifier === STAGE2_SPECIFIER) { | ||
const stage2Entry = getStage2Entry(basePath, functions) | ||
|
||
return inlineModule(specifier, stage2Entry) | ||
} | ||
|
||
if (specifier === PUBLIC_SPECIFIER) { | ||
return { | ||
kind: 'external', | ||
specifier, | ||
} | ||
} | ||
|
||
if (imports[specifier] !== undefined) { | ||
return await loadWithRetry(imports[specifier]) | ||
} | ||
|
||
if (specifier.startsWith(virtualRoot)) { | ||
return loadFromVirtualRoot(specifier, virtualRoot, basePath) | ||
} | ||
|
||
return await loadWithRetry(specifier) | ||
} | ||
} | ||
|
||
const writeStage2 = async ({ basePath, destPath, functions, imports }: WriteStage2Options) => { | ||
const loader = stage2Loader(basePath, functions, imports) | ||
const bytes = await build([STAGE2_SPECIFIER], loader) | ||
|
||
return await Deno.writeFile(destPath, bytes) | ||
} | ||
|
||
export { writeStage2 } |