mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-13 19:45:26 +00:00
split pages and server routes into separate arrays
This commit is contained in:
@@ -32,38 +32,42 @@ export function create_serviceworker_manifest({ routes, client_files }: {
|
||||
|
||||
export const shell = [\n\t${client_files.map((x: string) => `"${x}"`).join(',\n\t')}\n];
|
||||
|
||||
export const routes = [\n\t${routes.filter((r: Route) => r.type === 'page' && !/^_[45]xx$/.test(r.id)).map((r: Route) => `{ pattern: ${r.pattern} }`).join(',\n\t')}\n];
|
||||
export const routes = [\n\t${routes.pages.filter(r => r.id !== '_error').map((r: Route) => `{ pattern: ${r.pattern} }`).join(',\n\t')}\n];
|
||||
`.replace(/^\t\t/gm, '').trim();
|
||||
|
||||
write_if_changed(`${locations.app()}/manifest/service-worker.js`, code);
|
||||
}
|
||||
|
||||
function generate_client(routes: Route[], path_to_routes: string, dev_port?: number) {
|
||||
const page_ids = new Set(routes.pages.map(page => page.id));
|
||||
const server_routes_to_ignore = routes.server_routes.filter(route => !page_ids.has(route.id));
|
||||
|
||||
const pages = routes.pages.filter(page => page.id !== '_error');
|
||||
const error_route = routes.pages.find(page => page.id === '_error');
|
||||
|
||||
let code = `
|
||||
// This file is generated by Sapper — do not edit it!
|
||||
export const routes = [
|
||||
${routes
|
||||
.map(route => {
|
||||
const page = route.handlers.find(({ type }) => type === 'page');
|
||||
|
||||
if (!page) {
|
||||
return `{ pattern: ${route.pattern}, ignore: true }`;
|
||||
}
|
||||
export const routes = {
|
||||
ignore: [${server_routes_to_ignore.map(route => route.pattern).join(', ')}],
|
||||
|
||||
pages: [
|
||||
${pages.map(page => {
|
||||
const file = posixify(`${path_to_routes}/${page.file}`);
|
||||
|
||||
if (route.id === '_error') {
|
||||
return `{ error: true, load: () => import(/* webpackChunkName: "${route.id}" */ '${file}') }`;
|
||||
if (page.id === '_error') {
|
||||
return `{ error: true, load: () => import(/* webpackChunkName: "${page.id}" */ '${file}') }`;
|
||||
}
|
||||
|
||||
const params = route.params.length === 0
|
||||
const params = page.params.length === 0
|
||||
? '{}'
|
||||
: `{ ${route.params.map((part, i) => `${part}: match[${i + 1}]`).join(', ')} }`;
|
||||
: `{ ${page.params.map((part, i) => `${part}: match[${i + 1}]`).join(', ')} }`;
|
||||
|
||||
return `{ pattern: ${route.pattern}, params: ${route.params.length > 0 ? `match` : `()`} => (${params}), load: () => import(/* webpackChunkName: "${route.id}" */ '${file}') }`;
|
||||
})
|
||||
.join(',\n\t')}
|
||||
];`.replace(/^\t\t/gm, '').trim();
|
||||
return `{ pattern: ${page.pattern}, params: ${page.params.length > 0 ? `match` : `()`} => (${params}), load: () => import(/* webpackChunkName: "${page.id}" */ '${file}') }`;
|
||||
}).join(',\n\t\t\t\t')}
|
||||
],
|
||||
|
||||
error: () => import(/* webpackChunkName: '_error' */ '${posixify(`${path_to_routes}/${error_route.file}`)}')
|
||||
};`.replace(/^\t\t/gm, '').trim();
|
||||
|
||||
if (dev()) {
|
||||
const sapper_dev_client = posixify(
|
||||
@@ -83,43 +87,46 @@ function generate_client(routes: Route[], path_to_routes: string, dev_port?: num
|
||||
}
|
||||
|
||||
function generate_server(routes: Route[], path_to_routes: string) {
|
||||
const error_route = routes.pages.find(page => page.id === '_error');
|
||||
|
||||
const imports = [].concat(
|
||||
routes.server_routes.map(route =>
|
||||
`import * as route_${route.id} from '${posixify(`${path_to_routes}/${route.file}`)}';`),
|
||||
routes.pages.map(page =>
|
||||
`import page_${page.id} from '${posixify(`${path_to_routes}/${page.file}`)}';`),
|
||||
`import error from '${posixify(`${path_to_routes}/${error_route.file}`)}';`
|
||||
);
|
||||
|
||||
let code = `
|
||||
// This file is generated by Sapper — do not edit it!
|
||||
${routes
|
||||
.map(route =>
|
||||
route.handlers
|
||||
.map(({ type, file }, index) => {
|
||||
const module = posixify(`${path_to_routes}/${file}`);
|
||||
|
||||
return type === 'page'
|
||||
? `import ${route.id}${index} from '${module}';`
|
||||
: `import * as ${route.id}${index} from '${module}';`;
|
||||
})
|
||||
.join('\n')
|
||||
)
|
||||
.join('\n')}
|
||||
|
||||
export const routes = [
|
||||
${routes
|
||||
.map(route => {
|
||||
const handlers = route.handlers
|
||||
.map(({ type }, index) =>
|
||||
`{ type: '${type}', module: ${route.id}${index} }`)
|
||||
.join(', ');
|
||||
|
||||
if (route.id === '_error') {
|
||||
return `{ error: true, handlers: [${handlers}] }`;
|
||||
}
|
||||
${imports.join('\n')}
|
||||
|
||||
export const routes = {
|
||||
server_routes: [
|
||||
${routes.server_routes.map(route => {
|
||||
const params = route.params.length === 0
|
||||
? '{}'
|
||||
: `{ ${route.params.map((part, i) => `${part}: match[${i + 1}]`).join(', ')} }`;
|
||||
|
||||
return `{ id: '${route.id}', pattern: ${route.pattern}, params: ${route.params.length > 0 ? `match` : `()`} => (${params}), handlers: [${handlers}] }`;
|
||||
})
|
||||
.join(',\n\t')
|
||||
return `{ id: '${route.id}', pattern: ${route.pattern}, params: ${route.params.length > 0 ? `match` : `()`} => (${params}), handlers: route_${route.id} }`;
|
||||
}).join(',\n\t\t\t\t')}
|
||||
],
|
||||
|
||||
pages: [
|
||||
${routes.pages.map(page => {
|
||||
const params = page.params.length === 0
|
||||
? '{}'
|
||||
: `{ ${page.params.map((part, i) => `${part}: match[${i + 1}]`).join(', ')} }`;
|
||||
|
||||
return `{ id: '${page.id}', pattern: ${page.pattern}, params: ${page.params.length > 0 ? `match` : `()`} => (${params}), handler: page_${page.id} }`;
|
||||
}).join(',\n\t\t\t\t')}
|
||||
],
|
||||
|
||||
error: {
|
||||
error: true,
|
||||
handler: error
|
||||
}
|
||||
];`.replace(/^\t\t/gm, '').trim();
|
||||
};`.replace(/^\t\t/gm, '').trim();
|
||||
|
||||
return code;
|
||||
}
|
||||
@@ -34,6 +34,8 @@ export default function create_routes({
|
||||
parts.join('_').replace(/[[\]]/g, '$').replace(/^\d/, '_$&').replace(/[^a-zA-Z0-9_$]/g, '_')
|
||||
) || '_';
|
||||
|
||||
const type = file.endsWith('.html') ? 'page' : 'route';
|
||||
|
||||
const params: string[] = [];
|
||||
const match_patterns: Record<string, string> = {};
|
||||
const param_pattern = /\[([^\(\]]+)(?:\((.+?)\))?\]/g;
|
||||
@@ -67,7 +69,7 @@ export default function create_routes({
|
||||
const key_pattern = new RegExp('\\[' + k + '(?:\\((.+?)\\))?\\]');
|
||||
matcher = matcher.replace(key_pattern, match_patterns[k] || `([^/]+?)`);
|
||||
})
|
||||
pattern_string = nested ? `(?:\\/${matcher}${pattern_string})?` : `\\/${matcher}${pattern_string}`;
|
||||
pattern_string = (nested && type === 'page') ? `(?:\\/${matcher}${pattern_string})?` : `\\/${matcher}${pattern_string}`;
|
||||
} else {
|
||||
nested = false;
|
||||
pattern_string = `\\/${part}${pattern_string}`;
|
||||
@@ -93,7 +95,7 @@ export default function create_routes({
|
||||
return {
|
||||
id,
|
||||
base,
|
||||
type: file.endsWith('.html') ? 'page' : 'route',
|
||||
type,
|
||||
file,
|
||||
pattern,
|
||||
test,
|
||||
|
||||
Reference in New Issue
Block a user