Skip to content
This repository was archived by the owner on May 2, 2022. It is now read-only.

Add support for build.edge_handlers #57

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules/
.eslintcache
.nyc_output
coverage
# Local Netlify folder
.netlify
2 changes: 0 additions & 2 deletions integration-test/.gitignore

This file was deleted.

3 changes: 0 additions & 3 deletions manifest.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
name: "@netlify/plugin-edge-handlers"
inputs:
- name: sourceDir
default: "edge-handlers"
22 changes: 11 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const uploadBundle = require("./upload");
* Generates an entrypoint for bundling the handlers
* It also makes sure all handlers are registered with the runtime
*
* @param {string} sourceDir path to the edge handler directory
* @param {string} EDGE_HANDLERS_SRC path to the edge handler directory
* @returns {Promise<{ handlers: string[], mainFile: string }>} list of handlers and path to entrypoint
*/
async function assemble(sourceDir) {
const entries = await fsPromises.readdir(sourceDir, { withFileTypes: true });
async function assemble(EDGE_HANDLERS_SRC) {
const entries = await fsPromises.readdir(EDGE_HANDLERS_SRC, { withFileTypes: true });
const handlers = entries.filter(isHandlerFile).map(getFilename);

if (handlers.length === 0) {
Expand All @@ -36,7 +36,7 @@ async function assemble(sourceDir) {
for (const handler of handlers) {
const id = "func" + crypto.randomBytes(16).toString("hex");

imports.push(`import * as ${id} from "${path.resolve(sourceDir, handler)}";`);
imports.push(`import * as ${id} from "${path.resolve(EDGE_HANDLERS_SRC, handler)}";`);
registration.push(`netlifyRegistry.set("${handler}", ${id});`);
}
const mainContents = imports.concat(registration).join("\n");
Expand Down Expand Up @@ -154,26 +154,26 @@ async function publishBundle(bundle, handlers, outputDir, isLocal, apiToken) {
}
}

function logHandlers(handlers, sourceDir) {
function logHandlers(handlers, EDGE_HANDLERS_SRC) {
const handlersString = handlers.map(serializeHandler).join("\n");
console.log(`Packaging Edge handlers from ${sourceDir} directory:\n${handlersString}`);
console.log(`Packaging Edge handlers from ${EDGE_HANDLERS_SRC} directory:\n${handlersString}`);
}

function serializeHandler(handler) {
return ` - ${handler}`;
}

module.exports = {
onPostBuild: async ({ inputs: { sourceDir }, constants, utils }) => {
const { mainFile, handlers } = await assemble(sourceDir);
onPostBuild: async ({ constants: { IS_LOCAL, NETLIFY_API_TOKEN, EDGE_HANDLERS_SRC }, utils }) => {
const { mainFile, handlers } = await assemble(EDGE_HANDLERS_SRC);

if (handlers.length === 0) {
console.log(`No Edge handlers were found in ${sourceDir} directory`);
console.log(`No Edge handlers were found in ${EDGE_HANDLERS_SRC} directory`);
return;
}

logHandlers(handlers, sourceDir);
logHandlers(handlers, EDGE_HANDLERS_SRC);
const bundle = await bundleFunctions(mainFile, utils);
await publishBundle(bundle, handlers, LOCAL_OUT_DIR, constants.IS_LOCAL, constants.NETLIFY_API_TOKEN);
await publishBundle(bundle, handlers, LOCAL_OUT_DIR, IS_LOCAL, NETLIFY_API_TOKEN);
},
};
1 change: 1 addition & 0 deletions test/fixtures/config-dir/custom-edge-handlers/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function onRequest() {}
5 changes: 5 additions & 0 deletions test/fixtures/config-dir/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
edge_handlers = "custom-edge-handlers"

[[plugins]]
package = "../../.."
9 changes: 9 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { callHandler } = require("./helpers/handler");
const { runNetlifyBuild } = require("./helpers/run");

const INTEGRATION_TEST_DIR = `${__dirname}/../integration-test`;
const FIXTURES_DIR = `${__dirname}/fixtures`;
const CONFIG_FIXTURE_DIR = `${FIXTURES_DIR}/config-dir`;

test("Edge handlers should be bundled", async (t) => {
await runNetlifyBuild(t, INTEGRATION_TEST_DIR);
Expand All @@ -20,3 +22,10 @@ test("Edge handlers should be bundled", async (t) => {
});
t.deepEqual(helloWorld.logs, "1,2,3,4,379");
});

test("Edge handlers directory can be configured using build.edge_handlers", async (t) => {
await runNetlifyBuild(t, CONFIG_FIXTURE_DIR);

const { manifest } = loadBundle(t, CONFIG_FIXTURE_DIR);
t.deepEqual(manifest.handlers, ["example"]);
});