From fa70024a92e36af79e86367b6cbc1fc2284a42e1 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 12 Dec 2017 06:19:28 -0500 Subject: [PATCH] dont treat files and dirs with leading _ as routes --- utils/create_matchers.js | 3 +++ utils/create_matchers.test.js | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/utils/create_matchers.js b/utils/create_matchers.js index 525e772..8da4bca 100644 --- a/utils/create_matchers.js +++ b/utils/create_matchers.js @@ -3,6 +3,8 @@ const path = require('path'); module.exports = function create_matchers(files) { return files .map(file => { + if (/(^|\/|\\)_/.test(file)) return; + const parts = file.replace(/\.(html|js|mjs)$/, '').split(path.sep); if (parts[parts.length - 1] === 'index') parts.pop(); @@ -37,6 +39,7 @@ module.exports = function create_matchers(files) { dynamic }; }) + .filter(Boolean) .sort((a, b) => { return ( (a.dynamic.length - b.dynamic.length) || // match static paths first diff --git a/utils/create_matchers.test.js b/utils/create_matchers.test.js index e74f068..e998923 100644 --- a/utils/create_matchers.test.js +++ b/utils/create_matchers.test.js @@ -5,21 +5,21 @@ const create_matchers = require('./create_matchers.js'); describe('create_matchers', () => { 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( matchers.map(m => m.file), [ 'about.html', 'index.html', - 'post/%id%.html', - '%wildcard%.html' + 'post/[id].html', + '[wildcard].html' ] ); }); 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 params; @@ -31,9 +31,21 @@ describe('create_matchers', () => { } } - assert.equal(file, 'post/%id%.html'); + assert.equal(file, 'post/[id].html'); assert.deepEqual(params, { 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' + ] + ); + }); }); \ No newline at end of file