Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/plugin-router-watch-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@granite-js/plugin-router": patch
---

Preserve the default route file watcher when router options are empty.
30 changes: 30 additions & 0 deletions packages/plugin-router/src/routerPlugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { router } from './routerPlugin';

const mocks = vi.hoisted(() => ({
generateRouterFile: vi.fn(),
watchRouter: vi.fn(),
}));

vi.mock('./generateRouterFile', () => ({
generateRouterFile: mocks.generateRouterFile,
}));

vi.mock('./watchRouter', () => ({
watchRouter: mocks.watchRouter,
}));

describe('router', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('watches route files by default when options are empty', () => {
const plugin = router({});

plugin.dev?.handler.call({ meta: {} }, {} as never);

expect(mocks.generateRouterFile).toHaveBeenCalledOnce();
expect(mocks.watchRouter).toHaveBeenCalledOnce();
});
});
4 changes: 3 additions & 1 deletion packages/plugin-router/src/routerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const DEFAULT_OPTIONS: Required<RouterPluginOptions> = {
};

export const router = (options: RouterPluginOptions = DEFAULT_OPTIONS): GranitePluginCore => {
const resolvedOptions = { ...DEFAULT_OPTIONS, ...options };

return {
name: 'router-plugin',
build: {
Expand All @@ -23,7 +25,7 @@ export const router = (options: RouterPluginOptions = DEFAULT_OPTIONS): GraniteP
order: 'pre',
handler: () => {
generateRouterFile();
if (options.watch) {
if (resolvedOptions.watch) {
watchRouter();
}
},
Expand Down
Loading