Skip to content

Commit

Permalink
Merge pull request #16 from netlify/mk/clean-config
Browse files Browse the repository at this point in the history
feat: allow custom function name
  • Loading branch information
ascorbic authored May 11, 2022
2 parents ffb1e23 + c3beb84 commit c766cd0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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.
26 changes: 15 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,36 @@ export interface NetlifyEdgePluginOptions {
generateStaticManifest?: boolean
generateEdgeFunctionsManifest?: boolean
additionalStaticPaths?: Array<string>
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: {
Expand Down Expand Up @@ -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'
)
Expand Down

0 comments on commit c766cd0

Please sign in to comment.