Skip to content

Commit

Permalink
feat: mark custom layers as externals (#225)
Browse files Browse the repository at this point in the history
* feat!: remove support for JavaScript bundles

* feat: add layers as externals
  • Loading branch information
eduardoboucas committed Nov 24, 2022
1 parent d8f45b1 commit a68607b
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions deno/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writeStage2 } from './lib/stage2.ts'

const [payload] = Deno.args
const { basePath, destPath, functions, importMapURL } = JSON.parse(payload)
const { basePath, destPath, externals, functions, importMapURL } = JSON.parse(payload)

await writeStage2({ basePath, destPath, functions, importMapURL })
await writeStage2({ basePath, destPath, externals, functions, importMapURL })
1 change: 0 additions & 1 deletion deno/lib/consts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const CUSTOM_LAYER_PREFIX = 'layer:'
export const PUBLIC_SPECIFIER = 'netlify:edge'
export const STAGE1_SPECIFIER = 'netlify:bootstrap-stage1'
export const STAGE2_SPECIFIER = 'netlify:bootstrap-stage2'
Expand Down
10 changes: 5 additions & 5 deletions deno/lib/stage2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { build, LoadResponse } from 'https://deno.land/x/eszip@v0.28.0/mod.ts'
import * as path from 'https://deno.land/std@0.127.0/path/mod.ts'

import type { InputFunction, WriteStage2Options } from '../../shared/stage2.ts'
import { CUSTOM_LAYER_PREFIX, PUBLIC_SPECIFIER, STAGE2_SPECIFIER, virtualRoot } from './consts.ts'
import { PUBLIC_SPECIFIER, STAGE2_SPECIFIER, virtualRoot } from './consts.ts'
import { inlineModule, loadFromVirtualRoot, loadWithRetry } from './common.ts'

interface FunctionReference {
Expand Down Expand Up @@ -62,15 +62,15 @@ const getVirtualPath = (basePath: string, filePath: string) => {
return url
}

const stage2Loader = (basePath: string, functions: InputFunction[]) => {
const stage2Loader = (basePath: string, functions: InputFunction[], externals: Set<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 || specifier.startsWith(CUSTOM_LAYER_PREFIX)) {
if (specifier === PUBLIC_SPECIFIER || externals.has(specifier)) {
return {
kind: 'external',
specifier,
Expand All @@ -85,8 +85,8 @@ const stage2Loader = (basePath: string, functions: InputFunction[]) => {
}
}

const writeStage2 = async ({ basePath, destPath, functions, importMapURL }: WriteStage2Options) => {
const loader = stage2Loader(basePath, functions)
const writeStage2 = async ({ basePath, destPath, externals, functions, importMapURL }: WriteStage2Options) => {
const loader = stage2Loader(basePath, functions, new Set(externals))
const bytes = await build([STAGE2_SPECIFIER], loader, importMapURL)
const directory = path.dirname(destPath)

Expand Down
2 changes: 1 addition & 1 deletion node/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ test('Processes a function that imports a custom layer', async () => {
path: '/func1',
},
]
const layer = { name: 'test', flag: 'edge-functions-layer-test' }
const layer = { name: 'layer:test', flag: 'edge-functions-layer-test' }
const result = await bundle([sourceDirectory], tmpDir.path, declarations, {
basePath: fixturesDir,
configPath: join(sourceDirectory, 'config.json'),
Expand Down
6 changes: 5 additions & 1 deletion node/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ const bundle = async (
// exists.
const deployConfig = await loadDeployConfig(configPath, logger)

// Layers are marked as externals in the ESZIP, so that those specifiers are
// not actually included in the bundle.
const externals = deployConfig.layers.map((layer) => layer.name)
const importMap = new ImportMap()

if (deployConfig.importMap) {
Expand All @@ -86,9 +89,10 @@ const bundle = async (
debug,
deno,
distDirectory,
externals,
functions,
importMap,
featureFlags,
importMap,
})

// The final file name of the bundles contains a SHA256 hash of the contents,
Expand Down
3 changes: 3 additions & 0 deletions node/formats/eszip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface BundleESZIPOptions {
debug?: boolean
deno: DenoBridge
distDirectory: string
externals: string[]
featureFlags: FeatureFlags
functions: EdgeFunction[]
importMap: ImportMap
Expand All @@ -28,6 +29,7 @@ const bundleESZIP = async ({
debug,
deno,
distDirectory,
externals,
functions,
importMap,
}: BundleESZIPOptions): Promise<Bundle> => {
Expand All @@ -37,6 +39,7 @@ const bundleESZIP = async ({
const payload: WriteStage2Options = {
basePath,
destPath,
externals,
functions,
importMapURL: importMap.toDataURL(),
}
Expand Down
1 change: 1 addition & 0 deletions shared/stage2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface InputFunction {
export interface WriteStage2Options {
basePath: string
destPath: string
externals: string[]
functions: InputFunction[]
importMapURL?: string
}
2 changes: 1 addition & 1 deletion test/fixtures/with_layers/functions/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"layers": [{ "name": "test", "flag": "edge-functions-layer-test" }],
"layers": [{ "name": "layer:test", "flag": "edge-functions-layer-test" }],
"version": 1
}

0 comments on commit a68607b

Please sign in to comment.