From a90b480e6264877b0130239f236854e39d44f10b Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 19 Apr 2022 08:42:29 +0100 Subject: [PATCH 1/2] chore: typo in description --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ec70edb..2384d0c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@netlify/vite-plugin-netlify-edge", "version": "0.0.1", - "description": "Vite support for Netlify Edge Function", + "description": "Vite support for Netlify Edge Functions", "main": "./dist/index.js", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", From c3beb84fba5d7f33115d987c153e6e5dc9b8fc31 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Wed, 11 May 2022 11:25:20 +0100 Subject: [PATCH 2/2] feat: allow custom function name --- README.md | 26 ++++++++++++++++++++------ src/index.ts | 26 +++++++++++++++----------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 82bd487..f659d6f 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,6 @@ 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. -By default, it sets `outDir` to `.netlify/edge-functions/handler`, and generates an Edge Functions manifest that defines the `handler` function for all requests. - -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. - # Usage Install the plugin: @@ -26,7 +22,25 @@ export default defineConfig({ }) ``` -You can disable any of these features by passing options to the `netlifyEdge()` function: +By default, it sets `outDir` to `.netlify/edge-functions/handler`, and generates an Edge Functions manifest that defines the `handler` function for all requests. +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. + +```js +// vite.config.js + +// ... +export default defineConfig({ + plugins: [netlifyEdge({ functionName: 'server' })], +}) +``` + +This generates the file inside `.netlify/edge-functions/server`, and creates a manifest pointing to the `server` function. + +### Static file handling + +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. + +You can disable any of this feature by passing options to the `netlifyEdge()` function: ```js // vite.config.js @@ -92,4 +106,4 @@ You can then build it using the vite CLI: vite build --ssr handler.js ``` -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. +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. diff --git a/src/index.ts b/src/index.ts index 9fb89c2..0b1d002 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,34 +8,36 @@ export interface NetlifyEdgePluginOptions { generateStaticManifest?: boolean generateEdgeFunctionsManifest?: boolean additionalStaticPaths?: Array + functionName?: string } +const staticManifestModuleId = '@static-manifest' +const resolvedStaticManifestModuleId = '\0' + staticManifestModuleId +const edgeFunctionsDir = '.netlify/edge-functions' +const DEFAULT_FUNCTION_NAME = 'handler' + const netlifyEdge = ({ generateStaticManifest = true, generateEdgeFunctionsManifest = true, additionalStaticPaths = [], + functionName = DEFAULT_FUNCTION_NAME, }: NetlifyEdgePluginOptions = {}): Plugin => { let resolvedConfig: ResolvedConfig let originalPublicDir: string - const staticManifestModuleId = '@static-manifest' - const resolvedStaticManifestModuleId = '\0' + staticManifestModuleId - const edgeFunctionsDir = '.netlify/edge-functions/handler' return { name: 'vite-plugin-netlify-edge', config(config) { if (config.build?.ssr) { originalPublicDir = config.publicDir || 'public' - config.build.outDir ||= edgeFunctionsDir + config.build.outDir ||= path.join(edgeFunctionsDir, functionName) return { publicDir: false, + // The types for `ssr` are omitted because it's marked as alpha, but it's still used ssr: { target: 'webworker', noExternal: true, }, - output: { - format: 'es', - }, build: { rollupOptions: { output: { @@ -73,15 +75,17 @@ const netlifyEdge = ({ if ( generateEdgeFunctionsManifest && resolvedConfig.build.ssr && - options.dir?.endsWith(edgeFunctionsDir) + // Edge Functions can either be in a subdirectory or directly in the edge functions dir + (options.dir?.endsWith(edgeFunctionsDir) || + options.dir?.endsWith(path.join(edgeFunctionsDir, functionName))) ) { const manifest = { - functions: [{ function: 'handler', path: '/*' }], + functions: [{ function: functionName, path: '/*' }], version: 1, } - // Write the manifest to the parent directory of the output directory + // Write the manifest to the edge functions directory fs.writeFileSync( - path.resolve(options.dir, '..', 'manifest.json'), + path.join(resolvedConfig.root, edgeFunctionsDir, 'manifest.json'), JSON.stringify(manifest), 'utf-8' )