From b7bb4db8c15d8c64f0e36c31ba65d45ba0b1f707 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 28 Jun 2018 13:20:41 -0400 Subject: [PATCH] treat foo/index.json.js as foo.json.js - fixes #297 --- src/core/create_routes.ts | 5 ++++- test/app/routes/{blog.json.js => blog/index.json.js} | 2 +- test/unit/create_routes.test.js | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) rename test/app/routes/{blog.json.js => blog/index.json.js} (88%) diff --git a/src/core/create_routes.ts b/src/core/create_routes.ts index 0b6c3e8..fd02d88 100644 --- a/src/core/create_routes.ts +++ b/src/core/create_routes.ts @@ -17,7 +17,10 @@ export default function create_routes({ files } = { files: glob.sync('**/*.*', { const base = file.replace(/\.[^/.]+$/, ''); const parts = base.split('/'); // glob output is always posix-style - if (parts[parts.length - 1] === 'index') parts.pop(); + if (/^index(\..+)?/.test(parts[parts.length - 1])) { + const part = parts.pop(); + if (parts.length > 0) parts[parts.length - 1] += part.slice(5); + } return { files: [file], diff --git a/test/app/routes/blog.json.js b/test/app/routes/blog/index.json.js similarity index 88% rename from test/app/routes/blog.json.js rename to test/app/routes/blog/index.json.js index 9dd74e0..9d73d78 100644 --- a/test/app/routes/blog.json.js +++ b/test/app/routes/blog/index.json.js @@ -1,4 +1,4 @@ -import posts from './blog/_posts.js'; +import posts from './_posts.js'; const contents = JSON.stringify(posts.map(post => { return { diff --git a/test/unit/create_routes.test.js b/test/unit/create_routes.test.js index 95eb116..cae5dc2 100644 --- a/test/unit/create_routes.test.js +++ b/test/unit/create_routes.test.js @@ -296,4 +296,12 @@ describe('create_routes', () => { }); }, /As of Sapper 0.14, 4xx.html and 5xx.html should be replaced with _error.html/); }); + + it('treats foo/index.json.js the same as foo.json.js', () => { + const route = create_routes({ + files: ['foo/index.json.js'] + })[0]; + + assert.ok(route.test('/foo.json')); + }); }); \ No newline at end of file