@netlify/edge-bundler
Advanced tools
Comparing version 2.8.0 to 2.9.0
@@ -81,3 +81,3 @@ import { promises as fs } from 'fs'; | ||
} | ||
return getFunctionConfig(func, deno, logger); | ||
return getFunctionConfig(func, importMap, deno, logger); | ||
})); | ||
@@ -84,0 +84,0 @@ // Creating a hash of function names to configuration objects. |
import { DenoBridge } from './bridge.js'; | ||
import { EdgeFunction } from './edge_function.js'; | ||
import { ImportMap } from './import_map.js'; | ||
import { Logger } from './logger.js'; | ||
export declare const enum Mode { | ||
BeforeCache = "before-cache", | ||
AfterCache = "after-cache" | ||
} | ||
export interface FunctionConfig { | ||
mode?: Mode; | ||
path?: string; | ||
} | ||
export declare const getFunctionConfig: (func: EdgeFunction, deno: DenoBridge, log: Logger) => Promise<FunctionConfig>; | ||
export declare const getFunctionConfig: (func: EdgeFunction, importMap: ImportMap, deno: DenoBridge, log: Logger) => Promise<FunctionConfig>; |
@@ -22,3 +22,3 @@ import { promises as fs } from 'fs'; | ||
}; | ||
export const getFunctionConfig = async (func, deno, log) => { | ||
export const getFunctionConfig = async (func, importMap, deno, log) => { | ||
// The extractor is a Deno script that will import the function and run its | ||
@@ -42,2 +42,3 @@ // `config` export, if one exists. | ||
'--quiet', | ||
`--import-map=${importMap.toDataURL()}`, | ||
extractorPath, | ||
@@ -44,0 +45,0 @@ pathToFileURL(func.path).href, |
import { promises as fs } from 'fs'; | ||
import { join, resolve } from 'path'; | ||
import { pathToFileURL } from 'url'; | ||
import del from 'del'; | ||
@@ -11,2 +12,9 @@ import { stub } from 'sinon'; | ||
import { getFunctionConfig } from './config.js'; | ||
import { ImportMap } from './import_map.js'; | ||
const importMapFile = { | ||
baseURL: new URL('file:///some/path/import-map.json'), | ||
imports: { | ||
'alias:helper': pathToFileURL(join(fixturesDir, 'helper.ts')).toString(), | ||
}, | ||
}; | ||
test('`getFunctionConfig` extracts configuration properties from function file', async () => { | ||
@@ -105,3 +113,3 @@ const { path: tmpDir } = await tmp.dir(); | ||
path, | ||
}, deno, logger); | ||
}, new ImportMap([importMapFile]), deno, logger); | ||
expect(config).toEqual(func.expectedConfig); | ||
@@ -127,5 +135,6 @@ if (func.userLog) { | ||
}, | ||
importMaps: [importMapFile], | ||
}); | ||
const generatedFiles = await fs.readdir(tmpDir.path); | ||
expect(result.functions.length).toBe(4); | ||
expect(result.functions.length).toBe(6); | ||
expect(generatedFiles.length).toBe(2); | ||
@@ -161,13 +170,14 @@ const manifestFile = await fs.readFile(resolve(tmpDir.path, 'manifest.json'), 'utf8'); | ||
}, | ||
importMaps: [importMapFile], | ||
}); | ||
const generatedFiles = await fs.readdir(tmpDir.path); | ||
expect(result.functions.length).toBe(4); | ||
expect(result.functions.length).toBe(6); | ||
expect(generatedFiles.length).toBe(2); | ||
const manifestFile = await fs.readFile(resolve(tmpDir.path, 'manifest.json'), 'utf8'); | ||
const manifest = JSON.parse(manifestFile); | ||
const { bundles, routes } = manifest; | ||
const { bundles, routes, post_cache_routes: postCacheRoutes } = manifest; | ||
expect(bundles.length).toBe(1); | ||
expect(bundles[0].format).toBe('eszip2'); | ||
expect(generatedFiles.includes(bundles[0].asset)).toBe(true); | ||
expect(routes.length).toBe(4); | ||
expect(routes.length).toBe(5); | ||
expect(routes[0]).toEqual({ function: 'framework-func2', pattern: '^/framework-func2/?$' }); | ||
@@ -177,3 +187,6 @@ expect(routes[1]).toEqual({ function: 'user-func2', pattern: '^/user-func2/?$' }); | ||
expect(routes[3]).toEqual({ function: 'user-func1', pattern: '^/user-func1/?$' }); | ||
expect(routes[4]).toEqual({ function: 'user-func3', pattern: '^/user-func3/?$' }); | ||
expect(postCacheRoutes.length).toBe(1); | ||
expect(postCacheRoutes[0]).toEqual({ function: 'user-func4', pattern: '^/user-func4/?$' }); | ||
await fs.rmdir(tmpDir.path, { recursive: true }); | ||
}); |
import { FunctionConfig } from './config.js'; | ||
interface BaseDeclaration { | ||
function: string; | ||
mode?: string; | ||
name?: string; | ||
@@ -5,0 +6,0 @@ } |
@@ -12,3 +12,3 @@ export const getDeclarationsFromConfig = (tomlDeclarations, functionsConfig) => { | ||
functionsVisited.add(declaration.function); | ||
declarations.push({ function: declaration.function, path }); | ||
declarations.push({ ...declaration, path }); | ||
} | ||
@@ -22,5 +22,5 @@ else { | ||
for (const name in functionsConfig) { | ||
const { path } = functionsConfig[name]; | ||
const { path, ...config } = functionsConfig[name]; | ||
if (!functionsVisited.has(name) && path) { | ||
declarations.push({ function: name, path }); | ||
declarations.push({ ...config, function: name, path }); | ||
} | ||
@@ -27,0 +27,0 @@ } |
@@ -20,2 +20,7 @@ import type { Bundle } from './bundle.js'; | ||
}[]; | ||
post_cache_routes: { | ||
function: string; | ||
name?: string; | ||
pattern: string; | ||
}[]; | ||
} | ||
@@ -22,0 +27,0 @@ declare const generateManifest: ({ bundles, declarations, functions }: GenerateManifestOptions) => Manifest; |
@@ -7,3 +7,5 @@ import { promises as fs } from 'fs'; | ||
const generateManifest = ({ bundles = [], declarations = [], functions }) => { | ||
const routes = declarations.map((declaration) => { | ||
const preCacheRoutes = []; | ||
const postCacheRoutes = []; | ||
declarations.forEach((declaration) => { | ||
const func = functions.find(({ name }) => declaration.function === name); | ||
@@ -15,3 +17,3 @@ if (func === undefined) { | ||
const serializablePattern = pattern.source.replace(/\\\//g, '/'); | ||
return { | ||
const route = { | ||
function: func.name, | ||
@@ -21,2 +23,8 @@ name: declaration.name, | ||
}; | ||
if (declaration.mode === "after-cache" /* Mode.AfterCache */) { | ||
postCacheRoutes.push(route); | ||
} | ||
else { | ||
preCacheRoutes.push(route); | ||
} | ||
}); | ||
@@ -29,3 +37,4 @@ const manifestBundles = bundles.map(({ extension, format, hash }) => ({ | ||
bundles: manifestBundles, | ||
routes: routes.filter(nonNullable), | ||
routes: preCacheRoutes.filter(nonNullable), | ||
post_cache_routes: postCacheRoutes.filter(nonNullable), | ||
bundler_version: getPackageVersion(), | ||
@@ -32,0 +41,0 @@ }; |
@@ -83,1 +83,37 @@ import { env } from 'process'; | ||
}); | ||
test('Generates a manifest with pre and post-cache routes', () => { | ||
const bundle1 = { | ||
extension: '.ext1', | ||
format: 'format1', | ||
hash: '123456', | ||
}; | ||
const bundle2 = { | ||
extension: '.ext2', | ||
format: 'format2', | ||
hash: '654321', | ||
}; | ||
const functions = [ | ||
{ name: 'func-1', path: '/path/to/func-1.ts' }, | ||
{ name: 'func-2', path: '/path/to/func-2.ts' }, | ||
{ name: 'func-3', path: '/path/to/func-3.ts' }, | ||
]; | ||
const declarations = [ | ||
{ function: 'func-1', path: '/f1' }, | ||
{ function: 'func-2', mode: 'not_a_supported_mode', path: '/f2' }, | ||
{ function: 'func-3', mode: 'after-cache', path: '/f3' }, | ||
]; | ||
const manifest = generateManifest({ bundles: [bundle1, bundle2], declarations, functions }); | ||
const expectedBundles = [ | ||
{ asset: bundle1.hash + bundle1.extension, format: bundle1.format }, | ||
{ asset: bundle2.hash + bundle2.extension, format: bundle2.format }, | ||
]; | ||
const expectedPreCacheRoutes = [ | ||
{ function: 'func-1', name: undefined, pattern: '^/f1/?$' }, | ||
{ function: 'func-2', name: undefined, pattern: '^/f2/?$' }, | ||
]; | ||
const expectedPostCacheRoutes = [{ function: 'func-3', name: undefined, pattern: '^/f3/?$' }]; | ||
expect(manifest.bundles).toEqual(expectedBundles); | ||
expect(manifest.routes).toEqual(expectedPreCacheRoutes); | ||
expect(manifest.post_cache_routes).toEqual(expectedPostCacheRoutes); | ||
expect(manifest.bundler_version).toBe(env.npm_package_version); | ||
}); |
{ | ||
"name": "@netlify/edge-bundler", | ||
"version": "2.8.0", | ||
"version": "2.9.0", | ||
"description": "Intelligently prepare Netlify Edge Functions for deployment", | ||
@@ -5,0 +5,0 @@ "type": "module", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2994324
6127