From 25809ec409c99f0748747ec149f958265c02509d Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 25 Mar 2018 15:12:35 -0400 Subject: [PATCH] enforce stable sort --- src/core/create_routes.ts | 5 +++- test/unit/create_routes.test.js | 43 +++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/core/create_routes.ts b/src/core/create_routes.ts index 133b865..3888b87 100644 --- a/src/core/create_routes.ts +++ b/src/core/create_routes.ts @@ -102,7 +102,10 @@ export default function create_routes({ files } = { files: glob.sync('**/*.*', { } if (!a_sub_part.dynamic && a_sub_part.content !== b_sub_part.content) { - return b_sub_part.content.length - a_sub_part.content.length; + return ( + (b_sub_part.content.length - a_sub_part.content.length) || + (a_sub_part.content < b_sub_part.content ? -1 : 1) + ); } } } diff --git a/test/unit/create_routes.test.js b/test/unit/create_routes.test.js index 02d4c15..5b70ffe 100644 --- a/test/unit/create_routes.test.js +++ b/test/unit/create_routes.test.js @@ -12,8 +12,8 @@ describe('create_routes', () => { [ 'index.html', 'about.html', - 'post/foo.html', 'post/bar.html', + 'post/foo.html', 'post/f[xx].html', 'post/[id].json.js', 'post/[id].html', @@ -23,7 +23,7 @@ describe('create_routes', () => { }); it('prefers index page to nested route', () => { - const routes = create_routes({ + let routes = create_routes({ files: [ 'api/examples/[slug].js', 'api/examples/index.js', @@ -55,6 +55,45 @@ describe('create_routes', () => { 'api/gists/[id].js', ] ); + + routes = create_routes({ + files: [ + '4xx.html', + '5xx.html', + 'api/blog/[slug].js', + 'api/blog/index.js', + 'api/guide/contents.js', + 'api/guide/index.js', + 'blog/[slug].html', + 'blog/index.html', + 'blog/rss.xml.js', + 'gist/[id].js', + 'gist/create.js', + 'guide/index.html', + 'index.html', + 'repl/index.html' + ] + }); + + assert.deepEqual( + routes.map(r => r.file), + [ + '4xx.html', + '5xx.html', + 'index.html', + 'guide/index.html', + 'blog/index.html', + 'blog/rss.xml.js', + 'blog/[slug].html', + 'gist/create.js', + 'gist/[id].js', + 'repl/index.html', + 'api/guide/index.js', + 'api/guide/contents.js', + 'api/blog/index.js', + 'api/blog/[slug].js', + ] + ); }); it('generates params', () => {