-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add feature flags and debug mode (netlify/edge-bundler#21)
* feat: add debug mode * feat: add feature flags * chore: debug * chore: fix test * fix: use correct ESZIP bundler path
- Loading branch information
1 parent
db14cb1
commit 5dc7f3e
Showing
9 changed files
with
144 additions
and
6 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
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 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,18 @@ | ||
const defaultFlags: Record<string, boolean> = { | ||
edge_functions_produce_eszip: false, | ||
} | ||
|
||
type FeatureFlag = keyof typeof defaultFlags | ||
type FeatureFlags = Record<FeatureFlag, boolean> | ||
|
||
const getFlags = (input: Record<string, boolean> = {}, flags = defaultFlags): Record<FeatureFlag, string> => | ||
Object.entries(flags).reduce( | ||
(result, [key, defaultValue]) => ({ | ||
...result, | ||
[key]: input[key] === undefined ? defaultValue : input[key], | ||
}), | ||
{}, | ||
) | ||
|
||
export { defaultFlags, getFlags } | ||
export type { FeatureFlag, FeatureFlags } |
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 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,73 @@ | ||
import { promises as fs } from 'fs' | ||
import { resolve } from 'path' | ||
import { fileURLToPath } from 'url' | ||
|
||
import test from 'ava' | ||
import tmp from 'tmp-promise' | ||
|
||
import { bundle } from '../src/bundler.js' | ||
|
||
const url = new URL(import.meta.url) | ||
const dirname = fileURLToPath(url) | ||
|
||
test('Produces a JavaScript bundle and a manifest file', async (t) => { | ||
const sourceDirectory = resolve(dirname, '..', 'fixtures', 'project_1', 'functions') | ||
const tmpDir = await tmp.dir() | ||
const declarations = [ | ||
{ | ||
function: 'func1', | ||
path: '/func1', | ||
}, | ||
] | ||
const result = await bundle([sourceDirectory], tmpDir.path, declarations) | ||
const generatedFiles = await fs.readdir(tmpDir.path) | ||
|
||
console.log({ result, generatedFiles }) | ||
|
||
t.is(result.functions.length, 1) | ||
t.is(generatedFiles.length, 2) | ||
|
||
// eslint-disable-next-line unicorn/prefer-json-parse-buffer | ||
const manifestFile = await fs.readFile(resolve(tmpDir.path, 'manifest.json'), 'utf8') | ||
const manifest = JSON.parse(manifestFile) | ||
const { bundles } = manifest | ||
|
||
t.is(bundles.length, 1) | ||
t.is(bundles[0].format, 'js') | ||
t.true(generatedFiles.includes(bundles[0].asset)) | ||
|
||
await fs.rmdir(tmpDir.path, { recursive: true }) | ||
}) | ||
|
||
test('Produces an additional ESZIP bundle when the `edge_functions_produce_eszip` feature flag is set', async (t) => { | ||
const sourceDirectory = resolve(dirname, '..', 'fixtures', 'project_1', 'functions') | ||
const tmpDir = await tmp.dir() | ||
const declarations = [ | ||
{ | ||
function: 'func1', | ||
path: '/func1', | ||
}, | ||
] | ||
const result = await bundle([sourceDirectory], tmpDir.path, declarations, { | ||
featureFlags: { | ||
edge_functions_produce_eszip: true, | ||
}, | ||
}) | ||
const generatedFiles = await fs.readdir(tmpDir.path) | ||
|
||
t.is(result.functions.length, 1) | ||
t.is(generatedFiles.length, 3) | ||
|
||
// eslint-disable-next-line unicorn/prefer-json-parse-buffer | ||
const manifestFile = await fs.readFile(resolve(tmpDir.path, 'manifest.json'), 'utf8') | ||
const manifest = JSON.parse(manifestFile) | ||
const { bundles } = manifest | ||
|
||
t.is(bundles.length, 2) | ||
t.is(bundles[0].format, 'js') | ||
t.true(generatedFiles.includes(bundles[0].asset)) | ||
t.is(bundles[1].format, 'eszip2') | ||
t.true(generatedFiles.includes(bundles[1].asset)) | ||
|
||
await fs.rmdir(tmpDir.path, { recursive: true }) | ||
}) |
1 change: 1 addition & 0 deletions
1
packages/edge-bundler/test/fixtures/project_1/functions/func1.ts
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 @@ | ||
export default async () => new Response('Hello') |