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
12 changes: 9 additions & 3 deletions packages/void/src/router/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,16 @@ export function compileRouteTable(

if (!seenFiles.has(route.filePath)) {
seenFiles.add(route.filePath);
lines.push(`let ${varName};`);
if (options.dev !== true) {
lines.push(`let ${varName};`);
}
lines.push(`async function ${loaderName}() {`);
lines.push(` if (!${varName}) ${varName} = await import(${JSON.stringify(filePath)});`);
lines.push(` return ${varName};`);
if (options.dev === true) {
lines.push(` return import(${JSON.stringify(filePath)});`);
} else {
lines.push(` if (!${varName}) ${varName} = await import(${JSON.stringify(filePath)});`);
lines.push(` return ${varName};`);
}
lines.push(`}`);
}

Expand Down
29 changes: 29 additions & 0 deletions packages/void/test/unit/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,35 @@ describe('compileRouteTable', () => {
expect(code).toContain('export default app');
});

it('does not cache route modules in dev so transitive route dependency changes reload', () => {
const scan: ScanResult = {
routes: [
{
pattern: '/api/hello',
methods: ['GET'],
params: [],
catchAll: false,
env: null,
filePath: 'api/hello.ts',
},
],
middleware: [],
jobs: [],
queues: [],
pages: { pages: [], layouts: [], namedLayouts: [] },
};

const code = compileRouteTable(scan, '/routes', '/middleware', '/crons', '/queues', null, {
...testOptions,
dev: true,
});

expect(code).toContain('async function _load_api_hello_ts()');
expect(code).toContain('return import("/routes/api/hello.ts")');
expect(code).not.toContain('let _mod_api_hello_ts');
expect(code).not.toContain('if (!_mod_api_hello_ts)');
});

it('generates multiple case labels for multi-cron jobs', () => {
const scan: ScanResult = {
routes: [],
Expand Down