mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-15 04:14:46 +00:00
dont treat files and dirs with leading _ as routes
This commit is contained in:
@@ -3,6 +3,8 @@ const path = require('path');
|
|||||||
module.exports = function create_matchers(files) {
|
module.exports = function create_matchers(files) {
|
||||||
return files
|
return files
|
||||||
.map(file => {
|
.map(file => {
|
||||||
|
if (/(^|\/|\\)_/.test(file)) return;
|
||||||
|
|
||||||
const parts = file.replace(/\.(html|js|mjs)$/, '').split(path.sep);
|
const parts = file.replace(/\.(html|js|mjs)$/, '').split(path.sep);
|
||||||
if (parts[parts.length - 1] === 'index') parts.pop();
|
if (parts[parts.length - 1] === 'index') parts.pop();
|
||||||
|
|
||||||
@@ -37,6 +39,7 @@ module.exports = function create_matchers(files) {
|
|||||||
dynamic
|
dynamic
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
|
.filter(Boolean)
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
return (
|
return (
|
||||||
(a.dynamic.length - b.dynamic.length) || // match static paths first
|
(a.dynamic.length - b.dynamic.length) || // match static paths first
|
||||||
|
|||||||
@@ -5,21 +5,21 @@ const create_matchers = require('./create_matchers.js');
|
|||||||
|
|
||||||
describe('create_matchers', () => {
|
describe('create_matchers', () => {
|
||||||
it('sorts routes correctly', () => {
|
it('sorts routes correctly', () => {
|
||||||
const matchers = create_matchers(['index.html', 'about.html', '%wildcard%.html', 'post/%id%.html']);
|
const matchers = create_matchers(['index.html', 'about.html', '[wildcard].html', 'post/[id].html']);
|
||||||
|
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
matchers.map(m => m.file),
|
matchers.map(m => m.file),
|
||||||
[
|
[
|
||||||
'about.html',
|
'about.html',
|
||||||
'index.html',
|
'index.html',
|
||||||
'post/%id%.html',
|
'post/[id].html',
|
||||||
'%wildcard%.html'
|
'[wildcard].html'
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('generates params', () => {
|
it('generates params', () => {
|
||||||
const matchers = create_matchers(['index.html', 'about.html', '%wildcard%.html', 'post/%id%.html']);
|
const matchers = create_matchers(['index.html', 'about.html', '[wildcard].html', 'post/[id].html']);
|
||||||
|
|
||||||
let file;
|
let file;
|
||||||
let params;
|
let params;
|
||||||
@@ -31,9 +31,21 @@ describe('create_matchers', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.equal(file, 'post/%id%.html');
|
assert.equal(file, 'post/[id].html');
|
||||||
assert.deepEqual(params, {
|
assert.deepEqual(params, {
|
||||||
id: '123'
|
id: '123'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('ignores files and directories with leading underscores', () => {
|
||||||
|
const matches = create_matchers(['index.html', '_foo.html', 'a/_b/c/d.html', 'e/f/g/h.html', 'i/_j.html']);
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
matches.map(m => m.file),
|
||||||
|
[
|
||||||
|
'e/f/g/h.html',
|
||||||
|
'index.html'
|
||||||
|
]
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user