Skip to content

Commit

Permalink
feat: support route handlers (#106)
Browse files Browse the repository at this point in the history
* feat: function entry matcher utility

* test: function entry matcher

* chore: changeset

* chore: prefer forEach over test.each

* chore: clearer parameter names
  • Loading branch information
james-elicx authored Mar 17, 2023
1 parent 2b97f9d commit c6c8818
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-camels-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/next-on-pages": patch
---

Handle route handler function entries.
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { watch } from "chokidar";
import pLimit from "p-limit";
import acorn, { parse, Node } from "acorn";
import { generate } from "astring";
import { normalizePath } from "./utils";
import { matchFunctionEntry, normalizePath } from "./utils";

import { version as nextOnPagesVersion } from "../package.json";

Expand Down Expand Up @@ -400,10 +400,7 @@ const transform = async ({
}

for (const entry of functionsEntries) {
if (
`pages/${name}` === entry?.name ||
`app${name !== "index" ? `/${name}` : ""}/page` === entry?.name
) {
if (matchFunctionEntry(entry.name, name)) {
hydratedFunctions.set(name, { matchers: entry.matchers, filepath });
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { normalizePath } from "./paths";
export { normalizePath } from './paths';
export { matchFunctionEntry } from './transform';
1 change: 1 addition & 0 deletions src/utils/transform/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { matchFunctionEntry } from './manifest';
19 changes: 19 additions & 0 deletions src/utils/transform/manifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Check if a function file name matches an entry in the manifest.
*
* @param entryName Manifest entry name.
* @param fileName Function file name.
* @returns Whether the function file name matches the manifest entry name.
*/
export const matchFunctionEntry = (entryName: string, fileName: string) => {
// app directory
if (entryName.startsWith('app/')) {
const type = entryName.endsWith('/route') ? '/route' : '/page';
return (
`app${fileName !== 'index' ? `/${fileName}` : ''}${type}` === entryName
);
}

// pages directory
return entryName.startsWith('pages/') && `pages/${fileName}` === entryName;
};
21 changes: 21 additions & 0 deletions tests/manifest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, test, expect, suite } from 'vitest';
import { matchFunctionEntry } from '../src/utils';

const manifestEntries = [
{ name: 'app dir - normal page', entry: 'app/test/page', file: 'test' },
{ name: 'app dir - dynamic page', entry: 'app/[id]/page', file: '[id]' },
{ name: 'app dir - nested page', entry: 'app/1/2/3/page', file: '1/2/3' },
{ name: 'app dir - index page', entry: 'app/page', file: 'index' },
{ name: 'app dir - route handler', entry: 'app/test/route', file: 'test' },
{ name: 'pages dir - page', entry: 'pages/api/hello', file: 'api/hello' },
];

suite('manifest utils', () => {
describe('match function entries', () => {
manifestEntries.forEach(({ name, entry, file }) => {
test(name, () => {
expect(matchFunctionEntry(entry, file)).toEqual(true);
});
});
});
});

0 comments on commit c6c8818

Please sign in to comment.