mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-20 06:15:15 +00:00
Convert whitespace to tabs. Add some unit tests for create routes
This commit is contained in:
@@ -4,184 +4,187 @@ 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) => !/(^|\/|\\)(_(?!error\.html)|\.(?!well-known))/.test(file))
|
.filter((file: string) => !/(^|\/|\\)(_(?!error\.html)|\.(?!well-known))/.test(file))
|
||||||
.map((file: string) => {
|
.map((file: string) => {
|
||||||
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`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file === '4xx.html' || file === '5xx.html') {
|
if (file === '4xx.html' || file === '5xx.html') {
|
||||||
throw new Error('As of Sapper 0.14, 4xx.html and 5xx.html should be replaced with _error.html');
|
throw new Error('As of Sapper 0.14, 4xx.html and 5xx.html should be replaced with _error.html');
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (/^index(\..+)?/.test(parts[parts.length - 1])) {
|
if (/^index(\..+)?/.test(parts[parts.length - 1])) {
|
||||||
const part = parts.pop();
|
const part = parts.pop();
|
||||||
if (parts.length > 0) parts[parts.length - 1] += part.slice(5);
|
if (parts.length > 0) parts[parts.length - 1] += part.slice(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
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] === '_error') return -1;
|
if (a.parts[0] === '_error') return -1;
|
||||||
if (b.parts[0] === '_error') return 1;
|
if (b.parts[0] === '_error') 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 && a_match[1] !== b_match[1]) {
|
||||||
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: object = {};
|
const match_patterns: object = {};
|
||||||
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') {
|
||||||
// Make a map of the regexp patterns
|
if (/[\(\)\?\:]/.exec(match[2])) {
|
||||||
match_patterns[match[1]] = `(${match[2]}?)`;
|
throw new Error('Sapper does not allow (, ), ? or : in RegExp routes yet');
|
||||||
}
|
}
|
||||||
}
|
// Make a map of the regexp patterns
|
||||||
|
match_patterns[match[1]] = `(${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) {
|
||||||
// Get keys from part and replace with stored match patterns
|
// Get keys from part and replace with stored match patterns
|
||||||
const keys = part.replace(/\(.*?\)/, '').split(/[\[\]]/).filter((x, i) => { if (i % 2) return x });
|
const keys = part.replace(/\(.*?\)/, '').split(/[\[\]]/).filter((x, i) => { if (i % 2) return x });
|
||||||
let matcher = part;
|
let matcher = part;
|
||||||
keys.forEach(k => {
|
keys.forEach(k => {
|
||||||
const key_pattern = new RegExp('\\[' + k + '(?:\\((.+?)\\))?\\]');
|
const key_pattern = new RegExp('\\[' + k + '(?:\\((.+?)\\))?\\]');
|
||||||
matcher = matcher.replace(key_pattern, match_patterns[k] || `([^/]+?)`);
|
matcher = matcher.replace(key_pattern, match_patterns[k] || `([^/]+?)`);
|
||||||
})
|
})
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,17 @@ describe('create_routes', () => {
|
|||||||
|
|
||||||
it('sorts routes correctly', () => {
|
it('sorts routes correctly', () => {
|
||||||
const routes = create_routes({
|
const routes = create_routes({
|
||||||
files: ['index.html', 'about.html', 'post/f[xx].html', '[wildcard].html', 'post/foo.html', 'post/[id].html', 'post/bar.html', 'post/[id].json.js']
|
files: [
|
||||||
|
'index.html',
|
||||||
|
'about.html',
|
||||||
|
'post/f[xx].html',
|
||||||
|
'[wildcard].html',
|
||||||
|
'post/foo.html',
|
||||||
|
'post/[id].html',
|
||||||
|
'post/bar.html',
|
||||||
|
'post/[id].json.js',
|
||||||
|
'post/[id([0-9-a-z]{3,})].html',
|
||||||
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
@@ -56,6 +66,7 @@ describe('create_routes', () => {
|
|||||||
'post/bar.html',
|
'post/bar.html',
|
||||||
'post/foo.html',
|
'post/foo.html',
|
||||||
'post/f[xx].html',
|
'post/f[xx].html',
|
||||||
|
'post/[id([0-9-a-z]{3,})].html', // RegExp is more specific
|
||||||
'post/[id].json.js',
|
'post/[id].json.js',
|
||||||
'post/[id].html',
|
'post/[id].html',
|
||||||
'[wildcard].html'
|
'[wildcard].html'
|
||||||
@@ -63,6 +74,25 @@ describe('create_routes', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('distinguishes and sorts regexp routes correctly', () => {
|
||||||
|
const routes = create_routes({
|
||||||
|
files: [
|
||||||
|
'[slug].html',
|
||||||
|
'[slug([a-z]{2})].html',
|
||||||
|
'[slug([0-9-a-z]{3,})].html',
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
routes.map(r => r.handlers[0].file),
|
||||||
|
[
|
||||||
|
'[slug([0-9-a-z]{3,})].html',
|
||||||
|
'[slug([a-z]{2})].html',
|
||||||
|
'[slug].html',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('prefers index page to nested route', () => {
|
it('prefers index page to nested route', () => {
|
||||||
let routes = create_routes({
|
let routes = create_routes({
|
||||||
files: [
|
files: [
|
||||||
@@ -131,6 +161,24 @@ describe('create_routes', () => {
|
|||||||
'api/blog/[slug].js',
|
'api/blog/[slug].js',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// RegExp routes
|
||||||
|
routes = create_routes({
|
||||||
|
files: [
|
||||||
|
'blog/[slug].html',
|
||||||
|
'blog/index.html',
|
||||||
|
'blog/[slug([^0-9]+)].html',
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
routes.map(r => r.handlers[0].file),
|
||||||
|
[
|
||||||
|
'blog/index.html',
|
||||||
|
'blog/[slug([^0-9]+)].html',
|
||||||
|
'blog/[slug].html',
|
||||||
|
]
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('generates params', () => {
|
it('generates params', () => {
|
||||||
@@ -204,8 +252,15 @@ describe('create_routes', () => {
|
|||||||
files: ['[foo].html', '[bar]/index.html']
|
files: ['[foo].html', '[bar]/index.html']
|
||||||
});
|
});
|
||||||
}, /The \[foo\] and \[bar\]\/index routes clash/);
|
}, /The \[foo\] and \[bar\]\/index routes clash/);
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
create_routes({
|
||||||
|
files: ['[foo([0-9-a-z]+)].html', '[bar([0-9-a-z]+)]/index.html']
|
||||||
|
});
|
||||||
|
}, /The \[foo\(\[0-9-a-z\]\+\)\] and \[bar\(\[0-9-a-z\]\+\)\]\/index routes clash/);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('matches nested routes', () => {
|
it('matches nested routes', () => {
|
||||||
const route = create_routes({
|
const route = create_routes({
|
||||||
files: ['settings/[submenu].html']
|
files: ['settings/[submenu].html']
|
||||||
@@ -281,6 +336,14 @@ describe('create_routes', () => {
|
|||||||
}, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/);
|
}, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('errors when trying to use reserved characters in route regexp', () => {
|
||||||
|
assert.throws(() => {
|
||||||
|
create_routes({
|
||||||
|
files: ['[lang([a-z]{2}(?:-[a-z]{2,4})?)]']
|
||||||
|
});
|
||||||
|
}, /Sapper does not allow \(, \), \? or \: in RegExp routes yet/);
|
||||||
|
});
|
||||||
|
|
||||||
it('errors on 4xx.html', () => {
|
it('errors on 4xx.html', () => {
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
create_routes({
|
create_routes({
|
||||||
|
|||||||
Reference in New Issue
Block a user