Skip to content

Commit c3beb84

Browse files
committed
feat: allow custom function name
1 parent 1f16af4 commit c3beb84

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
This plugin helps add support for generating Netlify Edge Functions. This is mostly intended for frameworks that need to generate a catch-all Edge Function to serve all requests.
44

5-
By default, it sets `outDir` to `.netlify/edge-functions/handler`, and generates an Edge Functions manifest that defines the `handler` function for all requests.
6-
7-
To help with handling static files, it registers a virtual module called `@static-manifest` that exports a `Set` that includes the paths of all files in `publicDir`. This can be used in the handler to identify requests for static files.
8-
95
# Usage
106

117
Install the plugin:
@@ -26,7 +22,25 @@ export default defineConfig({
2622
})
2723
```
2824

29-
You can disable any of these features by passing options to the `netlifyEdge()` function:
25+
By default, it sets `outDir` to `.netlify/edge-functions/handler`, and generates an Edge Functions manifest that defines the `handler` function for all requests.
26+
Passing a value to `functionName` will override this. This will affect the generated manifest and the base directory for the output, but it will not affect the names of the generated bundles. For this reason you should ensure that your entrypoint file is named the same as the function name.
27+
28+
```js
29+
// vite.config.js
30+
31+
// ...
32+
export default defineConfig({
33+
plugins: [netlifyEdge({ functionName: 'server' })],
34+
})
35+
```
36+
37+
This generates the file inside `.netlify/edge-functions/server`, and creates a manifest pointing to the `server` function.
38+
39+
### Static file handling
40+
41+
To help with handling static files, it registers a virtual module called `@static-manifest` that exports a `Set` that includes the paths of all files in `publicDir`. This can be used in the handler to identify requests for static files.
42+
43+
You can disable any of this feature by passing options to the `netlifyEdge()` function:
3044

3145
```js
3246
// vite.config.js
@@ -92,4 +106,4 @@ You can then build it using the vite CLI:
92106
vite build --ssr handler.js
93107
```
94108

95-
This will generate the Edge Function `.netlify/edge-functions/handler/index.js` and a manifest file `.netlify/edge-functions/manifest.json` that defines the `handler` function.
109+
This will generate the Edge Function `.netlify/edge-functions/handler/handler.js` and a manifest file `.netlify/edge-functions/manifest.json` that defines the `handler` function.

src/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,36 @@ export interface NetlifyEdgePluginOptions {
88
generateStaticManifest?: boolean
99
generateEdgeFunctionsManifest?: boolean
1010
additionalStaticPaths?: Array<string>
11+
functionName?: string
1112
}
1213

14+
const staticManifestModuleId = '@static-manifest'
15+
const resolvedStaticManifestModuleId = '\0' + staticManifestModuleId
16+
const edgeFunctionsDir = '.netlify/edge-functions'
17+
const DEFAULT_FUNCTION_NAME = 'handler'
18+
1319
const netlifyEdge = ({
1420
generateStaticManifest = true,
1521
generateEdgeFunctionsManifest = true,
1622
additionalStaticPaths = [],
23+
functionName = DEFAULT_FUNCTION_NAME,
1724
}: NetlifyEdgePluginOptions = {}): Plugin => {
1825
let resolvedConfig: ResolvedConfig
1926
let originalPublicDir: string
20-
const staticManifestModuleId = '@static-manifest'
21-
const resolvedStaticManifestModuleId = '\0' + staticManifestModuleId
22-
const edgeFunctionsDir = '.netlify/edge-functions/handler'
2327

2428
return {
2529
name: 'vite-plugin-netlify-edge',
2630
config(config) {
2731
if (config.build?.ssr) {
2832
originalPublicDir = config.publicDir || 'public'
29-
config.build.outDir ||= edgeFunctionsDir
33+
config.build.outDir ||= path.join(edgeFunctionsDir, functionName)
3034
return {
3135
publicDir: false,
36+
// The types for `ssr` are omitted because it's marked as alpha, but it's still used
3237
ssr: {
3338
target: 'webworker',
3439
noExternal: true,
3540
},
36-
output: {
37-
format: 'es',
38-
},
3941
build: {
4042
rollupOptions: {
4143
output: {
@@ -73,15 +75,17 @@ const netlifyEdge = ({
7375
if (
7476
generateEdgeFunctionsManifest &&
7577
resolvedConfig.build.ssr &&
76-
options.dir?.endsWith(edgeFunctionsDir)
78+
// Edge Functions can either be in a subdirectory or directly in the edge functions dir
79+
(options.dir?.endsWith(edgeFunctionsDir) ||
80+
options.dir?.endsWith(path.join(edgeFunctionsDir, functionName)))
7781
) {
7882
const manifest = {
79-
functions: [{ function: 'handler', path: '/*' }],
83+
functions: [{ function: functionName, path: '/*' }],
8084
version: 1,
8185
}
82-
// Write the manifest to the parent directory of the output directory
86+
// Write the manifest to the edge functions directory
8387
fs.writeFileSync(
84-
path.resolve(options.dir, '..', 'manifest.json'),
88+
path.join(resolvedConfig.root, edgeFunctionsDir, 'manifest.json'),
8589
JSON.stringify(manifest),
8690
'utf-8'
8791
)

0 commit comments

Comments
 (0)