mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-17 21:24:59 +00:00
Correct spacing
This commit is contained in:
@@ -4,171 +4,171 @@ import { locations } from '../config';
|
|||||||
import { Route } from '../interfaces';
|
import { Route } from '../interfaces';
|
||||||
|
|
||||||
export default function create_routes({ files } = { files: glob.sync('**/*.*', { cwd: locations.routes(), dot: true, nodir: true }) }) {
|
export default function create_routes({ files } = { files: glob.sync('**/*.*', { cwd: locations.routes(), dot: true, nodir: true }) }) {
|
||||||
const routes: Route[] = files
|
const routes: Route[] = files
|
||||||
.filter((file: string) => !/(^|\/|\\)_/.test(file))
|
.filter((file: string) => !/(^|\/|\\)_/.test(file))
|
||||||
.map((file: string) => {
|
.map((file: string) => {
|
||||||
if (/(^|\/|\\)(_|\.(?!well-known))/.test(file)) return;
|
if (/(^|\/|\\)(_|\.(?!well-known))/.test(file)) return;
|
||||||
|
|
||||||
if (/]\[/.test(file)) {
|
if (/]\[/.test(file)) {
|
||||||
throw new Error(`Invalid route ${file} — parameters must be separated`);
|
throw new Error(`Invalid route ${file} — parameters must be separated`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const base = file.replace(/\.[^/.]+$/, '');
|
const base = file.replace(/\.[^/.]+$/, '');
|
||||||
const parts = base.split('/'); // glob output is always posix-style
|
const parts = base.split('/'); // glob output is always posix-style
|
||||||
if (parts[parts.length - 1] === 'index') parts.pop();
|
if (parts[parts.length - 1] === 'index') parts.pop();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
files: [file],
|
files: [file],
|
||||||
base,
|
base,
|
||||||
parts
|
parts
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.filter((a, index, array) => {
|
.filter((a, index, array) => {
|
||||||
const found = array.slice(index + 1).find(b => a.base === b.base);
|
const found = array.slice(index + 1).find(b => a.base === b.base);
|
||||||
if (found) found.files.push(...a.files);
|
if (found) found.files.push(...a.files);
|
||||||
return !found;
|
return !found;
|
||||||
})
|
})
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
if (a.parts[0] === '4xx' || a.parts[0] === '5xx') return -1;
|
if (a.parts[0] === '4xx' || a.parts[0] === '5xx') return -1;
|
||||||
if (b.parts[0] === '4xx' || b.parts[0] === '5xx') return 1;
|
if (b.parts[0] === '4xx' || b.parts[0] === '5xx') return 1;
|
||||||
|
|
||||||
const max = Math.max(a.parts.length, b.parts.length);
|
const max = Math.max(a.parts.length, b.parts.length);
|
||||||
|
|
||||||
for (let i = 0; i < max; i += 1) {
|
for (let i = 0; i < max; i += 1) {
|
||||||
const a_part = a.parts[i];
|
const a_part = a.parts[i];
|
||||||
const b_part = b.parts[i];
|
const b_part = b.parts[i];
|
||||||
|
|
||||||
if (!a_part) return -1;
|
if (!a_part) return -1;
|
||||||
if (!b_part) return 1;
|
if (!b_part) return 1;
|
||||||
|
|
||||||
const a_sub_parts = get_sub_parts(a_part);
|
const a_sub_parts = get_sub_parts(a_part);
|
||||||
const b_sub_parts = get_sub_parts(b_part);
|
const b_sub_parts = get_sub_parts(b_part);
|
||||||
const max = Math.max(a_sub_parts.length, b_sub_parts.length);
|
const max = Math.max(a_sub_parts.length, b_sub_parts.length);
|
||||||
|
|
||||||
for (let i = 0; i < max; i += 1) {
|
for (let i = 0; i < max; i += 1) {
|
||||||
const a_sub_part = a_sub_parts[i];
|
const a_sub_part = a_sub_parts[i];
|
||||||
const b_sub_part = b_sub_parts[i];
|
const b_sub_part = b_sub_parts[i];
|
||||||
|
|
||||||
if (!a_sub_part) return 1; // b is more specific, so goes first
|
if (!a_sub_part) return 1; // b is more specific, so goes first
|
||||||
if (!b_sub_part) return -1;
|
if (!b_sub_part) return -1;
|
||||||
|
|
||||||
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
|
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
|
||||||
return a_sub_part.dynamic ? 1 : -1;
|
return a_sub_part.dynamic ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!a_sub_part.dynamic && a_sub_part.content !== b_sub_part.content) {
|
if (!a_sub_part.dynamic && a_sub_part.content !== b_sub_part.content) {
|
||||||
return (
|
return (
|
||||||
(b_sub_part.content.length - a_sub_part.content.length) ||
|
(b_sub_part.content.length - a_sub_part.content.length) ||
|
||||||
(a_sub_part.content < b_sub_part.content ? -1 : 1)
|
(a_sub_part.content < b_sub_part.content ? -1 : 1)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// If both parts dynamic, check for regexp patterns
|
// If both parts dynamic, check for regexp patterns
|
||||||
if (a_sub_part.dynamic && b_sub_part.dynamic) {
|
if (a_sub_part.dynamic && b_sub_part.dynamic) {
|
||||||
const regexp_pattern = /\((.*?)\)/;
|
const regexp_pattern = /\((.*?)\)/;
|
||||||
const a_match = regexp_pattern.exec(a_sub_part.content);
|
const a_match = regexp_pattern.exec(a_sub_part.content);
|
||||||
const b_match = regexp_pattern.exec(b_sub_part.content);
|
const b_match = regexp_pattern.exec(b_sub_part.content);
|
||||||
|
|
||||||
if (!a_match && b_match) {
|
if (!a_match && b_match) {
|
||||||
return 1; // No regexp, so less specific than b
|
return 1; // No regexp, so less specific than b
|
||||||
}
|
}
|
||||||
if (!b_match && a_match) {
|
if (!b_match && a_match) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (a_match && b_match) {
|
if (a_match && b_match) {
|
||||||
return b_match[1].length - a_match[1].length;
|
return b_match[1].length - a_match[1].length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`The ${a.base} and ${b.base} routes clash`);
|
throw new Error(`The ${a.base} and ${b.base} routes clash`);
|
||||||
})
|
})
|
||||||
.map(({ files, base, parts }) => {
|
.map(({ files, base, parts }) => {
|
||||||
const id = (
|
const id = (
|
||||||
parts.join('_').replace(/[[\]]/g, '$').replace(/^\d/, '_$&').replace(/[^a-zA-Z0-9_$]/g, '_')
|
parts.join('_').replace(/[[\]]/g, '$').replace(/^\d/, '_$&').replace(/[^a-zA-Z0-9_$]/g, '_')
|
||||||
) || '_';
|
) || '_';
|
||||||
|
|
||||||
const params: string[] = [];
|
const params: string[] = [];
|
||||||
const match_patterns: string[] = [];
|
const match_patterns: string[] = [];
|
||||||
const param_pattern = /\[([^\(]+)(?:\((.+?)\))?\]/g;
|
const param_pattern = /\[([^\(]+)(?:\((.+?)\))?\]/g;
|
||||||
let match;
|
let match;
|
||||||
while (match = param_pattern.exec(base)) {
|
while (match = param_pattern.exec(base)) {
|
||||||
params.push(match[1]);
|
params.push(match[1]);
|
||||||
if (typeof match[2] !== 'undefined') {
|
if (typeof match[2] !== 'undefined') {
|
||||||
match_patterns.push(`(${match[2]}?)`);
|
match_patterns.push(`(${match[2]}?)`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO can we do all this with sub-parts? or does
|
// TODO can we do all this with sub-parts? or does
|
||||||
// nesting make that impossible?
|
// nesting make that impossible?
|
||||||
let pattern_string = '';
|
let pattern_string = '';
|
||||||
let i = parts.length;
|
let i = parts.length;
|
||||||
let nested = true;
|
let nested = true;
|
||||||
while (i--) {
|
while (i--) {
|
||||||
const part = encodeURI(parts[i].normalize()).replace(/\?/g, '%3F').replace(/#/g, '%23').replace(/%5B/g, '[').replace(/%5D/g, ']');
|
const part = encodeURI(parts[i].normalize()).replace(/\?/g, '%3F').replace(/#/g, '%23').replace(/%5B/g, '[').replace(/%5D/g, ']');
|
||||||
const dynamic = ~part.indexOf('[');
|
const dynamic = ~part.indexOf('[');
|
||||||
|
|
||||||
if (dynamic) {
|
if (dynamic) {
|
||||||
const matcher = part.replace(param_pattern, match_patterns[i] || `([^/]+?)`);
|
const matcher = part.replace(param_pattern, match_patterns[i] || `([^/]+?)`);
|
||||||
pattern_string = nested ? `(?:\\/${matcher}${pattern_string})?` : `\\/${matcher}${pattern_string}`;
|
pattern_string = nested ? `(?:\\/${matcher}${pattern_string})?` : `\\/${matcher}${pattern_string}`;
|
||||||
} else {
|
} else {
|
||||||
nested = false;
|
nested = false;
|
||||||
pattern_string = `\\/${part}${pattern_string}`;
|
pattern_string = `\\/${part}${pattern_string}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pattern = new RegExp(`^${pattern_string}\\/?$`);
|
const pattern = new RegExp(`^${pattern_string}\\/?$`);
|
||||||
|
|
||||||
const test = (url: string) => pattern.test(url);
|
const test = (url: string) => pattern.test(url);
|
||||||
|
|
||||||
const exec = (url: string) => {
|
const exec = (url: string) => {
|
||||||
const match = pattern.exec(url);
|
const match = pattern.exec(url);
|
||||||
if (!match) return;
|
if (!match) return;
|
||||||
|
|
||||||
const result: Record<string, string> = {};
|
const result: Record<string, string> = {};
|
||||||
params.forEach((param, i) => {
|
params.forEach((param, i) => {
|
||||||
result[param] = match[i + 1];
|
result[param] = match[i + 1];
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
handlers: files.map(file => ({
|
handlers: files.map(file => ({
|
||||||
type: path.extname(file) === '.html' ? 'page' : 'route',
|
type: path.extname(file) === '.html' ? 'page' : 'route',
|
||||||
file
|
file
|
||||||
})).sort((a, b) => {
|
})).sort((a, b) => {
|
||||||
if (a.type === 'page' && b.type === 'route') {
|
if (a.type === 'page' && b.type === 'route') {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a.type === 'route' && b.type === 'page') {
|
if (a.type === 'route' && b.type === 'page') {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}),
|
}),
|
||||||
pattern,
|
pattern,
|
||||||
test,
|
test,
|
||||||
exec,
|
exec,
|
||||||
parts,
|
parts,
|
||||||
params
|
params
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
return routes;
|
return routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_sub_parts(part: string) {
|
function get_sub_parts(part: string) {
|
||||||
return part.split(/\[(.+)\]/)
|
return part.split(/\[(.+)\]/)
|
||||||
.map((content, i) => {
|
.map((content, i) => {
|
||||||
if (!content) return null;
|
if (!content) return null;
|
||||||
return {
|
return {
|
||||||
content,
|
content,
|
||||||
dynamic: i % 2 === 1
|
dynamic: i % 2 === 1
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user